update leaderboard schema to match new API wire format with flattened FL points
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { Leaderboard, LeaderboardDriver } from '@/lib/schemas/leaderboard';
|
import { Leaderboard, LeaderboardDriver, FlPoints } from '@/lib/schemas/leaderboard';
|
||||||
import { saveLeaderboardAction } from '@/lib/actions/leaderboard';
|
import { saveLeaderboardAction } from '@/lib/actions/leaderboard';
|
||||||
import { LeaderboardStandings } from './LeaderboardStandings';
|
import { LeaderboardStandings } from './LeaderboardStandings';
|
||||||
import { DriversEditor } from './editors/DriversEditor';
|
import { DriversEditor } from './editors/DriversEditor';
|
||||||
@@ -24,13 +24,38 @@ interface LeaderboardManagerProps {
|
|||||||
initialData: Leaderboard;
|
initialData: Leaderboard;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDirty(a: Leaderboard, b: Leaderboard) {
|
// Extended type for internal UI state with full FL Points object
|
||||||
|
type LeaderboardDraft = Leaderboard & { flPointsData: FlPoints };
|
||||||
|
|
||||||
|
function toApiFormat(draft: LeaderboardDraft): Leaderboard {
|
||||||
|
return {
|
||||||
|
...draft,
|
||||||
|
flPoints: draft.flPointsData.points,
|
||||||
|
flColor: draft.flPointsData.color,
|
||||||
|
flTextColor: draft.flPointsData.textColor
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function toUiFormat(api: Leaderboard): LeaderboardDraft {
|
||||||
|
return {
|
||||||
|
...api,
|
||||||
|
flPointsData: {
|
||||||
|
points: api.flPoints,
|
||||||
|
label: 'FL +' + api.flPoints,
|
||||||
|
color: api.flColor,
|
||||||
|
textColor: api.flTextColor,
|
||||||
|
priority: 3
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDirty(a: LeaderboardDraft, b: LeaderboardDraft) {
|
||||||
return JSON.stringify(a) !== JSON.stringify(b);
|
return JSON.stringify(a) !== JSON.stringify(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function LeaderboardManager({ serverId, initialData }: LeaderboardManagerProps) {
|
export function LeaderboardManager({ serverId, initialData }: LeaderboardManagerProps) {
|
||||||
const [draft, setDraft] = useState<Leaderboard>(initialData);
|
const [draft, setDraft] = useState<LeaderboardDraft>(toUiFormat(initialData));
|
||||||
const [saved, setSaved] = useState<Leaderboard>(initialData);
|
const [saved, setSaved] = useState<LeaderboardDraft>(toUiFormat(initialData));
|
||||||
const [activeSection, setActiveSection] = useState<Section>('standings');
|
const [activeSection, setActiveSection] = useState<Section>('standings');
|
||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
@@ -61,7 +86,8 @@ export function LeaderboardManager({ serverId, initialData }: LeaderboardManager
|
|||||||
setIsSubmitting(true);
|
setIsSubmitting(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const result = await saveLeaderboardAction(serverId, draft);
|
const apiData = toApiFormat(draft);
|
||||||
|
const result = await saveLeaderboardAction(serverId, apiData);
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
setSaved(draft);
|
setSaved(draft);
|
||||||
} else {
|
} else {
|
||||||
@@ -77,7 +103,7 @@ export function LeaderboardManager({ serverId, initialData }: LeaderboardManager
|
|||||||
const renderSection = () => {
|
const renderSection = () => {
|
||||||
switch (activeSection) {
|
switch (activeSection) {
|
||||||
case 'standings':
|
case 'standings':
|
||||||
return <LeaderboardStandings leaderboard={draft} />;
|
return <LeaderboardStandings leaderboard={toApiFormat(draft)} />;
|
||||||
case 'drivers':
|
case 'drivers':
|
||||||
return <DriversEditor drivers={draft.drivers} onChange={handleDriversChange} />;
|
return <DriversEditor drivers={draft.drivers} onChange={handleDriversChange} />;
|
||||||
case 'points':
|
case 'points':
|
||||||
@@ -90,8 +116,8 @@ export function LeaderboardManager({ serverId, initialData }: LeaderboardManager
|
|||||||
case 'fl':
|
case 'fl':
|
||||||
return (
|
return (
|
||||||
<FlPointsEditor
|
<FlPointsEditor
|
||||||
flPoints={draft.flPoints}
|
flPoints={draft.flPointsData}
|
||||||
onChange={(flPoints) => setDraft({ ...draft, flPoints })}
|
onChange={(flPointsData) => setDraft({ ...draft, flPointsData })}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
case 'tracks':
|
case 'tracks':
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export function LeaderboardStandings({ leaderboard }: LeaderboardStandingsProps)
|
|||||||
).length;
|
).length;
|
||||||
const totalPoints =
|
const totalPoints =
|
||||||
trackResults.reduce((sum: number, r) => sum + (typeof r === 'number' ? r : 0), 0) +
|
trackResults.reduce((sum: number, r) => sum + (typeof r === 'number' ? r : 0), 0) +
|
||||||
fastestLapCount * leaderboard.flPoints.points;
|
fastestLapCount * leaderboard.flPoints;
|
||||||
return { driver, driverId: driver.id, totalPoints, trackResults, fastestLapCount };
|
return { driver, driverId: driver.id, totalPoints, trackResults, fastestLapCount };
|
||||||
})
|
})
|
||||||
.sort((a, b) => b.totalPoints - a.totalPoints);
|
.sort((a, b) => b.totalPoints - a.totalPoints);
|
||||||
@@ -87,10 +87,10 @@ export function LeaderboardStandings({ leaderboard }: LeaderboardStandingsProps)
|
|||||||
<span
|
<span
|
||||||
className="ml-0.5 rounded px-0.5 text-xs"
|
className="ml-0.5 rounded px-0.5 text-xs"
|
||||||
style={{
|
style={{
|
||||||
backgroundColor: leaderboard.flPoints.color,
|
backgroundColor: leaderboard.flColor,
|
||||||
color: leaderboard.flPoints.textColor
|
color: leaderboard.flTextColor
|
||||||
}}
|
}}
|
||||||
title={`Fastest lap: ${leaderboard.flPoints.label}`}
|
title="Fastest lap"
|
||||||
>
|
>
|
||||||
FL
|
FL
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export function PointsTableEditor({ pointsTable, onChange }: PointsTableEditorPr
|
|||||||
const pos = pointsTable.length + 1;
|
const pos = pointsTable.length + 1;
|
||||||
onChange([
|
onChange([
|
||||||
...pointsTable,
|
...pointsTable,
|
||||||
{ points: 0, label: `P${pos}`, color: '#374151', textColor: '#ffffff', priority: pos }
|
{ id: crypto.randomUUID(), points: 0, label: `P${pos}`, color: '#374151', textColor: '#ffffff', priority: pos }
|
||||||
]);
|
]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ export function TracksEditor({ tracks, drivers, pointsTable, onChange }: TracksE
|
|||||||
onChange([
|
onChange([
|
||||||
...tracks,
|
...tracks,
|
||||||
{
|
{
|
||||||
|
id: crypto.randomUUID(),
|
||||||
name: `Race ${tracks.length + 1}`,
|
name: `Race ${tracks.length + 1}`,
|
||||||
results: drivers.map((d) => ({ driverId: d.id, score: 'DNS' as const })),
|
results: drivers.map((d) => ({ driverId: d.id, score: 'DNS' as const })),
|
||||||
fastestLapDriverId: ''
|
fastestLapDriverId: ''
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export const leaderboardDriverSchema = z.object({
|
|||||||
export type LeaderboardDriver = z.infer<typeof leaderboardDriverSchema>;
|
export type LeaderboardDriver = z.infer<typeof leaderboardDriverSchema>;
|
||||||
|
|
||||||
const pointsEntryFields = {
|
const pointsEntryFields = {
|
||||||
|
id: z.string(),
|
||||||
points: z.number().int().min(0),
|
points: z.number().int().min(0),
|
||||||
label: z.string().min(1),
|
label: z.string().min(1),
|
||||||
color: z.string().regex(/^#[0-9a-fA-F]{6}$/),
|
color: z.string().regex(/^#[0-9a-fA-F]{6}$/),
|
||||||
@@ -22,10 +23,17 @@ const pointsEntryFields = {
|
|||||||
export const pointsEntrySchema = z.object(pointsEntryFields);
|
export const pointsEntrySchema = z.object(pointsEntryFields);
|
||||||
export type PointsEntry = z.infer<typeof pointsEntrySchema>;
|
export type PointsEntry = z.infer<typeof pointsEntrySchema>;
|
||||||
|
|
||||||
export const flPointsSchema = z.object(pointsEntryFields);
|
export const flPointsSchema = z.object({
|
||||||
|
points: z.number().int().min(0),
|
||||||
|
label: z.string().min(1),
|
||||||
|
color: z.string().regex(/^#[0-9a-fA-F]{6}$/),
|
||||||
|
textColor: z.string().regex(/^#[0-9a-fA-F]{6}$/),
|
||||||
|
priority: z.number().int().min(0)
|
||||||
|
});
|
||||||
export type FlPoints = z.infer<typeof flPointsSchema>;
|
export type FlPoints = z.infer<typeof flPointsSchema>;
|
||||||
|
|
||||||
export const leaderboardTrackSchema = z.object({
|
export const leaderboardTrackSchema = z.object({
|
||||||
|
id: z.string(),
|
||||||
name: z.string().min(1),
|
name: z.string().min(1),
|
||||||
results: z.array(z.object({ driverId: z.string(), score: resultValueSchema })),
|
results: z.array(z.object({ driverId: z.string(), score: resultValueSchema })),
|
||||||
fastestLapDriverId: z.string()
|
fastestLapDriverId: z.string()
|
||||||
@@ -33,9 +41,13 @@ export const leaderboardTrackSchema = z.object({
|
|||||||
export type LeaderboardTrack = z.infer<typeof leaderboardTrackSchema>;
|
export type LeaderboardTrack = z.infer<typeof leaderboardTrackSchema>;
|
||||||
|
|
||||||
export const leaderboardSchema = z.object({
|
export const leaderboardSchema = z.object({
|
||||||
|
id: z.string().optional(),
|
||||||
|
serverId: z.string().optional(),
|
||||||
drivers: z.array(leaderboardDriverSchema),
|
drivers: z.array(leaderboardDriverSchema),
|
||||||
pointsTable: z.array(pointsEntrySchema),
|
pointsTable: z.array(pointsEntrySchema),
|
||||||
flPoints: flPointsSchema,
|
flPoints: z.number().int().min(0),
|
||||||
|
flColor: z.string().regex(/^#[0-9a-fA-F]{6}$/),
|
||||||
|
flTextColor: z.string().regex(/^#[0-9a-fA-F]{6}$/),
|
||||||
tracks: z.array(leaderboardTrackSchema)
|
tracks: z.array(leaderboardTrackSchema)
|
||||||
});
|
});
|
||||||
export type Leaderboard = z.infer<typeof leaderboardSchema>;
|
export type Leaderboard = z.infer<typeof leaderboardSchema>;
|
||||||
@@ -43,7 +55,9 @@ export type Leaderboard = z.infer<typeof leaderboardSchema>;
|
|||||||
export const emptyLeaderboard: Leaderboard = {
|
export const emptyLeaderboard: Leaderboard = {
|
||||||
drivers: [],
|
drivers: [],
|
||||||
pointsTable: [],
|
pointsTable: [],
|
||||||
flPoints: { points: 1, label: 'FL +1', color: '#8b5cf6', textColor: '#000000', priority: 3 },
|
flPoints: 1,
|
||||||
|
flColor: '#8b5cf6',
|
||||||
|
flTextColor: '#000000',
|
||||||
tracks: []
|
tracks: []
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user