Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion actions/supabase/queries/registrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function registerForWorkshop(workshopId: string): Promise<void> {

const { data: participant, error: profileError } = await supabase
.from("participants")
.select("id")
.select("id, verification_status")
.eq("id", user.id)
.maybeSingle();
if (profileError)
Expand All @@ -33,6 +33,9 @@ export async function registerForWorkshop(workshopId: string): Promise<void> {
throw new Error(
"Please complete your participant profile before registering for a workshop.",
);
// Only verified participants can sign up, even if the button was reached.
if (!participant.verification_status)
throw new Error("Please verify your account before registering.");

// Only active registrations are unique; cancelled history is preserved.
const { error } = await supabase.from("registrations").insert({
Expand All @@ -49,6 +52,21 @@ export async function registerForWorkshop(workshopId: string): Promise<void> {
}
}

// Used to decide whether to show the Register button or a prompt to verify.
export async function isVerifiedParticipant(): Promise<boolean> {
const user = await getCurrentUser();
if (!user) return false;

const { data, error } = await supabase
.from("participants")
.select("verification_status")
.eq("id", user.id)
.maybeSingle();
if (error)
throw new Error("Could not check your verification. Please try again.");
return data?.verification_status === true;
}

export async function isRegisteredForWorkshop(
workshopId: string,
): Promise<boolean> {
Expand Down
13 changes: 13 additions & 0 deletions actions/supabase/queries/workshops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ export async function fetchWorkshops(): Promise<Workshop[]> {
return data;
}

// Only workshops that haven't started yet, soonest first. Workshops without a
// start date are left out because we can't tell if they're upcoming.
export async function fetchUpcomingWorkshops(): Promise<Workshop[]> {
const { data, error } = await supabase
.from("workshops")
.select("*")
.gte("start_date", new Date().toISOString())
.order("start_date", { ascending: true });

if (error) throw new Error("Could not load events. Please try again.");
return data;
}

export async function fetchWorkshop(id: string): Promise<Workshop | null> {
const { data, error } = await supabase
.from("workshops")
Expand Down
2 changes: 2 additions & 0 deletions app/(participant)/events/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// The event detail page is the workshop detail page, served at /events/<id>.
export { default } from "../../workshops/[id]/page";
63 changes: 63 additions & 0 deletions app/(participant)/events/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"use client";

import type { Workshop } from "@/types/workshop";
import { useEffect, useState } from "react";
import Link from "next/link";
import { fetchUpcomingWorkshops } from "@/actions/supabase/queries/workshops";

export default function EventsPage() {
const [events, setEvents] = useState<Workshop[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
let ignore = false;

async function loadEvents() {
try {
const data = await fetchUpcomingWorkshops();
if (!ignore) setEvents(data);
} catch (err) {
if (!ignore) {
setError(
err instanceof Error ? err.message : "Could not load events.",
);
}
} finally {
if (!ignore) setLoading(false);
}
}

loadEvents();

return () => {
ignore = true;
};
}, []);

if (loading) return <p role="status">Loading events...</p>;

if (error) return <p role="alert">{error}</p>;

return (
<main>
<h1>Upcoming events</h1>

{events.length === 0 ? (
<p>There are no upcoming events right now. Please check back soon.</p>
) : (
<ul>
{events.map(event => (
<li key={event.id}>
<h2>
<Link href={`/events/${event.id}`}>{event.name}</Link>
</h2>
<p>Start date: {event.start_date ?? "Not set"}</p>
<p>Location: {event.location ?? "Not set"}</p>
</li>
))}
</ul>
)}
</main>
);
}
15 changes: 13 additions & 2 deletions app/(participant)/workshops/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import type { Workshop } from "@/types/workshop";
import { useEffect, useState } from "react";
import Link from "next/link";
import { useParams } from "next/navigation";
import {
isRegisteredForWorkshop,
isVerifiedParticipant,
registerForWorkshop,
} from "@/actions/supabase/queries/registrations";
import {
Expand All @@ -24,6 +26,7 @@ export default function WorkshopDetailsPage() {
);
const [registeredCount, setRegisteredCount] = useState(0);
const [registered, setRegistered] = useState(false);
const [verified, setVerified] = useState(false);

useEffect(() => {
let ignore = false;
Expand All @@ -38,17 +41,19 @@ export default function WorkshopDetailsPage() {
try {
const data = await fetchWorkshop(id);
// Invalid or missing workshops must not trigger registration queries.
const [alreadyRegistered, count] = data
const [alreadyRegistered, count, isVerified] = data
? await Promise.all([
isRegisteredForWorkshop(id),
fetchWorkshopRegistrationCount(id),
isVerifiedParticipant(),
])
: ([false, 0] as const);
: ([false, 0, false] as const);

if (!ignore) {
setWorkshop(data);
setRegistered(alreadyRegistered);
setRegisteredCount(count);
setVerified(isVerified);
}
} catch (err) {
if (!ignore) {
Expand Down Expand Up @@ -151,6 +156,12 @@ export default function WorkshopDetailsPage() {

{registered ? (
<p role="status">You’re registered for this workshop.</p>
) : !verified ? (
// Unverified users can't sign up; send them to verify instead.
<p role="status">
You need to verify your account to register.{" "}
<Link href="/profile">Verify on your profile</Link>
</p>
) : (
<button
type="button"
Expand Down
12 changes: 12 additions & 0 deletions supabase/seed_events.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Sample events to check /events: only the three future ones should appear.
-- Run in the Supabase SQL editor. Dates are relative to now, so it stays valid.
insert into workshops (name, description, start_date, end_date, location, capacity)
values
('Seed: Resume Workshop (future)', 'Future event', now() + interval '3 days', now() + interval '3 days 2 hours', 'Online', 20),
('Seed: Networking Night (future)', 'Future event', now() + interval '10 days', now() + interval '10 days 2 hours', 'Oakland, CA', null),
('Seed: Benefits Q&A (future)', 'Future event', now() + interval '30 days', now() + interval '30 days 1 hour', 'Online', 50),
('Seed: Interview Prep (past)', 'Past event', now() - interval '5 days', now() - interval '5 days' + interval '2 hours', 'Online', 20),
('Seed: Career Fair (past)', 'Past event', now() - interval '30 days', now() - interval '30 days' + interval '4 hours', 'San Francisco, CA', 100);

-- Cleanup when done:
-- delete from workshops where name like 'Seed: %';
Loading