refactor tags store
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user