Skip to content

Commit

Permalink
feat(client): added zustand store for posts and categories
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoSkorJjj committed Jan 4, 2024
1 parent e83a788 commit 475c200
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
17 changes: 17 additions & 0 deletions client/src/shared/store/CategoryStore.tsx
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",
},
),
);
32 changes: 32 additions & 0 deletions client/src/shared/store/PostStore.tsx
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",
},
),
);
3 changes: 3 additions & 0 deletions client/src/shared/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./AuthStore";
export * from "./CategoryStore";
export * from "./PostStore";
6 changes: 6 additions & 0 deletions client/src/shared/types/CategoryStateType.ts
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;
}
9 changes: 9 additions & 0 deletions client/src/shared/types/PostStateType.ts
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;
}
2 changes: 2 additions & 0 deletions client/src/shared/types/index.ts
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";

0 comments on commit 475c200

Please sign in to comment.