el.xwx.moe/store/collections.ts

96 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-04-23 08:26:39 -05:00
// 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/>.
2023-02-18 21:32:02 -06:00
import { create } from "zustand";
import { ExtendedCollection, NewCollection } from "@/types/global";
import useTagStore from "./tags";
2023-04-28 16:17:04 -05:00
import useLinkStore from "./links";
2023-02-18 21:32:02 -06:00
2023-03-22 18:11:54 -05:00
type CollectionStore = {
collections: ExtendedCollection[];
2023-02-18 21:32:02 -06:00
setCollections: () => void;
2023-04-24 16:30:40 -05:00
addCollection: (body: NewCollection) => Promise<boolean>;
updateCollection: (collection: ExtendedCollection) => Promise<boolean>;
removeCollection: (collectionId: number) => Promise<boolean>;
2023-02-18 21:32:02 -06:00
};
2023-03-22 18:11:54 -05:00
const useCollectionStore = create<CollectionStore>()((set) => ({
2023-02-18 21:32:02 -06:00
collections: [],
setCollections: async () => {
const response = await fetch("/api/routes/collections");
const data = await response.json();
2023-05-25 13:44:08 -05:00
console.log(data);
2023-02-18 21:32:02 -06:00
if (response.ok) set({ collections: data.response });
},
2023-04-24 16:30:40 -05:00
addCollection: async (body) => {
2023-02-18 21:32:02 -06:00
const response = await fetch("/api/routes/collections", {
2023-04-24 16:30:40 -05:00
body: JSON.stringify(body),
2023-02-18 21:32:02 -06:00
headers: {
"Content-Type": "application/json",
},
method: "POST",
});
const data = await response.json();
if (response.ok)
set((state) => ({
collections: [...state.collections, data.response],
}));
2023-04-24 16:30:40 -05:00
return response.ok;
2023-02-18 21:32:02 -06:00
},
updateCollection: async (collection) => {
const response = await fetch("/api/routes/collections", {
body: JSON.stringify(collection),
headers: {
"Content-Type": "application/json",
},
method: "PUT",
});
const data = await response.json();
console.log(data);
if (response.ok)
set((state) => ({
collections: state.collections.map((e) =>
2023-05-01 15:00:23 -05:00
e.id === data.response.id ? data.response : e
),
}));
return response.ok;
},
removeCollection: async (id) => {
const response = await fetch("/api/routes/collections", {
body: JSON.stringify({ id }),
headers: {
"Content-Type": "application/json",
},
method: "DELETE",
});
const data = await response.json();
console.log(data);
if (response.ok) {
set((state) => ({
collections: state.collections.filter((e) => e.id !== id),
}));
useTagStore.getState().setTags();
2023-04-28 16:17:04 -05:00
useLinkStore.getState().setLinks();
}
return response.ok;
2023-02-18 21:32:02 -06:00
},
}));
2023-03-22 18:11:54 -05:00
export default useCollectionStore;