use-toggle-watched.ts 848 B

12345678910111213141516171819202122232425262728293031323334
  1. "use client";
  2. import { useMutation, useQueryClient } from "@tanstack/react-query";
  3. interface ToggleWatchedVars {
  4. movieId: string;
  5. watched: boolean;
  6. }
  7. async function toggleWatched({ movieId, watched }: ToggleWatchedVars) {
  8. const res = await fetch(`/api/movies/${movieId}/watched`, {
  9. method: "PATCH",
  10. headers: { "Content-Type": "application/json" },
  11. body: JSON.stringify({ watched }),
  12. });
  13. if (!res.ok) {
  14. const data = await res.json().catch(() => ({}));
  15. throw new Error(data.error || "Failed to update watched status");
  16. }
  17. return res.json();
  18. }
  19. export function useToggleWatched(groupId: string) {
  20. const queryClient = useQueryClient();
  21. return useMutation({
  22. mutationFn: toggleWatched,
  23. onSuccess: () => {
  24. void queryClient.invalidateQueries({ queryKey: ["group-movies", groupId] });
  25. },
  26. });
  27. }