diff --git a/.gitignore b/.gitignore index 8de8ede..fca56d5 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ data/ npm-debug.log* *.log .cursor +.verify-data/ \ No newline at end of file diff --git a/public/css/app.css b/public/css/app.css index 999516f..304d978 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -665,6 +665,33 @@ main a:hover { text-decoration: none; } +.receipt-dates { + display: grid; + margin: 0; + padding: 0.35rem 0 0; + list-style: none; +} + +.receipt-dates > li { + display: grid; + gap: 0.45rem; + padding: 0.85rem 1.25rem 0.35rem; +} + +.receipt-dates > li + li { + margin-top: 0.35rem; + border-top: 1px solid var(--hope-border); +} + +.receipt-dates .receipt-link { + margin-top: 0.2rem; + border-radius: 1rem; +} + +.receipt-dates + .receipt-note { + padding: 0.35rem 1.25rem 1.2rem; +} + .manage { display: grid; gap: 0.35rem 0; @@ -1890,6 +1917,125 @@ article.shift-card { padding: 0 0.35rem; } +.signup-form .more-dates { + border: 0; + padding: 0; + background: transparent; +} + +.signup-form .more-dates > summary { + display: flex; + align-items: center; + justify-content: space-between; + gap: 0.75rem; + min-height: 48px; + padding: 0.7rem 1rem; + list-style: none; + cursor: pointer; + background: var(--hope-blue-50); + border-radius: 999px; + box-shadow: var(--hope-shadow); + font-weight: 650; +} + +.signup-form .more-dates > summary::-webkit-details-marker { + display: none; +} + +.signup-form .more-dates > summary::marker { + content: ""; +} + +.signup-form .more-dates > summary::after { + content: ""; + width: 0.45rem; + height: 0.45rem; + margin-left: auto; + border-right: 2px solid currentColor; + border-bottom: 2px solid currentColor; + transform: translateY(-0.12rem) rotate(45deg); + flex: none; +} + +.signup-form .more-dates[open] > summary { + border-radius: 1.25rem 1.25rem 0 0; +} + +.signup-form .more-dates[open] > summary::after { + transform: translateY(0.12rem) rotate(225deg); +} + +.signup-form .more-dates-count { + color: var(--hope-ink-2); + font-size: 0.92rem; + font-weight: 500; +} + +.signup-form .more-dates-count[hidden] { + display: none; +} + +.signup-form .more-dates-panel { + display: grid; + gap: 0.55rem; + padding: 0.75rem 0.85rem 0.9rem; + background: var(--hope-blue-50); + border-radius: 0 0 1.25rem 1.25rem; + box-shadow: var(--hope-shadow); +} + +.signup-form .more-dates-panel .signup-note { + font-weight: 500; +} + +.signup-form .date-choices { + display: grid; + gap: 0.15rem; + max-height: 16.5rem; + margin: 0; + padding: 0; + overflow: auto; + list-style: none; +} + +.signup-form .date-choice { + display: flex; + align-items: flex-start; + gap: 0.7rem; + margin: 0; + padding: 0.4rem 0.15rem; + font-weight: 650; +} + +.signup-form .date-choice input { + width: 1.15rem; + height: 1.15rem; + margin-top: 0.2rem; + accent-color: var(--hope-gold); + flex: none; +} + +.signup-form .date-choice time, +.signup-form .date-choice-role, +.signup-form .date-choice-event, +.signup-form .date-choice-clock { + display: block; +} + +.signup-form .date-choice-clock { + margin-top: 0.1rem; + color: var(--hope-ink-2); + font-weight: 500; +} + +.signup-form .date-choice-spots { + display: block; + margin-top: 0.1rem; + color: var(--hope-ink-2); + font-size: 0.92rem; + font-weight: 500; +} + .signup-form input, .signup-form select, .signup-form textarea, diff --git a/public/js/enhance.js b/public/js/enhance.js index c6ccefd..801d5ae 100644 --- a/public/js/enhance.js +++ b/public/js/enhance.js @@ -3,6 +3,7 @@ bindCalendar(); bindViewMenu(); bindManage(); + bindSignupDates(); document.querySelectorAll('[data-contact]').forEach(bindContact); document.querySelectorAll('[data-see-more]').forEach(bindSeeMore); @@ -117,6 +118,52 @@ }); } + function bindSignupDates() { + document.addEventListener('click', function (event) { + var openMenus = document.querySelectorAll('details.more-dates[open]'); + for (var j = 0; j < openMenus.length; j += 1) { + if (!openMenus[j].contains(event.target)) openMenus[j].open = false; + } + }); + document.addEventListener('change', function (event) { + var box = event.target; + if (!box || box.name !== 'dates' || !box.form) return; + syncSignupDates(box.form); + }); + } + + function syncSignupDates(form) { + var boxes = form.querySelectorAll('input[name="dates"]'); + if (!boxes.length) return; + var pageRoom = Number(form.getAttribute('data-page-remaining')); + var min = pageRoom > 0 ? pageRoom : Infinity; + var checked = 0; + for (var i = 0; i < boxes.length; i += 1) { + if (!boxes[i].checked) continue; + checked += 1; + var room = Number(boxes[i].getAttribute('data-remaining')); + if (room > 0 && room < min) min = room; + } + var select = form.querySelector('select[name="party_size"]'); + if (select && min < Infinity) { + for (var j = 0; j < select.options.length; j += 1) { + var option = select.options[j]; + option.disabled = Number(option.value) > min; + } + if (select.selectedOptions.length && select.selectedOptions[0].disabled) select.value = String(min); + } + var count = form.querySelector('[data-more-dates-count]'); + if (count) { + count.hidden = checked === 0; + count.textContent = checked === 1 ? '1 selected' : checked + ' selected'; + } + var button = form.querySelector('button[type="submit"]'); + if (button && form.getAttribute('data-sending') !== 'true') { + var total = pageRoom > 0 ? checked + 1 : checked; + button.textContent = total > 1 ? 'Sign up for ' + total + ' dates' : 'Sign up'; + } + } + function bindContact(picker) { picker.addEventListener('change', function () { var form = picker.form; diff --git a/public/js/signup-dialog.js b/public/js/signup-dialog.js index c0ed5e5..945054d 100644 --- a/public/js/signup-dialog.js +++ b/public/js/signup-dialog.js @@ -177,7 +177,9 @@ form.removeAttribute('data-sending'); if (submit) { submit.disabled = false; - submit.textContent = 'Sign up'; + var chosen = form.querySelectorAll('input[name="dates"]:checked').length; + var total = form.hasAttribute('data-page-remaining') ? chosen + 1 : chosen; + submit.textContent = total > 1 ? 'Sign up for ' + total + ' dates' : 'Sign up'; } var alert = form.querySelector('[data-signup-send-error]'); if (!alert) { diff --git a/src/confirm-cookie.ts b/src/confirm-cookie.ts index f619a71..a3f6bbf 100644 --- a/src/confirm-cookie.ts +++ b/src/confirm-cookie.ts @@ -3,17 +3,31 @@ import { createCipheriv, createDecipheriv, createHash, randomBytes } from 'node: const COOKIE = 'hope_confirm'; const MAX_AGE_SECONDS = 900; +export type ConfirmationCompanion = { + signupId: number; + shiftId: number; + token: string; +}; + export type ConfirmationSeal = { signupId: number; shiftId: number; token: string; expiresAtMs: number; + companions?: ConfirmationCompanion[]; }; export function sealConfirmation(secret: string, payload: ConfirmationSeal): string { const iv = randomBytes(12); const cipher = createCipheriv('aes-256-gcm', cookieKey(secret), iv); - const ciphertext = Buffer.concat([cipher.update(JSON.stringify(payload), 'utf8'), cipher.final()]); + const stored = { + signupId: payload.signupId, + shiftId: payload.shiftId, + token: payload.token, + expiresAtMs: payload.expiresAtMs, + ...(payload.companions && payload.companions.length > 0 ? { companions: payload.companions } : {}), + }; + const ciphertext = Buffer.concat([cipher.update(JSON.stringify(stored), 'utf8'), cipher.final()]); const tag = cipher.getAuthTag(); return Buffer.concat([iv, tag, ciphertext]).toString('base64url'); } @@ -34,11 +48,14 @@ export function openConfirmation(secret: string, cookieHeader: string | undefine if (!parsed || typeof parsed.signupId !== 'number' || typeof parsed.shiftId !== 'number') return null; if (typeof parsed.token !== 'string' || typeof parsed.expiresAtMs !== 'number') return null; if (parsed.expiresAtMs <= nowMs) return null; + const companions = readCompanions(parsed.companions); + if (!companions) return null; return { signupId: parsed.signupId, shiftId: parsed.shiftId, token: parsed.token, expiresAtMs: parsed.expiresAtMs, + ...(companions.length > 0 ? { companions } : {}), }; } catch { return null; @@ -57,6 +74,21 @@ export function confirmationCookie(value: string, path: string, secure: boolean) return parts.join('; '); } +function readCompanions(value: unknown): ConfirmationCompanion[] | null { + if (value === undefined) return []; + if (!Array.isArray(value) || value.length > 24) return null; + const companions: ConfirmationCompanion[] = []; + for (const item of value) { + if (!item || typeof item !== 'object') return null; + const record = item as Record; + if (typeof record.signupId !== 'number' || !Number.isSafeInteger(record.signupId) || record.signupId <= 0) return null; + if (typeof record.shiftId !== 'number' || !Number.isSafeInteger(record.shiftId) || record.shiftId <= 0) return null; + if (typeof record.token !== 'string' || !/^[A-Za-z0-9_-]{20,200}$/.test(record.token)) return null; + companions.push({ signupId: record.signupId, shiftId: record.shiftId, token: record.token }); + } + return companions; +} + function cookieKey(secret: string): Buffer { return createHash('sha256').update(`hope-confirm-v1\0${secret}`).digest(); } diff --git a/src/pages.tsx b/src/pages.tsx index daf77f9..dfab6cf 100644 --- a/src/pages.tsx +++ b/src/pages.tsx @@ -1,7 +1,7 @@ import type { Child } from 'hono/jsx'; import type { AppConfig } from './config.js'; import { WEEKDAY_SHORT, calendarMonth, calendarToday, chooseCalendarMonth, monthStep, type CalendarDay } from './calendar.js'; -import { spotsSentence, type PublicEvent, type PublicShift } from './schedule.js'; +import { spotsSentence, type PublicEvent, type PublicShift, type SignupDateChoice } from './schedule.js'; import { SIGNUP_TOO_EARLY, signupDeadlineLabel } from './signup-window.js'; import { partyChoiceLabel, partyChoices, type SignupErrors, type SignupValues } from './signup-form.js'; import type { MoveTarget } from './admin/store.js'; @@ -298,6 +298,8 @@ export type SignupFormView = { siteKey: string; values: SignupValues; errors: SignupErrors; + dates: SignupDateChoice[]; + selectedDateIds: number[]; }; export function ShiftPage(props: { @@ -320,12 +322,17 @@ export function ShiftPage(props: {

) : null} {props.signup ? ( - + ) : null} ); } +export type ConfirmationEntry = { + shift: PublicShift; + managePath: string | null; +}; + export function ConfirmationPage(props: { config: AppConfig; shift: PublicShift; @@ -333,7 +340,23 @@ export function ConfirmationPage(props: { managePath: string | null; groupSize?: number; cancelled?: boolean; + also?: ConfirmationEntry[]; }) { + const also = props.also ?? []; + if (also.length > 0 && !props.cancelled) { + const entries = [{ shift: props.shift, managePath: props.managePath }, ...also].sort((left, right) => { + const byTime = left.shift.startsAtUtc.localeCompare(right.shift.startsAtUtc); + return byTime === 0 ? left.shift.id - right.shift.id : byTime; + }); + return ( + + ); + } const shift = props.shift; const cancelled = Boolean(props.cancelled); const summary = cancelled @@ -387,6 +410,57 @@ export function ConfirmationPage(props: { ); } +function SeveralDatesReceipt(props: { + config: AppConfig; + name: string | null; + groupSize: number; + entries: ConfirmationEntry[]; +}) { + const count = props.entries.length; + const summary = props.name + ? `${props.name} is signed up for ${count} dates.` + : `These ${count} dates are saved.`; + return ( +
+
+ +
+

You're signed up

+

{summary}

+ {props.groupSize > 1 ? ( +

Each date holds {props.groupSize} spots.

+ ) : null} +
+
+
+

Shift information

+
    + {props.entries.map((entry) => { + const location = entry.shift.event.location.trim(); + return ( +
  • +

    {entry.shift.role}

    + + {location ?

    {location}

    : null} + {entry.managePath ? ( + {`${props.config.baseUrl}${entry.managePath}`} + ) : null} +
  • + ); + })} +
+

+ A confirmation email is on the way for each date. This browser keeps the links above for a few minutes. Use a link to cancel that date or switch it. +

+
+
+ ); +} + export type SwitchForm = { action: string; token: string; @@ -1148,12 +1222,24 @@ function Spots(props: { shift: PublicShift }) { return

{spotsSentence(props.shift)}

; } -function SignupForm(props: { config: AppConfig; shiftId: number; remaining: number; form: SignupFormView }) { +function SignupForm(props: { config: AppConfig; shiftId: number; eventId: number; remaining: number; form: SignupFormView }) { const form = props.form; const errors = form.errors; const errorEntries = Object.entries(errors).filter((entry): entry is [string, string] => Boolean(entry[1])); + const selected = new Set(form.selectedDateIds); + const chosen = form.dates.filter((date) => selected.has(date.id)); + const selectedRoom = chosen.length > 0 ? Math.min(...chosen.map((date) => date.remaining)) : props.remaining; + const choiceRoom = form.dates.length > 0 ? Math.max(selectedRoom, ...form.dates.map((date) => date.remaining)) : props.remaining; + const moreDates = form.dates.filter((date) => date.id !== props.shiftId); + const severalDates = moreDates.length > 0; + const extraCount = moreDates.filter((date) => selected.has(date.id)).length; return ( -