refactored/cleaned up API + added support for renaming tags

This commit is contained in:
daniel31x13
2023-10-23 00:28:39 -04:00
parent 24cced9dba
commit ed24685aaf
48 changed files with 603 additions and 305 deletions
+3 -3
View File
@@ -15,16 +15,16 @@ type AccountStore = {
const useAccountStore = create<AccountStore>()((set) => ({
account: {} as AccountSettings,
setAccount: async (id) => {
const response = await fetch(`/api/users?id=${id}`);
const response = await fetch(`/api/v1/users/${id}`);
const data = await response.json();
const profilePic = `/api/avatar/${data.response.id}?${Date.now()}`;
const profilePic = `/api/v1/avatar/${data.response.id}?${Date.now()}`;
if (response.ok) set({ account: { ...data.response, profilePic } });
},
updateAccount: async (user) => {
const response = await fetch("/api/users", {
const response = await fetch(`/api/v1/users/${user.id}`, {
method: "PUT",
body: JSON.stringify(user),
headers: {
+6 -7
View File
@@ -22,14 +22,14 @@ type CollectionStore = {
const useCollectionStore = create<CollectionStore>()((set) => ({
collections: [],
setCollections: async () => {
const response = await fetch("/api/collections");
const response = await fetch("/api/v1/collections");
const data = await response.json();
if (response.ok) set({ collections: data.response });
},
addCollection: async (body) => {
const response = await fetch("/api/collections", {
const response = await fetch("/api/v1/collections", {
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
@@ -47,7 +47,7 @@ const useCollectionStore = create<CollectionStore>()((set) => ({
return { ok: response.ok, data: data.response };
},
updateCollection: async (collection) => {
const response = await fetch("/api/collections", {
const response = await fetch(`/api/v1/collections/${collection.id}`, {
body: JSON.stringify(collection),
headers: {
"Content-Type": "application/json",
@@ -66,9 +66,8 @@ const useCollectionStore = create<CollectionStore>()((set) => ({
return { ok: response.ok, data: data.response };
},
removeCollection: async (id) => {
const response = await fetch("/api/collections", {
body: JSON.stringify({ id }),
removeCollection: async (collectionId) => {
const response = await fetch(`/api/v1/collections/${collectionId}`, {
headers: {
"Content-Type": "application/json",
},
@@ -79,7 +78,7 @@ const useCollectionStore = create<CollectionStore>()((set) => ({
if (response.ok) {
set((state) => ({
collections: state.collections.filter((e) => e.id !== id),
collections: state.collections.filter((e) => e.id !== collectionId),
}));
useTagStore.getState().setTags();
}
+6 -9
View File
@@ -20,9 +20,7 @@ type LinkStore = {
updateLink: (
link: LinkIncludingShortenedCollectionAndTags
) => Promise<ResponseObject>;
removeLink: (
link: LinkIncludingShortenedCollectionAndTags
) => Promise<ResponseObject>;
removeLink: (linkId: number) => Promise<ResponseObject>;
resetLinks: () => void;
};
@@ -38,7 +36,7 @@ const useLinkStore = create<LinkStore>()((set) => ({
}));
},
addLink: async (body) => {
const response = await fetch("/api/links", {
const response = await fetch("/api/v1/links", {
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
@@ -59,7 +57,7 @@ const useLinkStore = create<LinkStore>()((set) => ({
return { ok: response.ok, data: data.response };
},
updateLink: async (link) => {
const response = await fetch("/api/links", {
const response = await fetch(`/api/v1/links/${link.id}`, {
body: JSON.stringify(link),
headers: {
"Content-Type": "application/json",
@@ -81,9 +79,8 @@ const useLinkStore = create<LinkStore>()((set) => ({
return { ok: response.ok, data: data.response };
},
removeLink: async (link) => {
const response = await fetch("/api/links", {
body: JSON.stringify(link),
removeLink: async (linkId) => {
const response = await fetch(`/api/v1/links/${linkId}`, {
headers: {
"Content-Type": "application/json",
},
@@ -94,7 +91,7 @@ const useLinkStore = create<LinkStore>()((set) => ({
if (response.ok) {
set((state) => ({
links: state.links.filter((e) => e.id !== link.id),
links: state.links.filter((e) => e.id !== linkId),
}));
useTagStore.getState().setTags();
}
+28 -1
View File
@@ -1,20 +1,47 @@
import { create } from "zustand";
import { Tag } from "@prisma/client";
type ResponseObject = {
ok: boolean;
data: object | string;
};
type TagStore = {
tags: Tag[];
setTags: () => void;
updateTag: (tag: Tag) => Promise<ResponseObject>;
};
const useTagStore = create<TagStore>()((set) => ({
tags: [],
setTags: async () => {
const response = await fetch("/api/tags");
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 };
},
}));
export default useTagStore;