diff --git a/backend/controllers/seasonController.js b/backend/controllers/seasonController.js index 7617e37..077972e 100644 --- a/backend/controllers/seasonController.js +++ b/backend/controllers/seasonController.js @@ -1,6 +1,9 @@ const Episode = require("../models/Episode"); const EpisodeSeen = require("../models/EpisodeSeen"); const Season = require("../models/Season"); +const Entry = require("../models/Entry"); +const EntryId = require("../models/EntryId"); +const { fetchSeasonData } = require("../services/tmdbService"); class SeasonController { constructor() { @@ -88,6 +91,48 @@ class SeasonController { } } + async refreshSeason(req, res) { + try { + const season = await Season.findByPk(req.params.id, { + include: [ + { + model: Entry, + as: "entry", + include: [{ model: EntryId, as: "entryId" }], + }, + ], + }); + + if (!season) { + return res.status(404).send("Season not found"); + } + + const tmdbTvId = season.entry?.entryId?.tmdb_id; + if (!tmdbTvId) { + return res.status(400).send("Entry has no TMDB ID"); + } + + const seasonData = await fetchSeasonData(tmdbTvId, season.season_number); + if (!seasonData) { + return res.status(500).send("Couldn't fetch data from TMDB"); + } + + await season.update({ + name: seasonData.name, + overview: seasonData.overview, + episode_count: seasonData.episodes?.length ?? season.episode_count, + air_date: seasonData.air_date, + tmdb_rating: seasonData.vote_average, + poster_path: seasonData.poster_path, + }); + + const refreshedSeason = await Season.findByPk(req.params.id); + res.send(refreshedSeason); + } catch (error) { + res.status(500).send(error.message); + } + } + async deleteSeason(req, res) { try { const deleted = await Season.destroy({ diff --git a/backend/routes/season.routes.js b/backend/routes/season.routes.js index 626412e..31a1104 100644 --- a/backend/routes/season.routes.js +++ b/backend/routes/season.routes.js @@ -19,6 +19,9 @@ seasonRouter.get("/:id/episodes", seasonController.getSeasonEpisodes); // * PUT - Update a season seasonRouter.put("/:id", seasonController.updateSeason); +// * PUT - Refresh a season from TMDB +seasonRouter.put("/:id/refresh", seasonController.refreshSeason); + // * DELETE - Delete a season seasonRouter.delete("/:id", seasonController.deleteSeason); diff --git a/react_frontend/src/api/seasons.ts b/react_frontend/src/api/seasons.ts index 0d098a9..cd4166c 100644 --- a/react_frontend/src/api/seasons.ts +++ b/react_frontend/src/api/seasons.ts @@ -17,4 +17,7 @@ export const seasonsApi = { api.put(`/seasons/${id}`, data).then((r) => r.data), delete: (id: number) => api.delete(`/seasons/${id}`).then((r) => r.data), + + refresh: (id: number) => + api.put(`/seasons/${id}/refresh`).then((r) => r.data), }; diff --git a/react_frontend/src/components/dialogs/SeasonRatingDialog.tsx b/react_frontend/src/components/dialogs/SeasonRatingDialog.tsx new file mode 100644 index 0000000..3d81df2 --- /dev/null +++ b/react_frontend/src/components/dialogs/SeasonRatingDialog.tsx @@ -0,0 +1,76 @@ +import { useState, useEffect } from "react"; +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { Button } from "@/components/ui/button"; +import { StarRating } from "@/components/ui/star-rating"; +import { useUpdateSeason } from "@/hooks/useSeasons"; +import type { Season } from "@/types"; + +interface SeasonRatingDialogProps { + open: boolean; + onOpenChange: (open: boolean) => void; + season: Season; +} + +export function SeasonRatingDialog({ + open, + onOpenChange, + season, +}: SeasonRatingDialogProps) { + const currentRating = season.user_rating ?? 0; + const [selected, setSelected] = useState(currentRating); + const updateSeason = useUpdateSeason(); + + useEffect(() => { + if (open) setSelected(season.user_rating ?? 0); + }, [open, season.user_rating]); + + const handleRate = (rating: number) => { + setSelected(rating); + updateSeason.mutate({ + id: season.id, + data: { user_rating: rating || null }, + }); + onOpenChange(false); + }; + + const handleClear = () => { + setSelected(0); + updateSeason.mutate({ + id: season.id, + data: { user_rating: null }, + }); + onOpenChange(false); + }; + + return ( + + + + Rate Season +

+ {season.name} +

+
+ +
+ + +

+ {selected > 0 ? `${selected} / 10` : "—"} +

+ + {(selected > 0 || currentRating > 0) && ( + + )} +
+
+
+ ); +} diff --git a/react_frontend/src/hooks/useSeasons.ts b/react_frontend/src/hooks/useSeasons.ts index 1dceb2a..5af6d87 100644 --- a/react_frontend/src/hooks/useSeasons.ts +++ b/react_frontend/src/hooks/useSeasons.ts @@ -1,5 +1,6 @@ -import { useQuery } from "@tanstack/react-query"; +import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { seasonsApi } from "@/api/seasons"; +import { toast } from "sonner"; export const seasonKeys = { all: ["seasons"] as const, @@ -22,3 +23,42 @@ export function useSeasonEpisodes(id: number) { 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[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"); + }, + }); +} diff --git a/react_frontend/src/pages/SeasonPage.tsx b/react_frontend/src/pages/SeasonPage.tsx index 8d7aa83..25de4d9 100644 --- a/react_frontend/src/pages/SeasonPage.tsx +++ b/react_frontend/src/pages/SeasonPage.tsx @@ -1,16 +1,36 @@ import { useState } from "react"; import { useParams, Link } from "react-router-dom"; -import { useSeason, useSeasonEpisodes } from "@/hooks/useSeasons"; +import { + useSeason, + useSeasonEpisodes, + useRefreshSeason, +} from "@/hooks/useSeasons"; import { useEntry, useEntrySeasons } from "@/hooks/useEntries"; import { useLibraryStore } from "@/stores/useLibraryStore"; import { EpisodeSeenEditDialog } from "@/components/dialogs/EpisodeSeenEditDialog"; import { MarkSeasonWatchedDialog } from "@/components/dialogs/MarkSeasonWatchedDialog"; +import { SeasonRatingDialog } from "@/components/dialogs/SeasonRatingDialog"; import { StreamingDialog } from "@/components/streaming/StreamingDialog"; import { useStreamingStore } from "@/stores/useStreamingStore"; import { Skeleton } from "@/components/ui/skeleton"; import { Progress } from "@/components/ui/progress"; import { Button } from "@/components/ui/button"; -import { Eye, CheckCircle2, ListChecks, PlayCircle } from "lucide-react"; +import { StarRating } from "@/components/ui/star-rating"; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from "@/components/ui/dropdown-menu"; +import { + Eye, + CheckCircle2, + ListChecks, + PlayCircle, + Ellipsis, + RefreshCw, + Star, +} from "lucide-react"; import { Tooltip, TooltipContent, @@ -33,8 +53,10 @@ export function SeasonPage() { entry?.title ?? useLibraryStore.getState().entries.get(parseInt(entryId!, 10))?.title; const { initialize, play } = useStreamingStore(); + const refreshSeason = useRefreshSeason(); const [dialogEpisode, setDialogEpisode] = useState(null); const [showMarkAllDialog, setShowMarkAllDialog] = useState(false); + const [showRatingDialog, setShowRatingDialog] = useState(false); const [streamingOpen, setStreamingOpen] = useState(false); const handleWatchEpisode = (season: number, episodeNumber: number) => { @@ -98,16 +120,37 @@ export function SeasonPage() {

{season.name}

- {/* Mark Season as Watched — top right */} - + {/* Actions dropdown — top right */} + + + + + + setShowMarkAllDialog(true)}> + + Mark Season Watched + + setShowRatingDialog(true)}> + + Rate Season + + refreshSeason.mutate(season.id)} + disabled={refreshSeason.isPending} + > + + Refresh Season + + +
Season {season.season_number} @@ -121,6 +164,21 @@ export function SeasonPage() { {totalEpisodes} episodes
+ {/* Season rating */} +
+ + {season.user_rating != null && season.user_rating > 0 && ( + + {season.user_rating}/10 + + )} + {season.tmdb_rating != null && season.tmdb_rating > 0 && ( + + TMDB {season.tmdb_rating} + + )} +
+ {/* Watch progress */}
@@ -281,6 +339,15 @@ export function SeasonPage() { episodes={episodes} /> + {/* Season Rating dialog */} + {season && ( + + )} + {/* Streaming dialog */} {entry && streamingOpen && (