Skip to content

Commit

Permalink
Add fallback ID function for crypto.randomUUID
Browse files Browse the repository at this point in the history
  • Loading branch information
cskrov committed May 31, 2024
1 parent 5c91bbf commit 2382c2f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion frontend/src/redux/session/klage/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const createSessionCase = (
innsendingsytelse: Innsendingsytelse,
internalSaksnummer: string | null,
): ISessionCase => ({
id: crypto.randomUUID(),
id: getId(),
type,
innsendingsytelse,
foedselsnummer: '',
Expand All @@ -27,3 +27,19 @@ export const createSessionCase = (
modifiedByUser: new Date().toISOString(),
caseIsAtKA: null,
});

const SUPPORTS_CRYPTO =
'crypto' in window &&
typeof window.crypto === 'object' &&
window.crypto !== null &&
'getRandomValues' in window.crypto &&
typeof window.crypto.getRandomValues === 'function';

const fallbackIdGenerator = (): `${string}-${string}` => {
const now = new Date().getTime();
const random = Math.random().toString(36).substring(2);

return `${now}-${random}`;
};

const getId = SUPPORTS_CRYPTO ? crypto.randomUUID : fallbackIdGenerator;

0 comments on commit 2382c2f

Please sign in to comment.