diff --git a/src/store/contentStore.ts b/src/store/contentStore.ts new file mode 100644 index 0000000..f833910 --- /dev/null +++ b/src/store/contentStore.ts @@ -0,0 +1,75 @@ +import { create } from "zustand"; +import { persist } from "zustand/middleware"; +import { v4 as uuidv4 } from "uuid"; +import type { ContentState, Post } from "@/types"; + +const SAMPLE_POSTS: Post[] = [ + { + id: "1", + title: "Welcome to Personal CMS", + slug: "welcome-to-personal-cms", + content: + "

This is your first post. You can edit or delete it, and start creating your own content!

", + excerpt: "A quick introduction to your new CMS.", + status: "published", + tags: ["welcome", "cms"], + views: 42, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + }, + { + id: "2", + title: "Getting Started with Content Creation", + slug: "getting-started-content-creation", + content: + "

Learn how to create engaging content using the built-in rich text editor.

", + excerpt: "Tips and tricks for creating great content.", + status: "draft", + tags: ["guide", "content"], + views: 0, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + }, +]; + +export const useContentStore = create()( + persist( + (set, get) => ({ + posts: SAMPLE_POSTS, + + addPost: (postData): Post => { + const now = new Date().toISOString(); + const post: Post = { + ...postData, + id: uuidv4(), + views: 0, + createdAt: now, + updatedAt: now, + }; + set((state) => ({ posts: [post, ...state.posts] })); + return post; + }, + + updatePost: (id: string, data: Partial) => { + set((state) => ({ + posts: state.posts.map((p) => + p.id === id ? { ...p, ...data, updatedAt: new Date().toISOString() } : p + ), + })); + }, + + deletePost: (id: string) => { + set((state) => ({ + posts: state.posts.filter((p) => p.id !== id), + })); + }, + + getPostById: (id: string): Post | undefined => { + return get().posts.find((p) => p.id === id); + }, + }), + { + name: "cms-content", + } + ) +);