Skip to content

Commit

Permalink
use cache instead of atom, but does not rerender when updating cache
Browse files Browse the repository at this point in the history
  • Loading branch information
RoanPaulus committed Jan 15, 2025
1 parent 9866835 commit 098fdad
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
61 changes: 27 additions & 34 deletions src/client/pages/HLI/HLI.hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
import { atom, useRecoilState } from 'recoil';
import useSWRMutation from 'swr/dist/mutation';
import { HttpStatusCode } from 'axios';
import { useSWRConfig } from 'swr';
import useSWRMutation from 'swr/mutation';

import { StadspasFrontend } from '../../../server/services/hli/stadspas-types';
import { useAppStateGetter } from '../../hooks/useAppState';

type StadspasActiefByID = {
[id: string]: boolean;
};

const stadspasActiefAtom = atom<StadspasActiefByID>({
key: 'stadspasActief',
default: {},
});

export function useStadspassen() {
const { HLI } = useAppStateGetter();
const [stadspasActief, setStadspassenActiefStatus] =
useRecoilState(stadspasActiefAtom);

const stadspassen: StadspasFrontend[] = (HLI.content?.stadspas || []).map(
(pas) => {
const stadspas = {
...pas,
actief: stadspasActief[pas.id] ?? true,
};
return stadspas;
const { cache } = useSWRConfig();

const stadspassen = (HLI.content?.stadspas || []).map((pas) => {
let cachedPas;
if (pas.blockPassURL) {
cachedPas = cache.get(pas.blockPassURL);
}
);

return [stadspassen, setStadspassenActiefStatus] as const;
}
const stadspas = {
...pas,
actief: cachedPas?.data?.actief ?? pas.actief,
};

export function useBlockStadspas(url: string | null, stadspasId: string) {
const setStadspassenActiefStatus = useStadspassen()[1];
return stadspas;
});

return [stadspassen] as const;
}

export function useBlockStadspas(url: string | null) {
return useSWRMutation(
url,
async (url) => {
Expand All @@ -42,16 +34,17 @@ export function useBlockStadspas(url: string | null, stadspasId: string) {
credentials: 'include',
});

if (!response.ok) {
if (response.status !== HttpStatusCode.Ok) {
throw new Error('Request returned with an error');
}

setStadspassenActiefStatus((stadspasActiefState) => {
return { ...stadspasActiefState, [stadspasId]: false };
});

return response;
return response.json();
},
{ revalidate: false, populateCache: false }
{
revalidate: false,
populateCache: (updatedStadspasResponse, stadspassen) => {
return { ...stadspassen, ...updatedStadspasResponse.content };
},
}
);
}
12 changes: 8 additions & 4 deletions src/client/pages/HLI/HLIStadspas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,16 @@ function BlockStadspas({ stadspas }: { stadspas: StadspasFrontend }) {
}

const [isModalOpen, setIsModalOpen] = useState(false);
const [showError, setShowError] = useState(false);

const { error, isMutating, trigger } = useBlockStadspas(
stadspas.blockPassURL,
stadspas.id
stadspas.blockPassURL
);

if (error) {
setShowError(true);
}

if (isMutating) {
return <Spinner></Spinner>;
}
Expand All @@ -272,12 +276,12 @@ function BlockStadspas({ stadspas }: { stadspas: StadspasFrontend }) {
>
Blokeer deze Stadspas
</Button>
{error && (
{showError && (
<Alert
className={styles.ErrorAlert}
heading="Fout bij het blokeren van de pas"
closeable={true}
onClose={() => {}}
onClose={() => setShowError(false)}
severity="error"
>
Probeer het later nog eens. Als dit niet lukt bel dan naar{' '}
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/hli/stadspas-gpass-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ async function blockStadspas_(
if (pas.actief) {
throw Error('City pass is still active after trying to block it.');
}
return null;
return pas;
},
});

Expand Down

0 comments on commit 098fdad

Please sign in to comment.