Express + node:sqlite server, vanilla HTML/JS frontend. Daily puzzle by assigned date, puzzle list, shuffle, one-away hint, shareable emoji results, and a password-protected admin page to create and edit puzzles. Answers are checked server-side and never sent before they are solved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
109 lines
5.8 KiB
JavaScript
109 lines
5.8 KiB
JavaScript
const COLORS = ['yellow', 'green', 'blue', 'purple'];
|
|
const $app = document.getElementById('app');
|
|
let games = [];
|
|
|
|
const icon = (name) => el('i', { class: `ph-bold ph-${name}`, 'aria-hidden': 'true' });
|
|
const errorList = (errors) => el('ul', { class: 'errors', role: 'alert' }, errors.map((e) => el('li', {}, e)));
|
|
|
|
async function showList() {
|
|
const { ok, status, data } = await api('/api/admin/games');
|
|
if (status === 401) return showLogin();
|
|
if (!ok) return $app.replaceChildren(el('div', { class: 'empty' }, 'Could not load puzzles. Try refreshing.'));
|
|
games = data;
|
|
|
|
const items = games.length
|
|
? games.map((g) => el('li', {}, el('div', { class: 'item' },
|
|
el('div', {}, el('strong', {}, g.title), el('div', { class: 'meta' }, g.date ? fmtDate(g.date) : 'No date (list only)')),
|
|
el('div', { class: 'row-actions' },
|
|
el('a', { class: 'btn icon-btn', href: `/play/${g.id}`, 'aria-label': `Play ${g.title}` }, icon('arrow-right')),
|
|
el('button', { class: 'btn icon-btn', 'aria-label': `Edit ${g.title}`, onclick: () => showEditor(g) }, icon('pencil-simple')),
|
|
el('button', { class: 'btn icon-btn danger', 'aria-label': `Delete ${g.title}`, onclick: () => remove(g) }, icon('trash'))))))
|
|
: [el('li', { class: 'empty' }, 'No puzzles yet. Create your first one.')];
|
|
|
|
$app.replaceChildren(
|
|
el('h1', {}, 'Your puzzles'),
|
|
el('p', { class: 'sub' }, 'Dated puzzles appear on their day. Future ones stay hidden until then.'),
|
|
el('div', { class: 'actions', style: 'justify-content:flex-start;margin-bottom:20px' },
|
|
el('button', { class: 'btn primary', onclick: () => showEditor() }, icon('plus'), 'New puzzle'),
|
|
el('button', { class: 'btn', onclick: logout }, icon('sign-out'), 'Log out')),
|
|
el('ul', { class: 'list' }, items),
|
|
);
|
|
}
|
|
|
|
function showLogin(error) {
|
|
const input = el('input', { id: 'pw', type: 'password', autocomplete: 'current-password', required: true });
|
|
$app.replaceChildren(
|
|
el('h1', {}, 'Admin login'),
|
|
el('p', { class: 'sub' }, 'Only the puzzle editor needs this.'),
|
|
el('form', { onsubmit: async (e) => {
|
|
e.preventDefault();
|
|
const btn = e.submitter; btn.disabled = true;
|
|
const { ok, data } = await api('/api/login', { method: 'POST', body: { password: input.value } });
|
|
ok ? showList() : showLogin(data.error || 'Login failed.');
|
|
} },
|
|
el('div', { class: 'field' }, el('label', { for: 'pw' }, 'Password'), input,
|
|
error ? el('p', { class: 'help', style: 'color:var(--error)', role: 'alert' }, error) : null),
|
|
el('button', { class: 'btn primary', type: 'submit' }, 'Log in')),
|
|
);
|
|
input.focus();
|
|
}
|
|
|
|
function showEditor(game) {
|
|
const g = game || { title: '', date: '', groups: COLORS.map((color) => ({ name: '', color, words: ['', '', '', ''] })) };
|
|
const field = (id, label, input, help) =>
|
|
el('div', { class: 'field' }, el('label', { for: id }, label), input, help ? el('p', { class: 'help' }, help) : null);
|
|
const title = el('input', { id: 'title', value: g.title, maxlength: 100, required: true });
|
|
const date = el('input', { id: 'date', type: 'date', value: g.date || '' });
|
|
const errBox = el('div');
|
|
|
|
const groupEls = g.groups.map((grp, i) => {
|
|
const n = i + 1;
|
|
const name = el('input', { id: `g${n}-name`, value: grp.name, maxlength: 80, required: true });
|
|
const color = el('select', { id: `g${n}-color` }, COLORS.map((c) => el('option', { value: c, selected: c === grp.color }, c[0].toUpperCase() + c.slice(1))));
|
|
const words = grp.words.map((w, j) => el('input', { value: w, maxlength: 30, required: true, 'aria-label': `Group ${n} word ${j + 1}` }));
|
|
const box = el('fieldset', { class: 'group-edit' },
|
|
el('legend', { class: 'sr' }, `Group ${n}`),
|
|
el('div', { class: 'head' },
|
|
field(`g${n}-name`, el('span', {}, el('span', { class: `swatch ${grp.color}` }), `Group ${n} name`), name),
|
|
field(`g${n}-color`, 'Color', color)),
|
|
el('label', {}, 'Words'), el('div', { class: 'words' }, words));
|
|
color.addEventListener('change', () => { box.querySelector('.swatch').className = `swatch ${color.value}`; });
|
|
return { box, read: () => ({ name: name.value, color: color.value, words: words.map((w) => w.value) }) };
|
|
});
|
|
|
|
$app.replaceChildren(
|
|
el('a', { href: '/admin', class: 'btn', style: 'margin-bottom:20px', onclick: (e) => { e.preventDefault(); showList(); } }, icon('arrow-left'), 'Back'),
|
|
el('h1', {}, game ? 'Edit puzzle' : 'New puzzle'),
|
|
el('p', { class: 'sub' }, 'Yellow is the easiest group, purple the trickiest.'),
|
|
el('form', { novalidate: true, onsubmit: async (e) => {
|
|
e.preventDefault();
|
|
const btn = e.submitter; btn.disabled = true;
|
|
const body = { title: title.value, date: date.value || null, groups: groupEls.map((x) => x.read()) };
|
|
const { ok, data } = await api(game ? `/api/admin/games/${game.id}` : '/api/admin/games', { method: game ? 'PUT' : 'POST', body });
|
|
btn.disabled = false;
|
|
if (ok) { toast('Saved'); return showList(); }
|
|
if (data.error) return showLogin(data.error);
|
|
errBox.replaceChildren(errorList(data.errors || ['Could not save.']));
|
|
errBox.scrollIntoView({ block: 'center' });
|
|
} },
|
|
errBox,
|
|
field('title', 'Title', title),
|
|
field('date', 'Date', date, 'Optional. The puzzle becomes the daily puzzle on this date.'),
|
|
groupEls.map((x) => x.box),
|
|
el('div', { class: 'actions', style: 'justify-content:flex-start' }, el('button', { class: 'btn primary', type: 'submit' }, icon('check'), 'Save puzzle'))),
|
|
);
|
|
}
|
|
|
|
async function remove(g) {
|
|
if (!confirm(`Delete "${g.title}"? This cannot be undone.`)) return;
|
|
const { ok } = await api(`/api/admin/games/${g.id}`, { method: 'DELETE' });
|
|
toast(ok ? 'Deleted' : 'Delete failed');
|
|
showList();
|
|
}
|
|
|
|
async function logout() {
|
|
await api('/api/logout', { method: 'POST' });
|
|
showLogin();
|
|
}
|
|
|
|
showList();
|