use-delete-movie.ts 954 B

1234567891011121314151617181920212223242526272829303132333435
  1. "use client";
  2. import { useMutation, useQueryClient } from "@tanstack/react-query";
  3. import { getSupabaseBrowserClient } from "@/lib/supabase/client";
  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. return useMutation({
  17. mutationFn: deleteMovie,
  18. onSuccess: async () => {
  19. void queryClient.invalidateQueries({ queryKey: ["group-movies", groupId] });
  20. const supabase = getSupabaseBrowserClient();
  21. const {
  22. data: { user },
  23. } = await supabase.auth.getUser();
  24. if (user) {
  25. void queryClient.invalidateQueries({ queryKey: ["all-user-movies", user.id] });
  26. }
  27. },
  28. });
  29. }