Skip to content

Commit

Permalink
refactor: persisting data storage is implemented using zustand middle…
Browse files Browse the repository at this point in the history
…ware persist
  • Loading branch information
yosong-github committed Oct 22, 2024
1 parent 38e7427 commit ef2b433
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 39 deletions.
47 changes: 27 additions & 20 deletions src/store/settingStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { create } from 'zustand';

import { getItem, removeItem, setItem } from '@/utils/storage';
import { persist, createJSONStorage } from 'zustand/middleware';

import { StorageEnum, ThemeColorPresets, ThemeLayout, ThemeMode } from '#/enum';

Expand All @@ -21,25 +20,33 @@ type SettingStore = {
};
};

const useSettingStore = create<SettingStore>((set) => ({
settings: getItem<SettingsType>(StorageEnum.Settings) || {
themeColorPresets: ThemeColorPresets.Default,
themeMode: ThemeMode.Light,
themeLayout: ThemeLayout.Vertical,
themeStretch: false,
breadCrumb: true,
multiTab: true,
},
actions: {
setSettings: (settings) => {
set({ settings });
setItem(StorageEnum.Settings, settings);
},
clearSettings() {
removeItem(StorageEnum.Settings);
const useSettingStore = create<SettingStore>()(
persist(
(set) => ({
settings: {
themeColorPresets: ThemeColorPresets.Default,
themeMode: ThemeMode.Light,
themeLayout: ThemeLayout.Vertical,
themeStretch: false,
breadCrumb: true,
multiTab: true,
},
actions: {
setSettings: (settings) => {
set({ settings });
},
clearSettings() {
useSettingStore.persist.clearStorage();
},
},
}),
{
name: StorageEnum.Settings, // name of the item in the storage (must be unique)
storage: createJSONStorage(() => localStorage), // (optional) by default, 'localStorage' is used
partialize: (state) => ({ settings: state.settings }),
},
},
}));
),
);

export const useSettings = () => useSettingStore((state) => state.settings);
export const useSettingActions = () => useSettingStore((state) => state.actions);
46 changes: 27 additions & 19 deletions src/store/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { useMutation } from '@tanstack/react-query';
import { App } from 'antd';
import { useNavigate } from 'react-router-dom';
import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';

import userService, { SignInReq } from '@/api/services/userService';
import { getItem, removeItem, setItem } from '@/utils/storage';

import { UserInfo, UserToken } from '#/entity';
import { StorageEnum } from '#/enum';
Expand All @@ -22,25 +22,33 @@ type UserStore = {
};
};

const useUserStore = create<UserStore>((set) => ({
userInfo: getItem<UserInfo>(StorageEnum.User) || {},
userToken: getItem<UserToken>(StorageEnum.Token) || {},
actions: {
setUserInfo: (userInfo) => {
set({ userInfo });
setItem(StorageEnum.User, userInfo);
},
setUserToken: (userToken) => {
set({ userToken });
setItem(StorageEnum.Token, userToken);
},
clearUserInfoAndToken() {
set({ userInfo: {}, userToken: {} });
removeItem(StorageEnum.User);
removeItem(StorageEnum.Token);
const useUserStore = create<UserStore>()(
persist(
(set) => ({
userInfo: {},
userToken: {},
actions: {
setUserInfo: (userInfo) => {
set({ userInfo });
},
setUserToken: (userToken) => {
set({ userToken });
},
clearUserInfoAndToken() {
set({ userInfo: {}, userToken: {} });
},
},
}),
{
name: 'userStore', // name of the item in the storage (must be unique)
storage: createJSONStorage(() => localStorage), // (optional) by default, 'localStorage' is used
partialize: (state) => ({
[StorageEnum.User]: state.userInfo,
[StorageEnum.Token]: state.userToken,
}),
},
},
}));
),
);

export const useUserInfo = () => useUserStore((state) => state.userInfo);
export const useUserToken = () => useUserStore((state) => state.userToken);
Expand Down

0 comments on commit ef2b433

Please sign in to comment.