2023-03-05 15:03:20 -06:00
|
|
|
import { create } from "zustand";
|
2024-08-12 23:08:57 -05:00
|
|
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
2023-03-05 15:03:20 -06:00
|
|
|
|
2023-06-26 17:33:40 -05:00
|
|
|
type ResponseObject = {
|
|
|
|
ok: boolean;
|
|
|
|
data: object | string;
|
|
|
|
};
|
|
|
|
|
2023-03-22 18:11:54 -05:00
|
|
|
type LinkStore = {
|
2024-02-10 15:53:46 -06:00
|
|
|
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) => ({
|
2024-02-09 23:24:22 -06:00
|
|
|
selectedLinks: [],
|
2024-02-10 15:53:46 -06:00
|
|
|
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) {
|
2024-08-12 23:08:57 -05:00
|
|
|
// 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;
|