Skip to content

Commit

Permalink
Håndter 401 på sistOppdatert bedre
Browse files Browse the repository at this point in the history
  • Loading branch information
tu55eladd committed Sep 3, 2024
1 parent d32ec61 commit a66d4c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/utils/Fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ export function fnrQuery(fnr?: string): string {
return fnr ? `?fnr=${fnr}` : '';
}

export class UnautorizedError extends Error {
response: Response;
constructor(response: Response) {
super('Unauthorized request, session expired?');
this.response = response;
}
}
export function sjekkStatuskode(response: Response) {
if (response.status >= 200 && response.status < 300 && response.ok) {
return response;
}
if (response.status === 401) {
throw new UnautorizedError(response);
}
throw new Error(response.statusText || response.type);
}

Expand Down
26 changes: 18 additions & 8 deletions src/view/dialogProvider/dialogStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { create } from 'zustand';
import { Status } from '../../api/typer';
import { DialogData, KladdData, SistOppdatert } from '../../utils/Typer';
import { DialogState, isDialogReloading } from '../DialogProvider';
import { fetchData } from '../../utils/Fetch';
import { fetchData, UnautorizedError } from '../../utils/Fetch';
import { DialogApi } from '../../api/UseApiBasePath';
import { isAfter } from 'date-fns';
import { devtools } from 'zustand/middleware';
Expand Down Expand Up @@ -132,13 +132,23 @@ export const useDialogStore = create(
closeWebsocket();
},
pollForChanges: async (fnr) => {
let { sistOppdatert: remoteSistOppdatert } = await fetchData<SistOppdatert>(DialogApi.sistOppdatert, {
method: 'POST',
body: !fnr ? undefined : JSON.stringify({ fnr })
});
const { silentlyHentDialoger, sistOppdatert: localSistOppdatert } = get();
if (!!remoteSistOppdatert && isAfter(remoteSistOppdatert, localSistOppdatert)) {
await silentlyHentDialoger(fnr);
try {
let { sistOppdatert: remoteSistOppdatert } = await fetchData<SistOppdatert>(
DialogApi.sistOppdatert,
{
method: 'POST',
body: !fnr ? undefined : JSON.stringify({ fnr })
}
);
const { silentlyHentDialoger, sistOppdatert: localSistOppdatert } = get();
if (!!remoteSistOppdatert && isAfter(remoteSistOppdatert, localSistOppdatert)) {
await silentlyHentDialoger(fnr);
}
} catch (e) {
if (e instanceof UnautorizedError) {
} else {
console.error(e);
}
}
},
updateDialogInDialoger: (dialog: DialogData): DialogData => {
Expand Down

0 comments on commit a66d4c1

Please sign in to comment.