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

fix: allow current geoloc on map #1314

Merged
merged 11 commits into from
Jul 5, 2024
2 changes: 1 addition & 1 deletion cypress/e2e/item/home/home.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('Home', () => {
i18n.changeLanguage(CURRENT_USER.extra.lang as string);
cy.visit(`${HOME_PATH}?mode=map`);

cy.get(`#${buildMapViewId()}`).should('be.visible');
cy.get(`#${buildMapViewId()}`, { timeout: 10000 }).should('be.visible');
});

describe('Grid', () => {
Expand Down
3 changes: 2 additions & 1 deletion cypress/e2e/item/view/viewFolder.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('View Folder', () => {
const { id } = parentItem;
cy.visit(buildItemPath(id, { mode: ItemLayoutMode.Map }));

cy.get(`#${buildMapViewId(id)}`).should('be.visible');
// wait on getting geoloc
cy.get(`#${buildMapViewId(id)}`, { timeout: 10000 }).should('be.visible');
});

describe('Grid', () => {
Expand Down
55 changes: 32 additions & 23 deletions src/components/item/MapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,52 @@ const useCurrentLocation = (enableGeolocation = false) => {
lng: number;
}>();

const getCurrentPosition = () => {
const success = (pos: {
coords: { latitude: number; longitude: number };
}) => {
const crd = pos.coords;
setCurrentPosition({ lat: crd.latitude, lng: crd.longitude });
setHasFetchedCurrentLocation(true);
};

navigator.geolocation.getCurrentPosition(
success,
(err: { code: number; message: string }) => {
// eslint-disable-next-line no-console
console.warn(`ERROR(${err.code}): ${err.message}`);
setHasFetchedCurrentLocation(true);
},
options,
);
};

// get current location
useEffect(() => {
if (enableGeolocation) {
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions#examples
if (!navigator.permissions) {
setHasFetchedCurrentLocation(true);
} else {
if (navigator.permissions) {
// check permissions
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/permissions#examples
navigator.permissions
.query({ name: 'geolocation' })
.then(({ state }) => {
if (state === 'granted') {
const success = (pos: {
coords: { latitude: number; longitude: number };
}) => {
const crd = pos.coords;
setCurrentPosition({ lat: crd.latitude, lng: crd.longitude });
setHasFetchedCurrentLocation(true);
};

navigator.geolocation.getCurrentPosition(
success,
(err: { code: number; message: string }) => {
// eslint-disable-next-line no-console
console.warn(`ERROR(${err.code}): ${err.message}`);
setHasFetchedCurrentLocation(true);
},
options,
);
} else {
if (state === 'denied') {
console.error('geolocation denied:', state);
setHasFetchedCurrentLocation(true);
}
// allows granted and prompt values (safari)
else {
getCurrentPosition();
}
})
.catch((e) => {
console.error('geolocation denied:', e);
setHasFetchedCurrentLocation(true);
});
} else {
// navigator.permissions does not exist in safari
// still try to get position for webview's ios
getCurrentPosition();
}
}
}, [enableGeolocation]);
Expand Down
Loading