Skip to content

Commit

Permalink
fix: Radius now displays properly on home page
Browse files Browse the repository at this point in the history
  • Loading branch information
devcshort committed Sep 30, 2024
1 parent e366a82 commit 28cc6ad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
23 changes: 21 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import { GetStaticPropsContext } from 'next';
import { GetServerSidePropsContext } from 'next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { HomeView } from '../features/home/views/home-view';
import {
serverSideAppConfig,
serverSideFlags,
} from '@/shared/lib/server-utils';
import { parseCookies } from 'nookies';
import {
USER_PREF_COORDS,
USER_PREF_DISTANCE,
USER_PREF_LOCATION,
} from '@/shared/lib/constants';
import { shake } from 'radash';

export async function getServerSideProps(ctx: GetServerSidePropsContext) {
const cookies = parseCookies(ctx);
const coords = cookies[USER_PREF_COORDS];
const location = cookies[USER_PREF_LOCATION];
const distance = cookies[USER_PREF_DISTANCE];

const pageProps = shake({
coords,
location,
distance,
});

export async function getStaticProps(ctx: GetStaticPropsContext) {
return {
props: {
...pageProps,
...(await serverSideAppConfig()),
...(await serverSideFlags()),
...(await serverSideTranslations(ctx.locale as string, [
Expand Down
25 changes: 16 additions & 9 deletions src/shared/components/jotai-hydration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ import {
favoriteListWithFavoritesAtom,
} from '../store/favorites';

function getCoordinates(pageProps, cookies) {
if (pageProps.coords) {
return decodeURIComponent(pageProps.coords)
.split(',')
.map((number) => parseFloat(number))
.filter((number) => !isNaN(number));
} else if (cookies[USER_PREF_COORDS]) {
return cookies[USER_PREF_COORDS].split(',')
.map((number) => parseFloat(number))
.filter((number) => !isNaN(number));
}

return [];
}

// This component handles the hydration of Jotai state as well as keeping it in sync with re-renders/fetches of new data
// This MUST be a child of the Jotai Provider component or hydration will not work
export function JotaiHydration({ pageProps }) {
Expand Down Expand Up @@ -54,15 +69,7 @@ export function JotaiHydration({ pageProps }) {
pageProps?.location ?? cookies?.[USER_PREF_LOCATION] ?? '',
searchLocationValidationError: '',
userLocation: pageProps?.location,
userCoordinates:
decodeURIComponent(pageProps?.coords)
?.split(',')
?.map((number) => parseFloat(number))
?.filter((number) => !isNaN(number)) ??
cookies?.[USER_PREF_COORDS]?.split(',')
?.map((number) => parseFloat(number))
?.filter((number) => !isNaN(number)) ??
[],
userCoordinates: getCoordinates(pageProps, cookies),
},
],
] as const);
Expand Down

0 comments on commit 28cc6ad

Please sign in to comment.