Skip to content

Commit

Permalink
Integrate geolocation API
Browse files Browse the repository at this point in the history
  • Loading branch information
dnhn committed Dec 16, 2024
1 parent c3a08fb commit 1f82838
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
18 changes: 18 additions & 0 deletions website/src/app/[lang]/[region]/(website)/me/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DONATION_CERTIFICATE_FIRESTORE_PATH } from '@socialincome/shared/src/ty
import { Employer, EMPLOYERS_FIRESTORE_PATH } from '@socialincome/shared/src/types/employers';
import { USER_FIRESTORE_PATH } from '@socialincome/shared/src/types/user';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Geo } from '@vercel/functions';
import { addDoc, collection, deleteDoc, doc, getDocs, query, Timestamp, updateDoc, where } from 'firebase/firestore';
import { useContext } from 'react';
import { useFirestore } from 'reactfire';
Expand Down Expand Up @@ -170,3 +171,20 @@ export const useAddEmployer = () => {
await queryClient.invalidateQueries({ queryKey: ['me', 'employers'] });
};
};

export const useGeolocation = () => {
const api = useApi();
const {
data: geolocation,
isLoading,
error,
} = useQuery({
queryKey: ['geolocation'],
queryFn: async () => {
const response = await api.get('/api/geolocation');
return (await response.json()) as Geo;
},
});

return { geolocation, isLoading, error };
};
10 changes: 10 additions & 0 deletions website/src/app/api/geolocation/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { geolocation } from '@vercel/functions';
import { handleApiError } from '@/app/api/auth';

export async function GET(request: Request) {
try {
return Response.json(geolocation(request));
} catch (error: any) {
return handleApiError(error);
}
}
6 changes: 4 additions & 2 deletions website/src/components/navbar/navbar-client.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import { DefaultParams } from '@/app/[lang]/[region]';
import { useGeolocation } from '@/app/[lang]/[region]/(website)/me/hooks';
import { getFlagComponentByCurrency } from '@/components/country-flags';
import { DonateIcon } from '@/components/logos/donate-icon';
import { SIAnimatedLogo } from '@/components/logos/si-animated-logo';
Expand Down Expand Up @@ -408,11 +409,12 @@ const DesktopNavigation = ({

export function NavbarClient(props: NavbarProps) {
const { backgroundColor } = useGlobalStateProvider();
const { geolocation } = useGeolocation();

return (
<nav className={twMerge('theme-blue group/navbar fixed inset-x-0 top-0 z-20 flex flex-col', backgroundColor)}>
<DesktopNavigation {...props} />
<MobileNavigation {...props} />
<DesktopNavigation {...props} country={geolocation?.country} />
<MobileNavigation {...props} country={geolocation?.country} />
</nav>
);
}

0 comments on commit 1f82838

Please sign in to comment.