Merge !105: improvements for qr code check-in feature

This commit is contained in:
Duarte Santos committed 2026-09-07 19:38:15 +02:00
commit 1680465ca2
20 files changed
+387 -52

No files matched your search

+113 -48
View File
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react'
import { useLayoutEffect, useRef, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { useStore } from '../store/useStore.js'
import { useUI } from '../store/useUI.js'
@@ -12,25 +12,60 @@ import QrCanvas from '../components/QrCanvas.jsx'
import CameraScan from '../components/CameraScan.jsx'
import { Button, TextField } from '../components/ui.jsx'
import { confirmSheet } from '../sheets.jsx'
import { tappable } from '../lib/use-sheet-keyboard.js'
// Gym check-in (reached from the Home "Check in" card; app and PWA alike). Shows each saved membership
// code as a QR the turnstile can read, swiped through horizontally, with a trailing "+" to add
// another. We only ever store the code's value + symbology; the QR is regenerated from it here
// every time (see lib/qr.js), so nothing sensitive is kept as an image.
// Move the card at `from` to sit at index `to`, returning a new array (the input is left alone).
// Backs the on-card ◀ ▶ reorder buttons. Out-of-range or no-op moves return an unchanged copy
// rather than throwing, so a button at either end is simply inert.
export function moveGymCard(cards, from, to) {
const next = [...cards]
if (from < 0 || from >= next.length) return next
const target = Math.max(0, Math.min(to, next.length - 1))
if (target === from) return next
const [moved] = next.splice(from, 1)
next.splice(target, 0, moved)
return next
}
// Gym check-in (reached from the Home "Check in" card; app and PWA alike). Shows each saved
// membership code as a QR the turnstile can read, swiped through horizontally, with a trailing
// "+" to add another. We only ever store the code's value + symbology; the QR is regenerated from
// it here every time (see lib/qr.js), so nothing sensitive is kept as an image.
//
// The rail reopens on the card you used last (lastGymCardId) and remembers the one you settle on,
// so a member with two gyms lands on the right code without swiping. Cards are added by scanning
// or importing a photo only — never by typing a code, which is too easy to fat-finger into a
// code that silently fails at the turnstile. An existing card can be edited (rename, re-scan), and
// with more than one card each carries ◀ ▶ buttons to nudge its position along the rail.
export default function CheckIn() {
const nav = useNavigate()
const cards = useStore(s => s.S.gymCards) || []
const lastId = useStore(s => s.S.lastGymCardId)
const update = useStore(s => s.update)
const railRef = useRef(null)
const [active, setActive] = useState(0)
// Which card the horizontal scroll has settled on — drives the little dots. Reading it off the
// scroll position keeps the rail itself the single control; no separate paging state to sync.
// Open on the last-used card. Done once on mount (and whenever the saved id changes from
// elsewhere) with layout effect so the jump happens before paint — no visible scroll from 0.
// A stale id (its card was removed) falls back to the first card.
useLayoutEffect(() => {
const rail = railRef.current
if (!rail || !cards.length) return
const idx = Math.max(0, cards.findIndex(c => c.id === lastId))
rail.scrollLeft = idx * rail.clientWidth
setActive(idx)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [lastId, cards.length])
// Which card the horizontal scroll has settled on — drives the dots and gets remembered as the
// last-used card. Reading it off the scroll position keeps the rail itself the single control.
const onScroll = () => {
const rail = railRef.current
if (!rail) return
const i = Math.round(rail.scrollLeft / rail.clientWidth)
if (i === active) return
setActive(i)
const card = cards[i]
if (card && card.id !== lastId) update(s => { s.lastGymCardId = card.id }, false)
}
return <div className="narrow">
@@ -43,7 +78,12 @@ export default function CheckIn() {
</div>
<div className="ci-rail" ref={railRef} onScroll={onScroll}>
{cards.map(card => <CardFace key={card.id} card={card} />)}
{cards.map((card, i) => <CardFace
key={card.id}
card={card}
index={i}
count={cards.length}
/>)}
<button className="ci-add" onClick={openAddCard}>
<Icon name="plus" />
<span className="ci-add-t">{t('Add a card')}</span>
@@ -57,74 +97,102 @@ export default function CheckIn() {
</div>}
{!cards.length && <div className="muted small" style={{ textAlign: 'center', marginTop: 18, lineHeight: 1.5 }}>
{t('Type the number on your membership card, import a photo of it, or scan it with the camera. No extra app needed at the gym — just open this screen.')}
{t('Import a photo of your membership card or scan it with the camera. No extra app needed at the gym — just open this screen.')}
</div>}
</div>
}
// One saved card: its label, the QR, and the raw value underneath (handy when a reader is fussy
// and a staff member types it in). Long-press / the trash button removes it.
function CardFace({ card }) {
// One saved card: its label centered above the QR, and the raw value underneath (handy when a
// reader is fussy and a staff member types it in). The edit button opens the same sheet used to
// add a card, pre-filled; the trash button removes it after a confirm. With more than one card, a
// row of ◀ ▶ buttons nudges this card one slot along the rail — a plain, reliable reorder that
// doesn't fight the rail's horizontal scroll the way a drag gesture did.
function CardFace({ card, index, count }) {
const update = useStore(s => s.update)
const remove = () => confirmSheet({
title: t('Remove this card?'),
message: card.label,
confirmText: t('Remove'),
danger: true,
onConfirm: () => update(s => { s.gymCards = (s.gymCards || []).filter(c => c.id !== card.id) }),
onConfirm: () => update(s => {
s.gymCards = (s.gymCards || []).filter(c => c.id !== card.id)
if (s.lastGymCardId === card.id) s.lastGymCardId = (s.gymCards[0]?.id) || null
}),
})
const move = to => update(s => { s.gymCards = moveGymCard(s.gymCards, index, to) })
return <div className="ci-card">
<div className="row between" style={{ width: '100%', alignItems: 'flex-start' }}>
<div className="ci-label" style={{ flex: 1 }}>{card.label}</div>
<button className="iconbtn" style={{ width: 30, height: 30, fontSize: 14, color: 'var(--red)' }} onClick={remove} aria-label={t('Remove')}><Icon name="trash" /></button>
<div className="ci-card-hd">
<button className="iconbtn ci-card-btn" onClick={() => openEditCard(card)} aria-label={t('Edit')}><Icon name="pencil" /></button>
<div className="ci-label">{card.label}</div>
<button className="iconbtn ci-card-btn" style={{ color: 'var(--red)' }} onClick={remove} aria-label={t('Remove')}><Icon name="trash" /></button>
</div>
<div className="ci-qr-plate"><QrCanvas value={card.value} size={230} /></div>
<div className="ci-value">{card.value}</div>
{count > 1 && <div className="ci-reorder">
<button className="iconbtn ci-card-btn" onClick={() => move(index - 1)} disabled={index === 0} aria-label={t('Move left')}><Icon name="chevronLeft" /></button>
<span className="ci-pos">{index + 1} / {count}</span>
<button className="iconbtn ci-card-btn" onClick={() => move(index + 1)} disabled={index === count - 1} aria-label={t('Move right')}><Icon name="chevronRight" /></button>
</div>}
</div>
}
/* ------------------------------------------------------------- add-card sheet -- */
/* ------------------------------------------------------------- add/edit sheet -- */
// Opens the add-card sheet. Co-located with the view (not sheets.jsx) because it pulls in the
// scanner paths — keeping it here keeps them out of the shared sheets module.
export function openAddCard() {
useUI.getState().openSheet(close => <AddCard close={close} />)
useUI.getState().openSheet(close => <CardSheet close={close} />)
}
function saveCard({ label, value, fmt }) {
const update = useStore.getState().update
const toast = useUI.getState().toast
const trimmed = (value || '').trim()
if (!trimmed) { toast(t('Nothing to save yet')); return false }
// We can read many barcode kinds but only redraw QR faithfully — refuse anything else rather
// than store a code that would display as the wrong bars at the turnstile.
if (fmt && !canRenderFmt(fmt)) { toast(t("That's not a QR code — only QR cards can be shown here")); return false }
update(s => {
if (!Array.isArray(s.gymCards)) s.gymCards = []
s.gymCards.push({ id: uid(), label: (label || '').trim() || t('Gym card'), value: trimmed, fmt: 'qrcode' })
})
toast(t('Card added'))
return true
// Opens the same sheet on an existing card to rename it or re-scan its code.
export function openEditCard(card) {
useUI.getState().openSheet(close => <CardSheet close={close} card={card} />)
}
function AddCard({ close }) {
const [label, setLabel] = useState('')
const [value, setValue] = useState('')
// Shared sheet for adding and editing a card. Without `card` it creates a new one; with `card` it
// updates that entry in place. The code always comes from a scan or a photo — there is no text
// field for it — so an edit that only renames keeps the existing scanned code untouched.
function CardSheet({ close, card }) {
const editing = !!card
const [label, setLabel] = useState(card?.label || '')
const [value, setValue] = useState(card?.value || '')
const [busy, setBusy] = useState(false)
const fileRef = useRef(null)
const toast = useUI(s => s.toast)
const commitTyped = () => { if (saveCard({ label, value, fmt: 'qrcode' })) close() }
const commit = () => {
const trimmed = value.trim()
if (!trimmed) { toast(t('Scan or import a code first')); return }
const update = useStore.getState().update
if (editing) {
update(s => {
const c = (s.gymCards || []).find(x => x.id === card.id)
if (!c) return
c.label = label.trim() || t('Gym card')
c.value = trimmed
})
toast(t('Card updated'))
} else {
update(s => {
if (!Array.isArray(s.gymCards)) s.gymCards = []
const id = uid()
s.gymCards.push({ id, label: label.trim() || t('Gym card'), value: trimmed, fmt: 'qrcode' })
s.lastGymCardId = id
})
toast(t('Card added'))
}
close()
}
// Camera scan: in the app, hands off to the native scanner; in a browser, opens our own camera
// sheet on top of this one. Either way the value drops straight into the form so the user can
// still name it before saving.
const doScan = async () => {
if (!MOBILE) {
useUI.getState().openSheet(close => <CameraScan
onCancel={close}
useUI.getState().openSheet(closeCam => <CameraScan
onCancel={closeCam}
onFound={code => {
close()
closeCam()
if (!canRenderFmt(code.fmt)) { toast(t("That's not a QR code — only QR cards can be shown here")); return }
setValue(code.value)
}} />)
@@ -157,23 +225,20 @@ function AddCard({ close }) {
}
return <>
<h3>{t('Add a card')}</h3>
<div className="muted small" style={{ marginBottom: 14 }}>{t('Scan it, import a photo, or type the code by hand.')}</div>
<h3>{editing ? t('Edit card') : t('Add a card')}</h3>
<div className="muted small" style={{ marginBottom: 14 }}>{t('Scan it with the camera or import a photo.')}</div>
<div className="row" style={{ gap: 8, marginBottom: 16 }}>
<Button variant="tinted" icon="camera" onClick={doScan} disabled={busy}>{t('Scan')}</Button>
<Button variant="tinted" icon="camera" onClick={doScan} disabled={busy}>{editing ? t('Re-scan') : t('Scan')}</Button>
<Button variant="tinted" icon="image" onClick={() => fileRef.current?.click()} disabled={busy}>{t('Import photo')}</Button>
</div>
<label className="sect-t">{t('Label')}</label>
<TextField value={label} onChange={e => setLabel(e.target.value)} placeholder={t('e.g. FitZone downtown')} style={{ marginBottom: 12 }} />
<label className="sect-t">{t('Code')}</label>
<TextField value={value} onChange={e => setValue(e.target.value)} placeholder={t('Membership number or code')} inputMode="text" style={{ marginBottom: 16 }} />
<TextField value={label} onChange={e => setLabel(e.target.value)} placeholder={t('e.g. FitZone downtown')} style={{ marginBottom: 16 }} />
{value.trim() && <div className="ci-qr-plate" style={{ alignSelf: 'center', marginBottom: 16 }}><QrCanvas value={value.trim()} size={150} /></div>}
<Button variant="primary" onClick={commitTyped} disabled={busy || !value.trim()}>{t('Save card')}</Button>
<Button variant="primary" onClick={commit} disabled={busy || !value.trim()}>{editing ? t('Save card') : t('Save card')}</Button>
<input ref={fileRef} type="file" accept="image/*" hidden onChange={onFile} />
</>
@@ -0,0 +1,58 @@
// @vitest-environment happy-dom
// @vitest-environment happy-dom
// moveGymCard backs the on-card ◀ ▶ reorder buttons: it moves the card at `from` to index `to`,
// returning a new array. These tests cover that contract — a move is a splice, the input is never
// mutated, and degenerate moves (in place, out of range, past the ends) are safe no-ops, so a
// button at either end of the rail is simply inert.
//
// moveGymCard itself is pure, but importing CheckIn.jsx loads the store (which registers a
// visibilitychange listener at module load) and the scanner/sheet modules, so the file runs under
// happy-dom with those heavy imports stubbed. The function under test touches none of them.
import { describe, expect, it, vi } from 'vitest'
vi.mock('../lib/scan.js', () => ({ scanCode: vi.fn(), importCodeFromImage: vi.fn() }))
vi.mock('../components/CameraScan.jsx', () => ({ default: () => null }))
vi.mock('../sheets.jsx', () => ({ confirmSheet: vi.fn() }))
import { moveGymCard } from './CheckIn.jsx'
const cards = (...ids) => ids.map(id => ({ id, label: id.toUpperCase(), value: id, fmt: 'qrcode' }))
const ids = list => list.map(c => c.id)
describe('moveGymCard', () => {
it('moves a card forward to a later slot', () => {
expect(ids(moveGymCard(cards('a', 'b', 'c', 'd'), 0, 2))).toEqual(['b', 'c', 'a', 'd'])
})
it('moves a card backward to an earlier slot', () => {
expect(ids(moveGymCard(cards('a', 'b', 'c', 'd'), 3, 1))).toEqual(['a', 'd', 'b', 'c'])
})
it('is a permutation — no card is lost or duplicated', () => {
const before = cards('a', 'b', 'c', 'd', 'e')
const after = moveGymCard(before, 4, 0)
expect(ids(after).sort()).toEqual(['a', 'b', 'c', 'd', 'e'])
expect(after).toHaveLength(before.length)
})
it('leaves the array unchanged when dropped in place', () => {
expect(ids(moveGymCard(cards('a', 'b', 'c'), 1, 1))).toEqual(['a', 'b', 'c'])
})
it('clamps a target past the end to the last slot', () => {
expect(ids(moveGymCard(cards('a', 'b', 'c'), 0, 99))).toEqual(['b', 'c', 'a'])
})
it('ignores an out-of-range source without throwing', () => {
expect(ids(moveGymCard(cards('a', 'b'), 5, 0))).toEqual(['a', 'b'])
})
it('does not mutate the input array', () => {
const before = cards('a', 'b', 'c')
const snapshot = ids(before)
moveGymCard(before, 0, 2)
expect(ids(before)).toEqual(snapshot)
})
})