Skip to content

Commit

Permalink
Bruk mutable swr
Browse files Browse the repository at this point in the history
  • Loading branch information
joarau committed Oct 14, 2024
1 parent 64da8be commit caaaf56
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/api/stillings-api/stillingsanalyse.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -24,27 +20,30 @@ export type StillingsanalyseDTO = z.infer<typeof stillingsanalyseSchema>;
export type StillingsanalyseRequestDTO = z.infer<typeof stillingsanalyseRequestSchema>;

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: '' };

0 comments on commit caaaf56

Please sign in to comment.