From 412b062409581aaf0cb9c0379a2ffb24d4889768 Mon Sep 17 00:00:00 2001 From: Timothy Wayne Gregg <5861166+CompleteDotTech@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:09:05 -0500 Subject: [PATCH 1/7] Add editing and audit history for saved QR codes --- README.md | 6 ++++ src/client/App.tsx | 83 ++++++++++++++++++++++++++++++++------------ tests/api.test.ts | 23 ++++++++++++ tests/editor.test.ts | 54 ++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 23 deletions(-) create mode 100644 tests/editor.test.ts diff --git a/README.md b/README.md index aa7f0e3..fbb95da 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,12 @@ In a second terminal, run `npm run dev:web`, then open **http://localhost:5173** Samples use example addresses. Supply your own destination, media, contact information, Wi-Fi credentials, or wallet address before using a QR code. Scan-test the final export, especially after changing contrast, shapes, or logos. +## Edit a saved QR code + +Select a saved QR code, then choose **Edit QR** to load its current content and design into the editor. Update the fields and choose **Update saved QR**; the record and its audit history refresh after saving. Select a content type or cancel editing to return to creating a new record. + +A dynamic QR keeps the same encoded link when its destination or landing content changes, so existing prints continue to work. A static QR encodes its content directly: changing that content requires a new print. Design changes also require a new print to appear on the printed code. + ## Build and run ```sh diff --git a/src/client/App.tsx b/src/client/App.tsx index e84a47b..931e7ef 100644 --- a/src/client/App.tsx +++ b/src/client/App.tsx @@ -3,7 +3,7 @@ import { useEffect, useMemo, useState } from 'react'; import { bodyShapes, defaultDesignConfig, eyeBalls, eyeFrames, logoGallery, templates } from '../qr/design'; import { getLogoIcon } from '../qr/logo-icons'; import { buildPayload } from '../qr/payloads'; -import type { ContentType, DesignConfig, QrMode, QrRecord } from '../shared/types'; +import type { AuditEvent, ContentType, DesignConfig, QrMode, QrRecord } from '../shared/types'; import { sampleContent } from './sample-content'; const contentTypes: ContentType[] = ['url', 'text', 'email', 'phone', 'sms', 'vcard', 'mecard', 'maps', 'facebook', 'twitter', 'youtube', 'wifi', 'event', 'crypto']; @@ -24,17 +24,14 @@ export function App() { const [error, setError] = useState(''); const [records, setRecords] = useState([]); const [selected, setSelected] = useState(null); + const [auditEvents, setAuditEvents] = useState([]); + const [editing, setEditing] = useState(false); const encodedPayload = useMemo(() => JSON.stringify(payload, null, 2), [payload]); - useEffect(() => { - setPayload(defaults[type]); - if (moreTypes.includes(type)) setMode('dynamic_payload'); - }, [type]); - useEffect(() => { void refreshPreview(); - }, [payload, design, size]); + }, [type, payload, design, size, editing, selected?.encodedPayload]); useEffect(() => { void loadRecords(); @@ -42,8 +39,7 @@ export function App() { async function refreshPreview() { try { - setError(''); - const data = await buildPreviewPayload(type, payload); + const data = editing && selected && selected.mode !== 'static' ? selected.encodedPayload : await buildPreviewPayload(type, payload); setDownloadData(data); const response = await fetch('/api/qr/custom', { method: 'POST', @@ -58,8 +54,8 @@ export function App() { } async function saveQr() { - const response = await fetch('/api/qr-codes', { - method: 'POST', + const response = await fetch(editing && selected ? `/api/qr-codes/${selected.id}` : '/api/qr-codes', { + method: editing && selected ? 'PATCH' : 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, mode, contentType: type, contentPayload: payload, designConfig: design, sizePx: size, keyword: keyword || undefined }) }); @@ -68,13 +64,45 @@ export function App() { return; } const qr = (await response.json()) as QrRecord; - setSelected(qr); + await loadDetail(qr.id); await loadRecords(); } async function loadRecords() { - const response = await fetch('/api/qr-codes'); - if (response.ok) setRecords(await response.json()); + try { + const response = await fetch('/api/qr-codes'); + if (!response.ok) throw new Error(response.status === 401 ? 'Admin token required. The bundled UI has no token login; configure access through your deployment.' : 'Could not load saved QR codes'); + setRecords(await response.json()); + } catch (err) { + setError(err instanceof Error ? err.message : 'Could not load saved QR codes'); + } + } + + async function loadDetail(id: string) { + try { + const response = await fetch(`/api/qr-codes/${id}`); + if (!response.ok) throw new Error((await response.json()).error || 'Could not load QR code'); + const detail: { qr: QrRecord; auditEvents: AuditEvent[] } = await response.json(); + setSelected(detail.qr); + setAuditEvents(detail.auditEvents); + return detail.qr; + } catch (err) { + setError(err instanceof Error ? err.message : 'Could not load QR code'); + return null; + } + } + + async function editRecord(id: string) { + const record = await loadDetail(id); + if (!record) return; + setType(record.contentType); + setMode(record.mode); + setName(record.name); + setPayload(record.contentPayload); + setDesign(record.designConfig); + setSize(record.sizePx); + setKeyword(record.yourlsKeyword || ''); + setEditing(true); } async function uploadLogo(file?: File) { @@ -111,7 +139,7 @@ export function App() {