Skip to content

Commit

Permalink
[dx] update settings-store structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreezag committed Jun 30, 2024
1 parent 489f840 commit 28c7517
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 108 deletions.
108 changes: 0 additions & 108 deletions src/shared/stores/settings.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/shared/stores/settings/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export const THEME_MODES = {
LIGHT: "light",
DARK: "dark",
};

2 changes: 2 additions & 0 deletions src/shared/stores/settings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {useSettingsStore} from './settings-store'
export * from './constants'
69 changes: 69 additions & 0 deletions src/shared/stores/settings/local-storage-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {LOCAL_STORAGE_KEYS} from "../../types";
import {THEME_MODES} from "./constants";

export const checkIfThemeActive = () => {
if (!process.client) {
return THEME_MODES.LIGHT;
}

const isStoredDarkTheme = window?.localStorage.getItem(LOCAL_STORAGE_KEYS.THEME) === THEME_MODES.DARK;
const isSystemDarkTheme = window.matchMedia("(prefers-color-scheme: dark)").matches;

if (isStoredDarkTheme || isSystemDarkTheme) {
document?.documentElement?.classList?.add(THEME_MODES.DARK);

return THEME_MODES.DARK
}

document?.documentElement?.classList?.remove(THEME_MODES.DARK);

return THEME_MODES.LIGHT
};

export const syncThemeLocalStorage = (themeName: string) => {
window?.localStorage.setItem(LOCAL_STORAGE_KEYS.THEME, themeName);

if (themeName === THEME_MODES.LIGHT) {
window?.document?.documentElement?.classList?.remove(THEME_MODES.DARK);
} else {
window?.document?.documentElement?.classList?.add(THEME_MODES.DARK);
}
}

export const getFixedHeaderState = () => {
if (!process.client) {
return false;
}

const storedValue: string = window?.localStorage.getItem(LOCAL_STORAGE_KEYS.NAVBAR) || "true";

const isFixed: boolean = storedValue === "true"

if (isFixed) {
document?.documentElement?.classList?.add("navbar-fixed");
} else {
document?.documentElement?.classList?.remove("navbar-fixed");
}

return isFixed;
}

export const syncFixedHeaderLocalStorage = (state: boolean) => {
window?.localStorage.setItem(LOCAL_STORAGE_KEYS.NAVBAR, String(state));

if (state) {
window?.document?.documentElement?.classList?.add("navbar-fixed");
} else {
window?.document?.documentElement?.classList?.remove("navbar-fixed");
}
}

export const getEventsCountVisibleState = (): boolean => {
const storageValue = window?.localStorage?.getItem(LOCAL_STORAGE_KEYS.EVENT_COUNTS) || "true";

return storageValue === "true";
};

export const syncEventsCountVisibleLocalStorage = (state: boolean) => {
window?.localStorage?.setItem(LOCAL_STORAGE_KEYS.EVENT_COUNTS, String(state));
}
55 changes: 55 additions & 0 deletions src/shared/stores/settings/settings-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {defineStore} from "pinia";
import {useSettings} from "../../lib/use-settings";
import type { TSettings } from "../../types";
import {THEME_MODES} from "./constants";
import {
getEventsCountVisibleState,
getFixedHeaderState,
checkIfThemeActive, syncEventsCountVisibleLocalStorage, syncFixedHeaderLocalStorage, syncThemeLocalStorage
} from "./local-storage-actions";

export const useSettingsStore = defineStore("settingsStore", {
state: () => ({
apiVersion: '',
auth: {
isEnabled: false,
loginUrl: '/login',
},
themeType: checkIfThemeActive(),
isFixedHeader: getFixedHeaderState(),
isVisibleEventCounts: getEventsCountVisibleState(),
}),
actions: {
changeTheme() {
this.themeType = this.themeType === THEME_MODES.DARK
? THEME_MODES.LIGHT
: THEME_MODES.DARK;

syncThemeLocalStorage(this.themeType)
},
changeNavbar() {
this.isFixedHeader = !this.isFixedHeader;

syncFixedHeaderLocalStorage(this.isFixedHeader)
},
changeEventCountsVisibility() {
this.isVisibleEventCounts = !this.isVisibleEventCounts;

syncEventsCountVisibleLocalStorage(this.isVisibleEventCounts)
},
initialize() {
const {api: { getSettings }} = useSettings();

getSettings().then(({ version, auth } = {} as TSettings) => {
if (version) {
this.apiVersion = version
}

if (auth) {
this.auth.isEnabled = auth.enabled;
this.auth.loginUrl = auth.login_url;
}
})
}
},
});

0 comments on commit 28c7517

Please sign in to comment.