use-delete-movie.ts 894 B

12345678910111213141516171819202122232425262728293031323334
  1. "use client";
  2. import { useMutation, useQueryClient } from "@tanstack/react-query";
  3. import { useCurrentUser } from "@/hooks/use-current-user";
  4. async function deleteMovie(movieId: string) {
  5. const res = await fetch(`/api/movies/${movieId}`, {
  6. method: "DELETE",
  7. });
  8. if (!res.ok) {
  9. const data = await res.json().catch(() => ({}));
  10. throw new Error(data.error || "Failed to delete movie");
  11. }
  12. return res.json();
  13. }
  14. export function useDeleteMovie(groupId: string) {
  15. const queryClient = useQueryClient();
  16. const { data: currentUser } = useCurrentUser();
  17. return useMutation({
  18. mutationFn: deleteMovie,
  19. onSuccess: () => {
  20. void queryClient.invalidateQueries({ queryKey: ["group-movies", groupId] });
  21. if (currentUser) {
  22. void queryClient.invalidateQueries({
  23. queryKey: ["all-user-movies", currentUser.id],
  24. });
  25. }
  26. },
  27. });
  28. }