-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): added zustand store for posts and categories
- Loading branch information
1 parent
e83a788
commit 475c200
Showing
6 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { create } from "zustand"; | ||
import { persist } from "zustand/middleware"; | ||
|
||
import { CategoryStateType } from "~shared/types"; | ||
|
||
export const useCategoryStore = create<CategoryStateType>()( | ||
persist( | ||
(set) => ({ | ||
categories: [], | ||
|
||
setCategories: (newCategories) => set({ categories: newCategories }), | ||
}), | ||
{ | ||
name: "category-storage", | ||
}, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { create } from "zustand"; | ||
import { persist } from "zustand/middleware"; | ||
|
||
import { PostState } from "~shared/types"; | ||
|
||
export const usePostStore = create<PostState>()( | ||
persist( | ||
(set) => ({ | ||
posts: [], | ||
|
||
addPost: (newPost) => | ||
set((state) => ({ posts: [...state.posts, newPost] })), | ||
|
||
removePost: (id) => | ||
set((state) => ({ | ||
posts: state.posts.filter((post) => post.id !== id), | ||
})), | ||
|
||
updatePost: (updatedPost) => | ||
set((state) => ({ | ||
posts: state.posts.map((post) => | ||
post.id === updatedPost.id ? updatedPost : post, | ||
), | ||
})), | ||
|
||
setPosts: (newPosts) => set({ posts: newPosts }), | ||
}), | ||
{ | ||
name: "post-storage", | ||
}, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./AuthStore"; | ||
export * from "./CategoryStore"; | ||
export * from "./PostStore"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { ICategory } from "~features/interfaces"; | ||
|
||
export interface CategoryStateType { | ||
categories: ICategory[]; | ||
setCategories: (categories: ICategory[]) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { IPost } from "~features/interfaces"; | ||
|
||
export interface PostState { | ||
posts: IPost[]; | ||
addPost: (newPost: IPost) => void; | ||
removePost: (id: number) => void; | ||
updatePost: (updatedPost: IPost) => void; | ||
setPosts: (newPosts: IPost[]) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export * from "./AuthContextType"; | ||
export * from "./AuthStateType"; | ||
export * from "./CategoryStateType"; | ||
export * from "./PostStateType"; | ||
export * from "./UserData"; | ||
export * from "./UserRole"; | ||
export * from "./ZustandStorage"; |