From caaaf56ec3510859b5b28f8007aa5fd3907fde43 Mon Sep 17 00:00:00 2001 From: Joar Aurdal Date: Mon, 14 Oct 2024 21:28:41 +0200 Subject: [PATCH] Bruk mutable swr --- src/api/stillings-api/stillingsanalyse.ts | 35 +++++++++++------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/api/stillings-api/stillingsanalyse.ts b/src/api/stillings-api/stillingsanalyse.ts index 541ab2c5..6a3cdc9e 100644 --- a/src/api/stillings-api/stillingsanalyse.ts +++ b/src/api/stillings-api/stillingsanalyse.ts @@ -1,8 +1,4 @@ -/** - * Endepunkt /rekrutteringsbistand/stillingsanalyse - */ -import { HttpResponse, http } from 'msw'; -import useSWRImmutable from 'swr/immutable'; +import useSWR from 'swr'; import { z } from 'zod'; import { postApiWithSchema } from '../fetcher'; @@ -24,27 +20,30 @@ export type StillingsanalyseDTO = z.infer; export type StillingsanalyseRequestDTO = z.infer; export const useStillingsanalyse = (props: StillingsanalyseRequestDTO, vis: boolean) => { + const { stillingsId, stillingstype, stillingstittel, stillingstekst } = props; + const key = - !vis || !props.stillingstekst || props.stillingstekst.trim() === '' + !vis || !stillingstekst || stillingstekst.trim() === '' ? null - : [stillingsanalyseEndepunkt, props]; - - const { data, error, isValidating } = useSWRImmutable(key, () => + : [ + stillingsanalyseEndepunkt, + stillingsId, + stillingstype, + stillingstittel, + stillingstekst, + ]; + + const fetcher = () => postApiWithSchema(stillingsanalyseSchema)({ url: stillingsanalyseEndepunkt, body: props, - }) - ); + }); + + const { data, error, isLoading } = useSWR(key, fetcher); return { stillingsanalyse: data ?? null, - isLoading: isValidating, + isLoading: isLoading, error: error, }; }; - -export const stillingsanalyseMockMsw = http.post(stillingsanalyseEndepunkt, async (_) => { - return HttpResponse.json(mock); -}); - -const mock: StillingsanalyseDTO = { sensitiv: false, begrunnelse: '' };