Skip to content

Commit

Permalink
polish names of functions to sync with localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreezag committed Jul 3, 2024
1 parent 94852ad commit d2ccd16
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
20 changes: 10 additions & 10 deletions src/shared/stores/events/events-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineStore } from "pinia";
import {PAGE_TYPES} from "../../constants";
import type { EventId, EventType , ServerEvent, TEventsGroup} from '../../types';
import {EVENT_TYPES} from "../../types";
import { getLockedIds, syncCachedIdsLocalStorage, syncLockedIdsLocalStorage, getCachedIds } from "./local-storage-actions";
import { getStoredLockedIds, setStoredCachedIds, setStoredLockedIds, getStoredCachedIds } from "./local-storage-actions";
import type {TEventsCachedIdsMap} from "./types";

const MAX_EVENTS_COUNT = 500;
Expand All @@ -22,8 +22,8 @@ const initialCachedIds: TEventsCachedIdsMap = {
export const useEventsStore = defineStore("eventsStore", {
state: () => ({
events: [] as ServerEvent<unknown>[],
cachedIds: getCachedIds() || initialCachedIds,
lockedIds: getLockedIds() || [],
cachedIds: getStoredCachedIds() || initialCachedIds,
lockedIds: getStoredLockedIds() || [],
}),
getters: {
eventsCounts: ({events}) => (eventType: EVENT_TYPES | undefined): number => {
Expand Down Expand Up @@ -118,11 +118,11 @@ export const useEventsStore = defineStore("eventsStore", {
this.cachedIds[cachedType].push(event.uuid);
});

syncCachedIdsLocalStorage(this.cachedIds);
setStoredCachedIds(this.cachedIds);
},
removeCachedByType(type: TEventsGroup) {
this.cachedIds[type].length = 0;
syncCachedIdsLocalStorage(this.cachedIds);
setStoredCachedIds(this.cachedIds);
},
removeCachedByIds(uuids: EventId[]) {
this.cachedIdsTypesList.forEach((type) => {
Expand All @@ -131,14 +131,14 @@ export const useEventsStore = defineStore("eventsStore", {
);
});

syncCachedIdsLocalStorage(this.cachedIds);
setStoredCachedIds(this.cachedIds);
},
removeCachedById(eventUuid: EventId) {
this.removeCachedByIds([eventUuid]);
},
removeCachedAll() {
this.cachedIds = initialCachedIds;
syncCachedIdsLocalStorage(this.cachedIds);
setStoredCachedIds(this.cachedIds);
},
syncCachedWithActive(activeIds: EventId[]) {
if (!activeIds.length) {
Expand All @@ -153,18 +153,18 @@ export const useEventsStore = defineStore("eventsStore", {
);
});

syncCachedIdsLocalStorage(this.cachedIds);
setStoredCachedIds(this.cachedIds);
},
// locked ids
removeLockedIds(eventUuid: EventId) {
this.lockedIds = this.lockedIds.filter((id) => id !== eventUuid);

syncLockedIdsLocalStorage(this.lockedIds);
setStoredLockedIds(this.lockedIds);
},
addLockedIds(eventUuid: EventId) {
this.lockedIds.push(eventUuid);

syncLockedIdsLocalStorage(this.lockedIds);
setStoredLockedIds(this.lockedIds);
}
},
});
8 changes: 4 additions & 4 deletions src/shared/stores/events/local-storage-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {TEventsCachedIdsMap} from "./types";

const { localStorage } = window;

export const getCachedIds = (): TEventsCachedIdsMap | null => {
export const getStoredCachedIds = (): TEventsCachedIdsMap | null => {
const storageValue = localStorage?.getItem(LOCAL_STORAGE_KEYS.CACHED_EVENTS);

if (storageValue) {
Expand All @@ -13,12 +13,12 @@ export const getCachedIds = (): TEventsCachedIdsMap | null => {
return null;
};

export const syncCachedIdsLocalStorage = (cachedEventMap: TEventsCachedIdsMap) => {
export const setStoredCachedIds = (cachedEventMap: TEventsCachedIdsMap) => {
localStorage?.setItem(LOCAL_STORAGE_KEYS.CACHED_EVENTS, JSON.stringify(cachedEventMap));
}


export const getLockedIds = (): EventId[] | null => {
export const getStoredLockedIds = (): EventId[] | null => {
const storageValue = localStorage?.getItem(LOCAL_STORAGE_KEYS.LOCKED_EVENTS);

if (storageValue) {
Expand All @@ -28,6 +28,6 @@ export const getLockedIds = (): EventId[] | null => {
return null;
};

export const syncLockedIdsLocalStorage = (lockedIds: EventId[]) => {
export const setStoredLockedIds = (lockedIds: EventId[]) => {
localStorage?.setItem(LOCAL_STORAGE_KEYS.LOCKED_EVENTS, JSON.stringify(lockedIds));
}
16 changes: 8 additions & 8 deletions src/shared/stores/settings/local-storage-actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {LOCAL_STORAGE_KEYS} from "../../types";
import {THEME_MODES} from "./constants";

export const checkIfThemeActive = () => {
export const getStoredActiveTheme = () => {
if (!process.client) {
return THEME_MODES.LIGHT;
}
Expand All @@ -20,7 +20,7 @@ export const checkIfThemeActive = () => {
return THEME_MODES.LIGHT
};

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

if (themeName === THEME_MODES.LIGHT) {
Expand All @@ -30,7 +30,7 @@ export const syncThemeLocalStorage = (themeName: string) => {
}
}

export const getFixedHeaderState = () => {
export const getStoredFixedHeader = () => {
if (!process.client) {
return false;
}
Expand All @@ -48,7 +48,7 @@ export const getFixedHeaderState = () => {
return isFixed;
}

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

if (state) {
Expand All @@ -58,23 +58,23 @@ export const syncFixedHeaderLocalStorage = (state: boolean) => {
}
}

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

return storageValue === "true";
};

export const syncEventsCountVisibleLocalStorage = (state: boolean) => {
export const setStoredEventsCountVisibility = (state: boolean) => {
window?.localStorage?.setItem(LOCAL_STORAGE_KEYS.EVENT_COUNTS, String(state));
}


export const getActiveCodeEditorState = (): string => {
export const getStoredPrimaryCodeEditor = (): string => {
const storedCodeEditor = window?.localStorage?.getItem(LOCAL_STORAGE_KEYS.CODE_EDITOR);

return storedCodeEditor || '';
};

export const setActiveCodeEditorState = (editor: string) => {
export const setStoredPrimaryCodeEditor = (editor: string) => {
window?.localStorage?.setItem(LOCAL_STORAGE_KEYS.CODE_EDITOR, editor);
}
28 changes: 14 additions & 14 deletions src/shared/stores/settings/settings-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {useSettings} from "../../lib/use-settings";
import type { TSettings } from "../../types";
import {THEME_MODES} from "./constants";
import {
getEventsCountVisibleState,
getFixedHeaderState,
checkIfThemeActive,
syncEventsCountVisibleLocalStorage,
syncFixedHeaderLocalStorage,
syncThemeLocalStorage, getActiveCodeEditorState, setActiveCodeEditorState,
getStoredEventsCountVisibility,
getStoredFixedHeader,
getStoredActiveTheme,
setStoredEventsCountVisibility,
setStoredFixedHeader,
setStoredActiveTheme, getStoredPrimaryCodeEditor, setStoredPrimaryCodeEditor,
} from "./local-storage-actions";

export const useSettingsStore = defineStore("settingsStore", {
Expand All @@ -18,10 +18,10 @@ export const useSettingsStore = defineStore("settingsStore", {
isEnabled: false,
loginUrl: '/login',
},
codeEditor: getActiveCodeEditorState() || 'phpstorm',
themeType: checkIfThemeActive(),
isFixedHeader: getFixedHeaderState(),
isVisibleEventCounts: getEventsCountVisibleState(),
codeEditor: getStoredPrimaryCodeEditor() || 'phpstorm',
themeType: getStoredActiveTheme(),
isFixedHeader: getStoredFixedHeader(),
isVisibleEventCounts: getStoredEventsCountVisibility(),
}),
actions: {
initialize() {
Expand All @@ -43,22 +43,22 @@ export const useSettingsStore = defineStore("settingsStore", {
? THEME_MODES.LIGHT
: THEME_MODES.DARK;

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

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

syncEventsCountVisibleLocalStorage(this.isVisibleEventCounts)
setStoredEventsCountVisibility(this.isVisibleEventCounts)
},
changeActiveCodeEditor(editor: string) {
this.codeEditor = editor;

setActiveCodeEditorState(editor);
setStoredPrimaryCodeEditor(editor);
}
},
});

0 comments on commit d2ccd16

Please sign in to comment.