Backend: Added PUT /seasons/:id/refresh endpoint that fetches updated TMDB data for a single season. Frontend: - New SeasonRatingDialog for rating seasons via StarRating - useRefreshSeason and useUpdateSeason hooks with query invalidation - SeasonPage now shows inline season rating (user + TMDB) - Mark Watched, Rate, and Refresh actions moved under a dropdown menu
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { seasonsApi } from "@/api/seasons";
|
|
import { toast } from "sonner";
|
|
|
|
export const seasonKeys = {
|
|
all: ["seasons"] as const,
|
|
detail: (id: number) => ["seasons", id] as const,
|
|
episodes: (id: number) => ["seasons", id, "episodes"] as const,
|
|
};
|
|
|
|
export function useSeason(id: number) {
|
|
return useQuery({
|
|
queryKey: seasonKeys.detail(id),
|
|
queryFn: () => seasonsApi.getById(id),
|
|
enabled: !!id,
|
|
});
|
|
}
|
|
|
|
export function useSeasonEpisodes(id: number) {
|
|
return useQuery({
|
|
queryKey: seasonKeys.episodes(id),
|
|
queryFn: () => seasonsApi.getEpisodes(id),
|
|
enabled: !!id,
|
|
});
|
|
}
|
|
|
|
export function useRefreshSeason() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (id: number) => seasonsApi.refresh(id),
|
|
onSuccess: (season) => {
|
|
queryClient.invalidateQueries({ queryKey: seasonKeys.detail(season.id) });
|
|
queryClient.invalidateQueries({
|
|
queryKey: seasonKeys.episodes(season.id),
|
|
});
|
|
toast.success("Season refreshed");
|
|
},
|
|
onError: () => {
|
|
toast.error("Failed to refresh season");
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateSeason() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
id,
|
|
data,
|
|
}: {
|
|
id: number;
|
|
data: Partial<Parameters<typeof seasonsApi.update>[1]>;
|
|
}) => seasonsApi.update(id, data),
|
|
onSuccess: (season) => {
|
|
queryClient.invalidateQueries({ queryKey: seasonKeys.detail(season.id) });
|
|
toast.success("Season updated");
|
|
},
|
|
onError: () => {
|
|
toast.error("Failed to update season");
|
|
},
|
|
});
|
|
}
|