| 12345678910111213141516171819202122232425262728293031323334 |
- "use client";
- import { useMutation, useQueryClient } from "@tanstack/react-query";
- interface ToggleWatchedVars {
- movieId: string;
- watched: boolean;
- }
- async function toggleWatched({ movieId, watched }: ToggleWatchedVars) {
- const res = await fetch(`/api/movies/${movieId}/watched`, {
- method: "PATCH",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ watched }),
- });
- if (!res.ok) {
- const data = await res.json().catch(() => ({}));
- throw new Error(data.error || "Failed to update watched status");
- }
- return res.json();
- }
- export function useToggleWatched(groupId: string) {
- const queryClient = useQueryClient();
- return useMutation({
- mutationFn: toggleWatched,
- onSuccess: () => {
- void queryClient.invalidateQueries({ queryKey: ["group-movies", groupId] });
- },
- });
- }
|