brackt/app/routes/api/queue.reorder.ts

55 lines
1.7 KiB
TypeScript
Raw Normal View History

import { getAuth } from "@clerk/react-router/server";
import { database } from "~/database/context";
import * as schema from "~/database/schema";
import { eq } from "drizzle-orm";
import { reorderQueueWithSeason, getTeamQueue } from "~/models/draft-queue";
export async function action(args: any) {
const { request } = args;
const auth = await getAuth(args);
const userId = (auth as any).userId as string | null;
if (!userId) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
const formData = await request.formData();
const teamId = formData.get("teamId");
const seasonId = formData.get("seasonId");
const participantIdsJson = formData.get("participantIds");
if (!teamId || !seasonId || !participantIdsJson) {
return Response.json({ error: "Missing required fields" }, { status: 400 });
}
Improve draft room UX with better error handling and UI refinements (#17) * Remove pause/resume controls when draft is complete, rename Live to Connected - Hide Pause/Resume Draft buttons when isDraftComplete is true - Change 'Live' status indicator to 'Connected' in both draft room and draft board views https://claude.ai/code/session_01AUaKzx465NrY29Qv6MVwjC * Fix code review issues in draft room: security, bugs, and quality Security: - Fix inconsistent commissioner check: draft.start, force-autopick, force-manual-pick, and make-pick all now query the commissioners table instead of league.createdBy, so co-commissioners have consistent access to all draft controls Bugs: - canPick now includes !isPaused so the UI correctly blocks picks during a pause - isDraftComplete initial state now covers 'completed' season status, not just 'active' - Guard JSON.parse in queue.reorder.ts with try/catch to return 400 instead of 500 Code quality: - Add error handling (try/catch + toast) to handlePauseDraft, handleResumeDraft, handleRemoveFromQueue, and handleReorderQueue - Replace alert() with toast.error() in handleMakePick, handleForceAutopick, handleForceManualPick for consistent UX - Memoize filteredParticipants with useMemo to avoid recomputing on every render - Replace custom force-pick dialog div with ShadCN Dialog component for proper keyboard support (Escape to close, focus trap, accessible markup); add dialog.tsx - Remove console.log debug statements from socket event handlers and API routes - Replace (global as any).__socketIO with getSocketIO() across all API routes - Replace window.location.reload() in handleStartDraft with useRevalidator https://claude.ai/code/session_01AUaKzx465NrY29Qv6MVwjC --------- Co-authored-by: Claude <noreply@anthropic.com>
2026-02-20 21:50:27 -08:00
let participantIds: string[];
try {
participantIds = JSON.parse(participantIdsJson as string);
} catch {
return Response.json({ error: "Invalid participantIds format" }, { status: 400 });
}
const db = database();
// Verify user owns this team
const team = await db.query.teams.findFirst({
where: eq(schema.teams.id, teamId as string),
});
if (!team || team.ownerId !== userId) {
return Response.json({ error: "Unauthorized" }, { status: 403 });
}
// Reorder queue
await reorderQueueWithSeason(seasonId as string, teamId as string, participantIds);
// Get updated queue
const updatedQueue = await getTeamQueue(teamId as string);
// TODO: Emit socket event for real-time update
// const io = getSocketIO();
// io.to(`draft-${seasonId}`).emit("queue-updated", { teamId, queue: updatedQueue });
return Response.json({ success: true, queue: updatedQueue });
}