fix: Live Courts data structure and status naming
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -135,7 +135,7 @@ export default function LiveCourtsPage() {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) throw new Error("Failed to load court data");
|
||||
const data = await response.json();
|
||||
setCourts(data.courts ?? data.data ?? []);
|
||||
setCourts(Array.isArray(data) ? data : data.courts ?? data.data ?? []);
|
||||
setLastUpdated(new Date());
|
||||
} catch (err) {
|
||||
console.error("Live courts fetch error:", err);
|
||||
|
||||
@@ -63,26 +63,46 @@ export async function GET(request: NextRequest) {
|
||||
orderBy: { displayOrder: 'asc' },
|
||||
});
|
||||
|
||||
// Compute status for each court
|
||||
// Compute status for each court and transform to frontend shape
|
||||
const courtsWithStatus = courts.map((court) => {
|
||||
let status: 'available' | 'active' | 'open-play' | 'booked';
|
||||
let status: 'available' | 'active' | 'open_play' | 'booked';
|
||||
|
||||
if (court.sessions.length > 0) {
|
||||
// Court has active sessions
|
||||
if (court.isOpenPlay) {
|
||||
status = 'open-play';
|
||||
} else {
|
||||
status = 'active';
|
||||
}
|
||||
status = court.isOpenPlay ? 'open_play' : 'active';
|
||||
} else if (court.isOpenPlay) {
|
||||
status = 'open_play';
|
||||
} else if (court.bookings.length > 0) {
|
||||
status = 'booked';
|
||||
} else {
|
||||
status = 'available';
|
||||
}
|
||||
|
||||
// Transform sessions to players array for frontend
|
||||
const players = court.sessions.map((session) => ({
|
||||
id: session.client?.id || session.id,
|
||||
firstName: session.client?.firstName,
|
||||
lastName: session.client?.lastName,
|
||||
walkInName: session.walkInName,
|
||||
checkedInAt: session.startTime.toISOString(),
|
||||
sessionId: session.id,
|
||||
}));
|
||||
|
||||
// Get upcoming booking info
|
||||
const upcomingBooking = court.bookings.length > 0 ? {
|
||||
startTime: court.bookings[0].startTime.toISOString(),
|
||||
clientName: court.bookings[0].client
|
||||
? `${court.bookings[0].client.firstName} ${court.bookings[0].client.lastName}`
|
||||
: 'Walk-in',
|
||||
} : undefined;
|
||||
|
||||
return {
|
||||
...court,
|
||||
id: court.id,
|
||||
name: court.name,
|
||||
type: court.type,
|
||||
isOpenPlay: court.isOpenPlay,
|
||||
status,
|
||||
players,
|
||||
upcomingBooking,
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user