-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import casual from 'casual'; | ||
|
||
export const mock = (app) => { | ||
app.use('/min-side-arbeidsgiver/api/varslingStatus/v1', (req, res) => { | ||
res.send({ | ||
status: casual.boolean ? 'MANGLER_KOFUVI' : 'OK', | ||
varselTimestamp: '2021-01-01T00:00:00', | ||
kvittertEventTimestamp: '2021-01-04T00:00:00Z', | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { z } from 'zod'; | ||
import useSWR from 'swr'; | ||
import * as Sentry from '@sentry/browser'; | ||
import { Organisasjon } from '../altinn/organisasjon'; | ||
import { altinntjeneste, AltinntjenesteId } from '../altinn/tjenester'; | ||
import * as Record from '../utils/Record'; | ||
import { Set } from 'immutable'; | ||
import { useContext, useState } from 'react'; | ||
import { OrganisasjonsDetaljerContext } from './OrganisasjonDetaljerProvider'; | ||
|
||
const VarslingStatusRespons = z.object({ | ||
status: z.enum(['OK', 'MANGLER_KOFUVI', 'ANNEN_FEIL']), | ||
varselTimestamp: z.string(), | ||
kvittertEventTimestamp: z.string(), | ||
}); | ||
type VarslingStatus = z.infer<typeof VarslingStatusRespons>; | ||
|
||
const fallbackData: VarslingStatus = { | ||
status: 'OK', | ||
varselTimestamp: '', | ||
kvittertEventTimestamp: '', | ||
}; | ||
export const useVarslingStatus = (): VarslingStatus => { | ||
const { valgtOrganisasjon } = useContext(OrganisasjonsDetaljerContext); | ||
const [retries, setRetries] = useState(0); | ||
const { data } = useSWR( | ||
valgtOrganisasjon !== undefined | ||
? { | ||
url: '/min-side-arbeidsgiver/api/varslingStatus/v1', | ||
virksomhetsnummer: valgtOrganisasjon.organisasjon.OrganizationNumber, | ||
} | ||
: null, | ||
fetcher, | ||
{ | ||
onSuccess: () => setRetries(0), | ||
onError: (error) => { | ||
console.log('error', error); | ||
if (retries === 5) { | ||
Sentry.captureMessage( | ||
`hent varslingStatus fra min-side-arbeidsgiver feilet med ${ | ||
error.status !== undefined | ||
? `${error.status} ${error.statusText}` | ||
: error | ||
}` | ||
); | ||
} | ||
setRetries((x) => x + 1); | ||
}, | ||
errorRetryInterval: 300, | ||
fallbackData, | ||
} | ||
); | ||
console.log('useVarslingStatus', data); | ||
|
||
return data; | ||
}; | ||
|
||
const fetcher = async ({ url, virksomhetsnummer }: { url: string; virksomhetsnummer: string }) => { | ||
const respons = await fetch(url, { | ||
method: 'POST', | ||
body: JSON.stringify({ virksomhetsnummer }), | ||
}); | ||
|
||
if (respons.status !== 200) throw respons; | ||
|
||
return VarslingStatusRespons.parse(await respons.json()); | ||
}; |