Add cook later

This commit is contained in:
Joren de Graaf
2026-10-15 19:58:33 +02:00
parent b72283818c
commit f36f05b9bc
27 changed files with 523 additions and 129 deletions
+1
View File
@@ -29,3 +29,4 @@ body[data-theme=dark] .recipe-match__image-fallback{color:#253029}body[data-them
.recipe-match__reset button:hover{border-color:#a44cbe}
@media(min-width:821px){.recipe-match__more-filters[open]>summary{display:none}}
@media(max-width:600px){.recipe-match__filter-panel{padding:12px}.recipe-match__filter-panel .recipe-match__filters{padding-top:7px}.recipe-match__search-row button{padding-inline:12px}}
.recipe-match__header .recipes-mode{margin-top:16px}.recipe-match__stage{min-height:0}.recipe-match__card-body>a{display:inline-block;margin-top:12px;color:#235943;font-weight:700}body[data-theme=dark] .recipe-match__card-body>a{color:#a7d9b6}
+23 -55
View File
@@ -3,26 +3,21 @@
if (!deck) return;
const cards = [...deck.querySelectorAll('[data-match-card]')];
const skip = document.querySelector('[data-match-skip]');
const select = document.querySelector('[data-match-select]');
const save = document.querySelector('[data-match-select]');
const status = document.querySelector('[data-match-status]');
const counter = document.querySelector('[data-match-counter]');
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
let index = 0;
let busy = false;
let drag = null;
function current() { return cards[index]; }
let index = 0, busy = false, drag = null;
const current = () => cards[index];
function update() {
cards.forEach((card, position) => { card.hidden = position !== index; });
if (counter) counter.textContent = index < cards.length ? `Recipe ${index + 1} of ${cards.length}` : (cards.length ? 'You have seen all matching recipes' : '');
if (skip) skip.disabled = index >= cards.length;
if (select) select.disabled = index >= cards.length;
if (index >= cards.length && cards.length) status.textContent = 'That’s all for now. Change the filters to explore again.';
if (counter) counter.textContent = index < cards.length ? `Recipe ${index + 1} of ${cards.length}` : 'No more recipes in this set';
if (skip) skip.disabled = busy || index >= cards.length;
if (save) save.disabled = busy || index >= cards.length;
if (index >= cards.length && cards.length) status.textContent = 'You have seen all matching recipes. Change the filters to explore again.';
}
function advance(direction) {
const card = current();
if (!card) return;
const moveFocus = card.contains(document.activeElement);
card.style.setProperty('--match-exit', direction === 'left' ? '-110%' : '110%');
card.style.setProperty('--match-tilt', direction === 'left' ? '-8deg' : '8deg');
card.classList.add('is-leaving');
@@ -32,84 +27,57 @@
index += 1;
busy = false;
update();
if (moveFocus) {
if (skip && !skip.disabled) skip.focus(); else status.focus();
}
if (save && !save.disabled) save.focus(); else status.focus();
}, reduced.matches ? 0 : 230);
}
function confetti(card) {
if (reduced.matches) return;
const area = card.querySelector('.recipe-match__confetti');
const colors = ['#f84d7c', '#ffa62b', '#24ac61', '#8d53df'];
for (let i = 0; i < 25; i++) {
const piece = document.createElement('i');
piece.style.setProperty('--confetti-color', colors[i % colors.length]);
piece.style.setProperty('--confetti-x', `${Math.round((Math.random() - .5) * 300)}px`);
piece.style.setProperty('--confetti-y', `${Math.round((Math.random() - .6) * 330)}px`);
area.append(piece);
}
window.setTimeout(() => area.replaceChildren(), 900);
}
function skipCurrent() {
if (busy || !current()) return;
busy = true;
status.textContent = `Skipped ${current().querySelector('h2').textContent}.`;
advance('left');
update();
advance('right');
}
async function selectCurrent() {
async function saveCurrent() {
if (busy || !current()) return;
busy = true;
update();
const card = current();
status.textContent = `Saving ${card.querySelector('h2').textContent}…`;
try {
const response = await fetch(card.dataset.selectUrl, {
method: 'POST', credentials: 'same-origin',
headers: {'X-CSRFToken': document.getElementById('recipe-match-csrf').value, 'Accept': 'application/json'},
});
if (!response.ok) throw new Error('Could not save your choice. Please try again.');
if (!response.ok) throw new Error('Could not save this recipe. Please try again.');
const result = await response.json();
if (result.matched) {
card.querySelector('[data-match-celebration]').hidden = false;
status.textContent = `It's a match! The demo account selected ${result.title} too.`;
if (skip) skip.disabled = true;
if (select) select.disabled = true;
confetti(card);
card.querySelector('[data-match-continue]').focus();
} else {
status.textContent = `Selected ${result.title}.`;
advance('right');
}
status.textContent = `${result.title} added to Cook Later.`;
card.querySelector('.recipe-match__kind').textContent = 'Saved in Cook Later';
advance('left');
} catch (error) {
status.textContent = error.message;
busy = false;
update();
}
}
deck.addEventListener('click', event => {
if (event.target.closest('[data-match-continue]')) {
current().querySelector('[data-match-celebration]').hidden = true;
advance('right');
}
});
deck.addEventListener('pointerdown', event => {
if (busy || !current() || event.target.closest('button,a')) return;
drag = {id: event.pointerId, x: event.clientX, y: event.clientY};
});
deck.addEventListener('pointerup', event => {
if (!drag || drag.id !== event.pointerId || busy) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
const dx = event.clientX - drag.x, dy = event.clientY - drag.y;
drag = null;
current().style.transform = '';
if (Math.abs(dx) < 75 || Math.abs(dx) < Math.abs(dy) * 1.3) return;
if (dx < 0) skipCurrent(); else selectCurrent();
if (dx < 0) saveCurrent(); else skipCurrent();
});
deck.addEventListener('pointercancel', () => { drag = null; if (current()) current().style.transform = ''; });
deck.addEventListener('pointermove', event => {
if (!drag || drag.id !== event.pointerId || !current()) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
const dx = event.clientX - drag.x, dy = event.clientY - drag.y;
if (Math.abs(dx) > Math.abs(dy) * 1.3) current().style.transform = `translateX(${Math.max(-35, Math.min(35, dx / 4))}px) rotate(${Math.max(-3, Math.min(3, dx / 40))}deg)`;
});
if (skip) skip.addEventListener('click', skipCurrent);
if (select) select.addEventListener('click', selectCurrent);
skip?.addEventListener('click', skipCurrent);
save?.addEventListener('click', saveCurrent);
update();
})();
+7
View File
@@ -33,3 +33,10 @@ body[data-theme=dark] .recipes-markdown-editor .toastui-editor-defaultUI,body[da
@media(min-width:821px){.recipes-filter-details[open]>summary{display:none}}
.recipes-filter-details summary::after{content:'⌄';margin-left:auto;font-size:1.2rem;line-height:1}
.recipes-filter-details[open] summary::after{transform:rotate(180deg)}
.recipes-mode{display:flex;width:max-content;max-width:100%;margin:12px 0 16px;padding:4px;border:1px solid var(--recipe-line,#d0d8d0);border-radius:12px;background:var(--recipe-surface,#fff)}
.recipes-mode a{padding:8px 20px;border-radius:9px;color:inherit;text-decoration:none;font-weight:700}
.recipes-mode a[aria-current=page]{background:#235943;color:#fff}
.recipes-mode a:focus-visible,.cook-later :is(a,button):focus-visible{outline:3px solid #a44cbe;outline-offset:3px}
.recipes-saved-badge{align-self:center;padding:5px 8px;border-radius:99px;background:#e1f2e8;color:#235943;font-size:.72rem;font-weight:700}
.cook-later{max-width:850px;margin:0 auto;padding:20px;color:var(--recipe-ink,#25292e)}.cook-later h1{margin:15px 0 5px;font-family:Fraunces,Georgia,serif}.cook-later__list{display:grid;gap:12px;margin-top:24px}.cook-later__item{display:flex;align-items:center;justify-content:space-between;gap:20px;padding:18px;border:1px solid var(--recipe-line,#d0d8d0);border-radius:14px;background:var(--recipe-surface,#fff)}.cook-later__item h2{margin:0;font-size:1.15rem}.cook-later__item p{margin:6px 0}.cook-later__item small{color:var(--recipe-muted,#687178)}.cook-later__item button{min-height:44px;padding:8px 14px;border:1px solid #b94a55;border-radius:9px;background:transparent;color:#a12435;cursor:pointer}body[data-theme=dark] .cook-later__item button{color:#ffb3bb}body[data-theme=dark] .recipes-saved-badge{background:#294d38;color:#b7e8c5}@media(max-width:600px){.cook-later__item{align-items:flex-start;flex-direction:column}}
body[data-theme=dark] .cook-later{--recipe-ink:#eaf0ea;--recipe-muted:#afbbb2;--recipe-surface:#202a23;--recipe-line:#435046}body[data-theme=dark] .cook-later a{color:#a7d9b6}