el.xwx.moe/store/links.ts

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-03-05 15:03:20 -06:00
import { create } from "zustand";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
2023-03-05 15:03:20 -06:00
type ResponseObject = {
ok: boolean;
data: object | string;
};
2023-03-22 18:11:54 -05:00
type LinkStore = {
selectedLinks: LinkIncludingShortenedCollectionAndTags[];
setSelectedLinks: (links: LinkIncludingShortenedCollectionAndTags[]) => void;
2024-02-10 18:34:25 -06:00
updateLinks: (
links: LinkIncludingShortenedCollectionAndTags[],
2024-02-11 01:21:25 -06:00
removePreviousTags: boolean,
2024-02-10 18:34:25 -06:00
newData: Pick<
LinkIncludingShortenedCollectionAndTags,
"tags" | "collectionId"
>
) => Promise<ResponseObject>;
2023-03-05 15:03:20 -06:00
};
2023-03-22 18:11:54 -05:00
const useLinkStore = create<LinkStore>()((set) => ({
selectedLinks: [],
setSelectedLinks: (links) => set({ selectedLinks: links }),
2024-02-11 01:21:25 -06:00
updateLinks: async (links, removePreviousTags, newData) => {
2024-02-10 16:23:59 -06:00
const response = await fetch("/api/v1/links", {
2024-02-11 01:21:25 -06:00
body: JSON.stringify({ links, removePreviousTags, newData }),
2024-02-10 16:23:59 -06:00
headers: {
"Content-Type": "application/json",
},
method: "PUT",
});
const data = await response.json();
2024-02-10 01:38:19 -06:00
2024-02-10 16:23:59 -06:00
if (response.ok) {
// Update the selected links with the new data
2024-02-10 00:37:48 -06:00
}
return { ok: response.ok, data: data.response };
},
2023-03-05 15:03:20 -06:00
}));
2023-03-22 18:11:54 -05:00
export default useLinkStore;