-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persisterer language og context valg til cookies (#451)
\+ litt diverse rydding, refactoring av typer, fjerner ubrukte funksjoner, etc
- Loading branch information
1 parent
22c564c
commit 4fec939
Showing
11 changed files
with
163 additions
and
98 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
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 |
---|---|---|
@@ -1,37 +1,70 @@ | ||
import { ClientParams, Environment } from "decorator-shared/params"; | ||
import Cookies from "js-cookie"; | ||
import { | ||
ClientParams, | ||
Context, | ||
Environment, | ||
Language, | ||
} from "decorator-shared/params"; | ||
import { createEvent } from "./events"; | ||
|
||
export const hasParam = (paramKey: keyof ClientParams): boolean => { | ||
return window.__DECORATOR_DATA__.params[paramKey] !== undefined; | ||
}; | ||
type ParamKey = keyof ClientParams; | ||
type EnvKey = keyof Environment; | ||
|
||
const CONTEXT_COOKIE = "decorator-context"; | ||
const LANGUAGE_COOKIE = "decorator-language"; | ||
|
||
export const param = <TKey extends keyof ClientParams>(paramKey: TKey) => { | ||
return window.__DECORATOR_DATA__.params[paramKey] as ClientParams[TKey]; | ||
export const param = <TKey extends ParamKey>(paramKey: TKey) => { | ||
return window.__DECORATOR_DATA__.params[paramKey]; | ||
}; | ||
|
||
export const env = <TKey extends keyof Environment>(envKey: TKey): string => { | ||
return window.__DECORATOR_DATA__.env[envKey] as Environment[TKey]; | ||
export const env = <TKey extends EnvKey>(envKey: TKey) => { | ||
return window.__DECORATOR_DATA__.env[envKey]; | ||
}; | ||
|
||
export const updateDecoratorParams = (params: Partial<ClientParams>) => { | ||
const updatedParams = params; | ||
const updatedParams = { ...params }; | ||
|
||
Object.entries(params).map(([key, value]) => { | ||
if (param(key as keyof ClientParams) === value) { | ||
delete updatedParams[key as keyof ClientParams]; | ||
Object.entries(params).forEach(([key, value]) => { | ||
if (param(key as ParamKey) === value) { | ||
delete updatedParams[key as ParamKey]; | ||
} | ||
}); | ||
|
||
if (Object.keys(updatedParams).length > 0) { | ||
window.__DECORATOR_DATA__.params = { | ||
...window.__DECORATOR_DATA__.params, | ||
...updatedParams, | ||
}; | ||
window.__DECORATOR_DATA__.params = { | ||
...window.__DECORATOR_DATA__.params, | ||
...updatedParams, | ||
}; | ||
|
||
Cookies.set(CONTEXT_COOKIE, param("context")); | ||
Cookies.set(LANGUAGE_COOKIE, param("language")); | ||
|
||
if (Object.keys(updatedParams).length > 0) { | ||
window.dispatchEvent( | ||
createEvent("paramsupdated", { | ||
detail: { params: updatedParams }, | ||
}), | ||
); | ||
} | ||
}; | ||
|
||
export const initParams = () => { | ||
const rawParams = window.__DECORATOR_DATA__.rawParams; | ||
|
||
const initialParams: Partial<ClientParams> = {}; | ||
|
||
const language = | ||
rawParams?.language || | ||
(Cookies.get(LANGUAGE_COOKIE) as Language | undefined); | ||
if (language) { | ||
initialParams.language = language; | ||
} | ||
|
||
const context = | ||
rawParams?.context || | ||
(Cookies.get(CONTEXT_COOKIE) as Context | undefined); | ||
if (context) { | ||
initialParams.context = context; | ||
} | ||
|
||
updateDecoratorParams(initialParams); | ||
}; |
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
Oops, something went wrong.