diff --git a/app/routes/events-team.tsx b/app/routes/events-team.tsx index 23bf1c1..669546c 100644 --- a/app/routes/events-team.tsx +++ b/app/routes/events-team.tsx @@ -84,6 +84,16 @@ export default function () { } = useLoaderData(); const [eventData, setEventData] = useState(events); const { isOpen, onClose, onOpen } = useDisclosure(); + const { + isOpen: isCompleteOpen, + onClose: closeComplete, + onOpen: openComplete, + } = useDisclosure(); + const { + isOpen: isAnsweredOpen, + onClose: closeAnswered, + onOpen: openAnswered, + } = useDisclosure(); const toast = useToast(); const [selectedEvent, setSelectedEvent] = useState(""); @@ -160,16 +170,92 @@ export default function () { toast({ description: "Game night certified", + status: "success", title: "Success", }); const newEventData = eventData; - newEventData[ eventData.findIndex((e) => e.id === eventId) ].reached_minimum_player_count = true; setEventData([...newEventData]); + setSelectedEvent(""); + } + + async function markAnswered(eventId: string) { + const answerResp = await fetch(`/api/events-team/events/${eventId}/solve`, { + body: "{}", + headers: { + "content-type": "application/json", + }, + method: "POST", + }); + + closeAnswered(); + + if (!answerResp.ok) { + toast({ + description: "Failed to mark as solved", + status: "error", + title: "Oops", + }); + + return; + } + + const newEventData = eventData; + newEventData[eventData.findIndex((e) => e.id === eventId)].answered_at = + Date.now(); + + setEventData([...newEventData]); + setSelectedEvent(""); + } + + async function markComplete(eventId: string) { + const completeResp = await fetch( + `/api/events-team/events/${eventId}/complete`, + { + body: "{}", + headers: { + "content-type": "application/json", + }, + method: "POST", + }, + ); + + closeComplete(); + + if (!completeResp.ok) { + let msg = "Unknown error"; + + try { + msg = ((await completeResp.json()) as { error: string }).error; + } catch {} + + toast({ + description: msg, + status: "error", + title: "Failed to complete", + }); + + return; + } + + toast({ + description: "Event marked as completed", + status: "success", + title: "Success", + }); + + const newEventData = eventData; + + // Technically this won't be the same as the time in the db, but that doesn't matter since this is just to hide the button + newEventData[eventData.findIndex((e) => e.id === eventId)].performed_at = + Date.now(); + + setEventData([...newEventData]); + setSelectedEvent(""); } return ( @@ -202,6 +288,49 @@ export default function () { + + + + Mark as Completed + + + + By marking this event as completed, you confirm that the event + creator has performed this event + + + + + + + + + + + + Mark as Solved + + + Are you sure you want to mark this riddle as solved? + + + + + + + {eventData.map((event) => { const eventCreatorName = members.find( @@ -266,21 +395,46 @@ export default function () { ) : null} + {can_approve && !event.pending && !event.performed_at ? ( + + ) : null} + {can_approve && + !event.pending && + event.performed_at && + event.type === "rotw" && + !event.answered_at ? ( + + ) : null} {can_approve && event.approved && event.type === "gamenight" && + event.performed_at && !event.reached_minimum_player_count ? ( - <> - - + ) : null} diff --git a/functions/api/events-team/events/[id]/complete.ts b/functions/api/events-team/events/[id]/complete.ts new file mode 100644 index 0000000..4feea44 --- /dev/null +++ b/functions/api/events-team/events/[id]/complete.ts @@ -0,0 +1,13 @@ +export async function onRequestPost(context: RequestContext) { + const id = context.params.id as string; + + await context.env.D1.prepare( + "UPDATE events SET performed_at = ? WHERE id = ?;", + ) + .bind(Date.now(), id) + .run(); + + return new Response(null, { + status: 204, + }); +} diff --git a/functions/api/events-team/events/[id]/solve.ts b/functions/api/events-team/events/[id]/solve.ts new file mode 100644 index 0000000..8a9fd57 --- /dev/null +++ b/functions/api/events-team/events/[id]/solve.ts @@ -0,0 +1,11 @@ +export async function onRequestPost(context: RequestContext) { + await context.env.D1.prepare( + "UPDATE events SET answered_at = ? WHERE id = ?;", + ) + .bind(Date.now(), context.params.id) + .run(); + + return new Response(null, { + status: 204, + }); +}