changes and improvements

This commit is contained in:
daniel31x13
2024-08-14 15:22:28 -04:00
parent d15d965139
commit 9cc3a7206e
24 changed files with 292 additions and 203 deletions
-29
View File
@@ -1,7 +1,5 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
import { useTranslation } from "next-i18next";
import toast from "react-hot-toast";
const useCollections = () => {
return useQuery({
@@ -15,13 +13,10 @@ const useCollections = () => {
};
const useCreateCollection = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (body: any) => {
const load = toast.loading(t("creating"));
const response = await fetch("/api/v1/collections", {
body: JSON.stringify(body),
headers: {
@@ -30,8 +25,6 @@ const useCreateCollection = () => {
method: "POST",
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -39,25 +32,18 @@ const useCreateCollection = () => {
return data.response;
},
onSuccess: (data) => {
toast.success(t("created"));
return queryClient.setQueryData(["collections"], (oldData: any) => {
return [...oldData, data];
});
},
onError: (error) => {
toast.error(error.message);
},
});
};
const useUpdateCollection = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (body: any) => {
const load = toast.loading(t("updating_collection"));
const response = await fetch(`/api/v1/collections/${body.id}`, {
method: "PUT",
headers: {
@@ -66,8 +52,6 @@ const useUpdateCollection = () => {
body: JSON.stringify(body),
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -76,7 +60,6 @@ const useUpdateCollection = () => {
},
onSuccess: (data) => {
{
toast.success(t("updated"));
return queryClient.setQueryData(["collections"], (oldData: any) => {
return oldData.map((collection: any) =>
collection.id === data.id ? data : collection
@@ -92,20 +75,14 @@ const useUpdateCollection = () => {
// )
// });
// },
onError: (error) => {
toast.error(error.message);
},
});
};
const useDeleteCollection = () => {
const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (id: number) => {
const load = toast.loading(t("deleting_collection"));
const response = await fetch(`/api/v1/collections/${id}`, {
method: "DELETE",
headers: {
@@ -113,8 +90,6 @@ const useDeleteCollection = () => {
},
});
toast.dismiss(load);
const data = await response.json();
if (!response.ok) throw new Error(data.response);
@@ -122,14 +97,10 @@ const useDeleteCollection = () => {
return data.response;
},
onSuccess: (data) => {
toast.success(t("deleted"));
return queryClient.setQueryData(["collections"], (oldData: any) => {
return oldData.filter((collection: any) => collection.id !== data.id);
});
},
onError: (error) => {
toast.error(error.message);
},
});
};