refactor tags store

This commit is contained in:
daniel31x13
2024-08-01 17:23:51 -04:00
parent e889509697
commit da8dc83b8f
8 changed files with 108 additions and 107 deletions
-7
View File
@@ -3,7 +3,6 @@ import {
ArchivedFormat,
LinkIncludingShortenedCollectionAndTags,
} from "@/types/global";
import useTagStore from "./tags";
type ResponseObject = {
ok: boolean;
@@ -79,7 +78,6 @@ const useLinkStore = create<LinkStore>()((set) => ({
set((state) => ({
links: [data.response, ...state.links],
}));
useTagStore.getState().setTags();
}
return { ok: response.ok, data: data.response };
@@ -154,7 +152,6 @@ const useLinkStore = create<LinkStore>()((set) => ({
...state.links,
],
}));
useTagStore.getState().setTags();
}
return { ok: response.ok, data: data.response };
@@ -209,7 +206,6 @@ const useLinkStore = create<LinkStore>()((set) => ({
e.id === data.response.id ? data.response : e
),
}));
useTagStore.getState().setTags();
}
return { ok: response.ok, data: data.response };
@@ -243,7 +239,6 @@ const useLinkStore = create<LinkStore>()((set) => ({
: e
),
}));
useTagStore.getState().setTags();
}
return { ok: response.ok, data: data.response };
@@ -262,7 +257,6 @@ const useLinkStore = create<LinkStore>()((set) => ({
set((state) => ({
links: state.links.filter((e) => e.id !== linkId),
}));
useTagStore.getState().setTags();
}
return { ok: response.ok, data: data.response };
@@ -282,7 +276,6 @@ const useLinkStore = create<LinkStore>()((set) => ({
set((state) => ({
links: state.links.filter((e) => !linkIds.includes(e.id as number)),
}));
useTagStore.getState().setTags();
}
return { ok: response.ok, data: data.response };
-62
View File
@@ -1,62 +0,0 @@
import { create } from "zustand";
import { TagIncludingLinkCount } from "@/types/global";
type ResponseObject = {
ok: boolean;
data: object | string;
};
type TagStore = {
tags: TagIncludingLinkCount[];
setTags: () => void;
updateTag: (tag: TagIncludingLinkCount) => Promise<ResponseObject>;
removeTag: (tagId: number) => Promise<ResponseObject>;
};
const useTagStore = create<TagStore>()((set) => ({
tags: [],
setTags: async () => {
const response = await fetch("/api/v1/tags");
const data = await response.json();
if (response.ok) set({ tags: data.response });
},
updateTag: async (tag) => {
const response = await fetch(`/api/v1/tags/${tag.id}`, {
body: JSON.stringify(tag),
headers: {
"Content-Type": "application/json",
},
method: "PUT",
});
const data = await response.json();
if (response.ok) {
set((state) => ({
tags: state.tags.map((e) =>
e.id === data.response.id ? data.response : e
),
}));
}
return { ok: response.ok, data: data.response };
},
removeTag: async (tagId) => {
const response = await fetch(`/api/v1/tags/${tagId}`, {
method: "DELETE",
});
if (response.ok) {
set((state) => ({
tags: state.tags.filter((e) => e.id !== tagId),
}));
}
const data = await response.json();
return { ok: response.ok, data: data.response };
},
}));
export default useTagStore;