first commit
This commit is contained in:
32
src/pages/news/lib/api.ts
Normal file
32
src/pages/news/lib/api.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { NewsType } from "@/pages/news/lib/type";
|
||||
|
||||
const STORAGE_KEY = "news_data";
|
||||
|
||||
export const getAllNews = (): NewsType[] => {
|
||||
const data = localStorage.getItem(STORAGE_KEY);
|
||||
return data ? JSON.parse(data) : [];
|
||||
};
|
||||
|
||||
export const addNews = (news: Omit<NewsType, "id" | "createdAt">) => {
|
||||
const all = getAllNews();
|
||||
const newNews: NewsType = {
|
||||
id: "1",
|
||||
createdAt: new Date().toISOString(),
|
||||
...news,
|
||||
};
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify([newNews, ...all]));
|
||||
};
|
||||
|
||||
export const updateNews = (id: string, updated: Partial<NewsType>) => {
|
||||
const all = getAllNews().map((n) => (n.id === id ? { ...n, ...updated } : n));
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(all));
|
||||
};
|
||||
|
||||
export const deleteNews = (id: string) => {
|
||||
const filtered = getAllNews().filter((n) => n.id !== id);
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(filtered));
|
||||
};
|
||||
|
||||
export const getNewsById = (id: string) => {
|
||||
return getAllNews().find((n) => n.id === id);
|
||||
};
|
||||
Reference in New Issue
Block a user