2023-03-05 15:03:20 -06:00
|
|
|
import { create } from "zustand";
|
2023-06-14 17:34:54 -05:00
|
|
|
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
|
2023-03-25 09:17:34 -05:00
|
|
|
import useTagStore from "./tags";
|
|
|
|
import useCollectionStore from "./collections";
|
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 = {
|
2023-06-14 17:34:54 -05:00
|
|
|
links: LinkIncludingShortenedCollectionAndTags[];
|
|
|
|
setLinks: (
|
|
|
|
data: LinkIncludingShortenedCollectionAndTags[],
|
|
|
|
isInitialCall: boolean
|
|
|
|
) => void;
|
2023-06-26 17:33:40 -05:00
|
|
|
addLink: (
|
|
|
|
body: LinkIncludingShortenedCollectionAndTags
|
|
|
|
) => Promise<ResponseObject>;
|
2023-06-14 17:34:54 -05:00
|
|
|
updateLink: (
|
|
|
|
link: LinkIncludingShortenedCollectionAndTags
|
2023-06-26 17:33:40 -05:00
|
|
|
) => Promise<ResponseObject>;
|
2023-10-22 23:28:39 -05:00
|
|
|
removeLink: (linkId: number) => Promise<ResponseObject>;
|
2023-06-14 17:34:54 -05:00
|
|
|
resetLinks: () => void;
|
2023-03-05 15:03:20 -06:00
|
|
|
};
|
|
|
|
|
2023-03-22 18:11:54 -05:00
|
|
|
const useLinkStore = create<LinkStore>()((set) => ({
|
2023-03-05 15:03:20 -06:00
|
|
|
links: [],
|
2023-06-14 17:34:54 -05:00
|
|
|
setLinks: async (data, isInitialCall) => {
|
|
|
|
isInitialCall &&
|
|
|
|
set(() => ({
|
|
|
|
links: [],
|
|
|
|
}));
|
|
|
|
set((state) => ({
|
|
|
|
links: [...state.links, ...data],
|
|
|
|
}));
|
2023-03-05 15:03:20 -06:00
|
|
|
},
|
2023-04-24 16:30:40 -05:00
|
|
|
addLink: async (body) => {
|
2023-10-22 23:28:39 -05:00
|
|
|
const response = await fetch("/api/v1/links", {
|
2023-04-24 16:30:40 -05:00
|
|
|
body: JSON.stringify(body),
|
2023-03-05 15:03:20 -06:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
2023-03-28 10:11:34 -05:00
|
|
|
if (response.ok) {
|
2023-03-05 15:03:20 -06:00
|
|
|
set((state) => ({
|
2023-06-14 17:34:54 -05:00
|
|
|
links: [data.response, ...state.links],
|
2023-03-05 15:03:20 -06:00
|
|
|
}));
|
2023-03-28 10:11:34 -05:00
|
|
|
useTagStore.getState().setTags();
|
|
|
|
useCollectionStore.getState().setCollections();
|
|
|
|
}
|
2023-03-25 09:17:34 -05:00
|
|
|
|
2023-06-26 17:33:40 -05:00
|
|
|
return { ok: response.ok, data: data.response };
|
2023-03-05 15:03:20 -06:00
|
|
|
},
|
2023-03-28 10:11:34 -05:00
|
|
|
updateLink: async (link) => {
|
2023-10-22 23:28:39 -05:00
|
|
|
const response = await fetch(`/api/v1/links/${link.id}`, {
|
2023-03-28 10:11:34 -05:00
|
|
|
body: JSON.stringify(link),
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
method: "PUT",
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
set((state) => ({
|
2023-05-01 05:07:01 -05:00
|
|
|
links: state.links.map((e) =>
|
|
|
|
e.id === data.response.id ? data.response : e
|
|
|
|
),
|
2023-03-28 10:11:34 -05:00
|
|
|
}));
|
|
|
|
useTagStore.getState().setTags();
|
2023-05-01 05:07:01 -05:00
|
|
|
useCollectionStore.getState().setCollections();
|
2023-03-28 10:11:34 -05:00
|
|
|
}
|
2023-05-27 12:49:09 -05:00
|
|
|
|
2023-06-26 17:33:40 -05:00
|
|
|
return { ok: response.ok, data: data.response };
|
2023-03-28 10:11:34 -05:00
|
|
|
},
|
2023-10-22 23:28:39 -05:00
|
|
|
removeLink: async (linkId) => {
|
|
|
|
const response = await fetch(`/api/v1/links/${linkId}`, {
|
2023-03-23 10:25:17 -05:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
method: "DELETE",
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
2023-03-28 10:11:34 -05:00
|
|
|
if (response.ok) {
|
2023-03-23 10:25:17 -05:00
|
|
|
set((state) => ({
|
2023-10-22 23:28:39 -05:00
|
|
|
links: state.links.filter((e) => e.id !== linkId),
|
2023-03-23 10:25:17 -05:00
|
|
|
}));
|
2023-03-28 10:11:34 -05:00
|
|
|
useTagStore.getState().setTags();
|
|
|
|
}
|
2023-03-23 10:25:17 -05:00
|
|
|
|
2023-06-26 17:33:40 -05:00
|
|
|
return { ok: response.ok, data: data.response };
|
2023-03-05 15:03:20 -06:00
|
|
|
},
|
2023-06-14 17:34:54 -05:00
|
|
|
resetLinks: () => set({ links: [] }),
|
2023-03-05 15:03:20 -06:00
|
|
|
}));
|
|
|
|
|
2023-03-22 18:11:54 -05:00
|
|
|
export default useLinkStore;
|