Skip to content

Commit

Permalink
Add zutand persist middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
n1klaus committed May 14, 2023
1 parent 1e4f640 commit 9a7f9cd
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions frontend/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { create } from 'zustand';
import { persist, createJSONStorage } from 'zustand/middleware';
import { IUser, IVideo, IChannel } from '../utils/types';

type Store = {
Expand All @@ -12,15 +13,23 @@ type Store = {
setRequestLoading: (isLoading: boolean) => void;
};

const useStore = create<Store>((set) => ({
authUser: null,
currentVideo: null,
currentChannel: null,
requestLoading: false,
setAuthUser: (user) => set((state) => ({ ...state, authUser: user })),
setCurrentVideo: (video) => set((state) => ({ ...state, currentVideo: video })),
setCurrentChannel: (channel) => set((state) => ({ ...state, currentChannel: channel })),
setRequestLoading: (isLoading) => set((state) => ({ ...state, requestLoading: isLoading })),
}));
const useStore = create<Store, any>(
persist(
(set) => ({
authUser: null,
currentVideo: null,
currentChannel: null,
requestLoading: false,
setAuthUser: (user) => set((state) => ({ ...state, authUser: user })),
setCurrentVideo: (video) => set((state) => ({ ...state, currentVideo: video })),
setCurrentChannel: (channel) => set((state) => ({ ...state, currentChannel: channel })),
setRequestLoading: (isLoading) => set((state) => ({ ...state, requestLoading: isLoading })),
}),
{
name: 'authUser',
storage: createJSONStorage(() => sessionStorage),
},
),
);

export default useStore;

0 comments on commit 9a7f9cd

Please sign in to comment.