el.xwx.moe/store/collections.ts
2023-04-27 00:10:48 +03:30

61 lines
2.1 KiB
TypeScript

// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
import { create } from "zustand";
import { Collection } from "@prisma/client";
import { ExtendedCollection, NewCollection } from "@/types/global";
type CollectionStore = {
collections: ExtendedCollection[];
setCollections: () => void;
addCollection: (body: NewCollection) => Promise<boolean>;
// updateCollection: (collection: Collection) => void;
removeCollection: (collectionId: number) => void;
};
const useCollectionStore = create<CollectionStore>()((set) => ({
collections: [],
setCollections: async () => {
const response = await fetch("/api/routes/collections");
const data = await response.json();
if (response.ok) set({ collections: data.response });
},
addCollection: async (body) => {
const response = await fetch("/api/routes/collections", {
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
},
method: "POST",
});
const data = await response.json();
console.log(data);
if (response.ok)
set((state) => ({
collections: [...state.collections, data.response],
}));
return response.ok;
},
// updateCollection: (collection) =>
// set((state) => ({
// collections: state.collections.map((c) =>
// c.id === collection.id ? collection : c
// ),
// })),
removeCollection: (collectionId) => {
set((state) => ({
collections: state.collections.filter((c) => c.id !== collectionId),
}));
},
}));
export default useCollectionStore;