diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-06-05 21:50:26 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-06-05 21:50:26 +0200 |
| commit | 44a95927edb532f8982cf24c03d9fdd129016bd6 (patch) | |
| tree | cd0efffc182f9429f8ec2bfe9622e6d36446f112 /apps/web-shared/src/lib/helpers.ts | |
| parent | 54b63daf1b5cefb54a3c616a4fee3ef57a671936 (diff) | |
| download | greatoffice-44a95927edb532f8982cf24c03d9fdd129016bd6.tar.xz greatoffice-44a95927edb532f8982cf24c03d9fdd129016bd6.zip | |
feat: Implement new theme switcher component and backend
The theme is now shared between the domain returned by base_domain()
Diffstat (limited to 'apps/web-shared/src/lib/helpers.ts')
| -rw-r--r-- | apps/web-shared/src/lib/helpers.ts | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/apps/web-shared/src/lib/helpers.ts b/apps/web-shared/src/lib/helpers.ts index acf74d7..f2d0cca 100644 --- a/apps/web-shared/src/lib/helpers.ts +++ b/apps/web-shared/src/lib/helpers.ts @@ -1,4 +1,4 @@ -import {StorageKeys} from "$shared/lib/configuration"; +import {base_domain, CookieNames, StorageKeys} from "$shared/lib/configuration"; import {TimeEntryDto} from "$shared/lib/models/TimeEntryDto"; import {UnwrappedEntryDateTime} from "$shared/lib/models/UnwrappedEntryDateTime"; import {Temporal} from "@js-temporal/polyfill"; @@ -36,14 +36,24 @@ export function is_norwegian_phone_number(value: string): boolean { export function switch_theme() { const html = document.querySelector("html"); - if (html) { - if (html.dataset.theme === "dark") { - html.dataset.theme = "light"; - } else { - html.dataset.theme = "dark"; - } - localStorage.setItem(StorageKeys.theme, html.dataset.theme); + if (html.dataset.theme === "dark") { + html.dataset.theme = "light"; + } else { + html.dataset.theme = "dark"; } + set_cookie(CookieNames.theme, html.dataset.theme, base_domain()); +} + +export function get_cookie(name) { + const value = `; ${document.cookie}`; + const parts = value.split(`; ${name}=`); + if (parts.length === 2) return parts.pop().split(";").shift(); +} + +export function set_cookie(name, value, baseDomain = window.location.host) { + let asdf = name + "=" + encodeURIComponent(value) + (baseDomain ? ";domain=" + baseDomain : ""); + console.log(asdf); + document.cookie = asdf; } export function unwrap_date_time_from_entry(entry: TimeEntryDto): UnwrappedEntryDateTime { |
