fix: apply bugfix agent improvements to src/store/contentStore.ts
This commit is contained in:
parent
9088b06ec5
commit
88730e43e2
|
|
@ -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:
|
||||||
|
"<p>This is your first post. You can edit or delete it, and start creating your own content!</p>",
|
||||||
|
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:
|
||||||
|
"<p>Learn how to create engaging content using the built-in rich text editor.</p>",
|
||||||
|
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<ContentState>()(
|
||||||
|
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<Post>) => {
|
||||||
|
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",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
Loading…
Reference in New Issue