mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 01:35:39 -04:00
Add quick-add watchlist button and consolidate server actions into lib/actions
Move all server actions from scattered page-level files into shared lib/actions/ directory (settings.ts, titles.ts, watchlist.ts) so they can be reused across the app. Add a hover-triggered plus button on explore page title cards that lets users add titles to their watchlist without navigating away. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
"use server";
|
||||
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { headers } from "next/headers";
|
||||
import { auth } from "@/lib/auth/server";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { userTitleStatus } from "@/lib/db/schema";
|
||||
import { importTitle } from "@/lib/services/metadata";
|
||||
import { setTitleStatus } from "@/lib/services/tracking";
|
||||
|
||||
export async function quickAddToWatchlist(
|
||||
tmdbId: number,
|
||||
type: "movie" | "tv",
|
||||
) {
|
||||
const session = await auth.api.getSession({ headers: await headers() });
|
||||
if (!session) throw new Error("Unauthorized");
|
||||
const userId = session.user.id;
|
||||
|
||||
const title = await importTitle(tmdbId, type);
|
||||
if (!title) throw new Error("Failed to import title");
|
||||
|
||||
const existing = db
|
||||
.select()
|
||||
.from(userTitleStatus)
|
||||
.where(
|
||||
and(
|
||||
eq(userTitleStatus.userId, userId),
|
||||
eq(userTitleStatus.titleId, title.id),
|
||||
),
|
||||
)
|
||||
.get();
|
||||
|
||||
if (existing) {
|
||||
return { success: true, titleId: title.id, alreadyAdded: true };
|
||||
}
|
||||
|
||||
setTitleStatus(userId, title.id, "watchlist");
|
||||
return { success: true, titleId: title.id, alreadyAdded: false };
|
||||
}
|
||||
Reference in New Issue
Block a user