diff --git a/src/utils/Fetch.ts b/src/utils/Fetch.ts index 9c36389f..6fc260b4 100644 --- a/src/utils/Fetch.ts +++ b/src/utils/Fetch.ts @@ -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); } diff --git a/src/view/dialogProvider/dialogStore.ts b/src/view/dialogProvider/dialogStore.ts index 755161bb..b6301d70 100644 --- a/src/view/dialogProvider/dialogStore.ts +++ b/src/view/dialogProvider/dialogStore.ts @@ -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'; @@ -132,13 +132,23 @@ export const useDialogStore = create( closeWebsocket(); }, pollForChanges: async (fnr) => { - let { sistOppdatert: remoteSistOppdatert } = await fetchData(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( + 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 => {