2024-07-30 13:57:09 -05:00
|
|
|
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
|
|
import { CollectionIncludingMembersAndLinkCount } from "@/types/global";
|
2024-08-14 15:44:07 -05:00
|
|
|
import { useSession } from "next-auth/react";
|
2024-07-30 13:57:09 -05:00
|
|
|
|
|
|
|
const useCollections = () => {
|
2024-08-14 15:44:07 -05:00
|
|
|
const { status } = useSession();
|
|
|
|
|
2024-07-30 13:57:09 -05:00
|
|
|
return useQuery({
|
|
|
|
queryKey: ["collections"],
|
2024-07-30 22:19:29 -05:00
|
|
|
queryFn: async (): Promise<CollectionIncludingMembersAndLinkCount[]> => {
|
2024-07-30 13:57:09 -05:00
|
|
|
const response = await fetch("/api/v1/collections");
|
|
|
|
const data = await response.json();
|
2024-07-30 22:19:29 -05:00
|
|
|
return data.response;
|
2024-07-30 13:57:09 -05:00
|
|
|
},
|
2024-08-14 15:44:07 -05:00
|
|
|
enabled: status === "authenticated",
|
2024-07-30 13:57:09 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const useCreateCollection = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
return useMutation({
|
2024-07-30 22:19:29 -05:00
|
|
|
mutationFn: async (body: any) => {
|
2024-07-30 13:57:09 -05:00
|
|
|
const response = await fetch("/api/v1/collections", {
|
2024-07-30 22:19:29 -05:00
|
|
|
body: JSON.stringify(body),
|
2024-07-30 13:57:09 -05:00
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
method: "POST",
|
|
|
|
});
|
|
|
|
|
2024-07-30 22:19:29 -05:00
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
if (!response.ok) throw new Error(data.response);
|
|
|
|
|
|
|
|
return data.response;
|
2024-07-30 13:57:09 -05:00
|
|
|
},
|
|
|
|
onSuccess: (data) => {
|
|
|
|
return queryClient.setQueryData(["collections"], (oldData: any) => {
|
2024-07-30 22:19:29 -05:00
|
|
|
return [...oldData, data];
|
2024-07-30 13:57:09 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const useUpdateCollection = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
return useMutation({
|
2024-07-30 22:19:29 -05:00
|
|
|
mutationFn: async (body: any) => {
|
|
|
|
const response = await fetch(`/api/v1/collections/${body.id}`, {
|
2024-07-30 13:57:09 -05:00
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
2024-07-30 22:19:29 -05:00
|
|
|
body: JSON.stringify(body),
|
2024-07-30 13:57:09 -05:00
|
|
|
});
|
|
|
|
|
2024-07-30 22:19:29 -05:00
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
if (!response.ok) throw new Error(data.response);
|
|
|
|
|
|
|
|
return data.response;
|
2024-07-30 13:57:09 -05:00
|
|
|
},
|
|
|
|
onSuccess: (data) => {
|
|
|
|
{
|
|
|
|
return queryClient.setQueryData(["collections"], (oldData: any) => {
|
2024-07-30 22:19:29 -05:00
|
|
|
return oldData.map((collection: any) =>
|
|
|
|
collection.id === data.id ? data : collection
|
|
|
|
);
|
2024-07-30 13:57:09 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2024-07-30 22:19:29 -05:00
|
|
|
// onMutate: async (data) => {
|
|
|
|
// await queryClient.cancelQueries({ queryKey: ["collections"] });
|
|
|
|
// queryClient.setQueryData(["collections"], (oldData: any) => {
|
|
|
|
// return oldData.map((collection: any) =>
|
|
|
|
// collection.id === data.id ? data : collection
|
|
|
|
// )
|
|
|
|
// });
|
|
|
|
// },
|
2024-07-30 13:57:09 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const useDeleteCollection = () => {
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
mutationFn: async (id: number) => {
|
|
|
|
const response = await fetch(`/api/v1/collections/${id}`, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-07-30 22:19:29 -05:00
|
|
|
const data = await response.json();
|
|
|
|
|
|
|
|
if (!response.ok) throw new Error(data.response);
|
|
|
|
|
|
|
|
return data.response;
|
2024-07-30 13:57:09 -05:00
|
|
|
},
|
|
|
|
onSuccess: (data) => {
|
|
|
|
return queryClient.setQueryData(["collections"], (oldData: any) => {
|
2024-07-30 22:19:29 -05:00
|
|
|
return oldData.filter((collection: any) => collection.id !== data.id);
|
2024-07-30 13:57:09 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
useCollections,
|
|
|
|
useCreateCollection,
|
|
|
|
useUpdateCollection,
|
|
|
|
useDeleteCollection,
|
|
|
|
};
|