Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Distance constraint add on frontend #2084

Open
wants to merge 15 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/mapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@turf/bbox": "^7.1.0",
"@turf/buffer": "^7.1.0",
"@turf/centroid": "^7.1.0",
"@turf/distance": "^7.2.0",
"@turf/helpers": "^7.1.0",
"@watergis/maplibre-gl-terradraw": "^0.5.1",
"drizzle-orm": "^0.35.3",
Expand Down
30 changes: 30 additions & 0 deletions src/mapper/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/mapper/src/lib/components/dialog-entities-actions.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script lang="ts">
import { distance } from '@turf/distance';
import type { Coord } from '@turf/helpers';
import { TaskStatusEnum, type ProjectData } from '$lib/types';
import { getEntitiesStatusStore } from '$store/entities.svelte.ts';
import { getAlertStore } from '$store/common.svelte.ts';
Expand All @@ -25,7 +27,37 @@
const selectedEntityCoordinate = $derived(entitiesStore.selectedEntityCoordinate);
const entityToNavigate = $derived(entitiesStore.entityToNavigate);

// check if distance constraint is set to projectand is valid
const isDistanceConstaintValid = (): boolean => {
if (projectData?.geo_restrict_force_error) {
const to = entitiesStore.selectedEntityCoordinate?.coordinate;
const from = entitiesStore.userLocationCoord;
if (!from) {
alertStore.setAlert({
message:
'This project has a distance constraint set. Please enable device geolocation for optimal functionality',
variant: 'warning',
});
return false;
}

const entityDistance = distance(from as Coord, to as Coord, { units: 'kilometers' }) * 1000;
if (entityDistance && entityDistance > projectData?.geo_restrict_distance_meters) {
alertStore.setAlert({
message: `The feature must be within ${projectData?.geo_restrict_distance_meters} meters of your location`,
variant: 'warning',
});
return false;
}

return true;
}
return true;
};

const mapFeature = () => {
if (!isDistanceConstaintValid()) return;

const xformId = projectData?.odk_form_id;
const entityUuid = selectedEntity?.entity_id;

Expand Down
1 change: 1 addition & 0 deletions src/mapper/src/lib/components/map/geolocation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@
on:click={() => {
entitiesStore.setToggleGeolocation(!entitiesStore.toggleGeolocation);
if (!entitiesStore.toggleGeolocation) {
entitiesStore.setUserLocationCoordinate(undefined);
exitNavigationMode();
}
}}
Expand Down
2 changes: 2 additions & 0 deletions src/mapper/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface ProjectData {
status: number;
hashtags: string[];
tasks: ProjectTask[];
geo_restrict_distance_meters: number;
geo_restrict_force_error: boolean;
}

export interface ZoomToTaskEventDetail {
Expand Down
Loading