171 lines
5.5 KiB
JavaScript
171 lines
5.5 KiB
JavaScript
import { submitDish, addDish, removeDish, pickDish, parseState, suggest, difficultyOf, pickByDifficulty } from "./logic.js";
|
|
import { catalog, LEVEL_KEYS } from "./catalog.js";
|
|
import { findRecipe } from "./recipes.js";
|
|
import { messages, pickLang } from "./i18n.js";
|
|
|
|
const KEY = "wtc:v1";
|
|
const t = messages[pickLang(navigator.language)];
|
|
const $ = (id) => document.getElementById(id);
|
|
|
|
let state = { dishes: [], last: null };
|
|
try { state = parseState(localStorage.getItem(KEY)); } catch {}
|
|
|
|
function save() {
|
|
try { localStorage.setItem(KEY, JSON.stringify(state)); } catch {}
|
|
}
|
|
|
|
function render() {
|
|
document.documentElement.lang = pickLang(navigator.language);
|
|
document.title = t.title;
|
|
$("title").textContent = t.title;
|
|
$("listTitle").textContent = t.yourDishes;
|
|
$("name").placeholder = t.placeholder;
|
|
$("nameLabel").textContent = t.nameLabel;
|
|
$("add").textContent = t.add;
|
|
$("pick").textContent = t.pick;
|
|
$("pick").disabled = state.dishes.length === 0;
|
|
|
|
const n = state.dishes.length;
|
|
$("hint").textContent = n === 0 ? t.emptyHint : n === 1 && state.last ? t.oneHint : "";
|
|
$("hint").hidden = !$("hint").textContent;
|
|
const show = state.last !== null;
|
|
$("today").hidden = $("dish").hidden = !show;
|
|
$("today").textContent = t.today;
|
|
$("dish").textContent = state.last ?? "";
|
|
|
|
$("recipeToday").hidden = !(show && findRecipe(state.last));
|
|
$("recipeToday").textContent = t.recipe;
|
|
$("recipeToday").setAttribute("aria-label", show ? t.recipeLabel.replace("{name}", state.last) : "");
|
|
const lvl = state.last ? difficultyOf(state.last) : null;
|
|
$("level").hidden = lvl === null;
|
|
$("level").textContent = lvl === null ? "" : t.difficulty.replace("{level}", t[LEVEL_KEYS[lvl]]);
|
|
$("tune").hidden = !show;
|
|
$("easier").textContent = t.easier;
|
|
$("harder").textContent = t.harder;
|
|
|
|
$("empty").textContent = t.listEmpty;
|
|
$("empty").hidden = n !== 0;
|
|
const ul = $("list");
|
|
ul.replaceChildren(...state.dishes.map((d) => {
|
|
const li = document.createElement("li");
|
|
const span = document.createElement("span");
|
|
span.textContent = d;
|
|
const b = document.createElement("button");
|
|
b.type = "button";
|
|
b.textContent = t.remove;
|
|
b.setAttribute("aria-label", t.removeLabel.replace("{name}", d));
|
|
b.addEventListener("click", () => {
|
|
state.dishes = removeDish(state.dishes, d);
|
|
if (state.last === d) state.last = null;
|
|
save(); render();
|
|
});
|
|
const tail = document.createElement("span");
|
|
tail.className = "actions";
|
|
if (findRecipe(d)) {
|
|
const r = document.createElement("button");
|
|
r.type = "button";
|
|
r.className = "recipe-btn";
|
|
r.textContent = t.recipe;
|
|
r.setAttribute("aria-label", t.recipeLabel.replace("{name}", d));
|
|
r.addEventListener("click", () => openRecipe(d, r));
|
|
tail.append(r);
|
|
} else {
|
|
const m = document.createElement("span");
|
|
m.className = "no-recipe";
|
|
m.textContent = t.noRecipe;
|
|
tail.append(m);
|
|
}
|
|
tail.append(b);
|
|
li.append(span, tail);
|
|
return li;
|
|
}));
|
|
}
|
|
|
|
let opener = null;
|
|
function openRecipe(name, from) {
|
|
const r = findRecipe(name);
|
|
if (!r) return;
|
|
opener = from;
|
|
$("recipeTitle").textContent = r.name;
|
|
$("back").textContent = t.back;
|
|
$("ingTitle").textContent = t.ingredients;
|
|
$("stepsTitle").textContent = t.steps;
|
|
const items = (arr) => arr.map((x) => Object.assign(document.createElement("li"), { textContent: x }));
|
|
$("ingredients").replaceChildren(...items(r.ingredients));
|
|
$("steps").replaceChildren(...items(r.steps));
|
|
$("home").hidden = true;
|
|
$("recipe").hidden = false;
|
|
window.scrollTo(0, 0);
|
|
$("recipe").focus();
|
|
}
|
|
|
|
$("back").addEventListener("click", () => {
|
|
$("recipe").hidden = true;
|
|
$("home").hidden = false;
|
|
if (opener && opener.isConnected) opener.focus();
|
|
});
|
|
|
|
$("recipeToday").addEventListener("click", () => openRecipe(state.last, $("recipeToday")));
|
|
|
|
function showNameError(key) {
|
|
$("nameError").textContent = key ? t[key] : "";
|
|
$("nameError").hidden = !key;
|
|
if (key) $("name").setAttribute("aria-invalid", "true");
|
|
else $("name").removeAttribute("aria-invalid");
|
|
}
|
|
|
|
$("form").addEventListener("submit", (e) => {
|
|
e.preventDefault();
|
|
const r = submitDish(state.dishes, $("name").value, catalog);
|
|
showNameError(r.error);
|
|
$("name").focus();
|
|
if (r.error) return;
|
|
state.dishes = r.dishes;
|
|
$("name").value = "";
|
|
save(); renderSuggest(); render();
|
|
});
|
|
|
|
function renderSuggest() {
|
|
const items = suggest($("name").value, state.dishes, catalog);
|
|
$("suggest").hidden = items.length === 0;
|
|
$("suggest").replaceChildren(...items.map((name) => {
|
|
const li = document.createElement("li");
|
|
const b = document.createElement("button");
|
|
b.type = "button";
|
|
b.textContent = name;
|
|
b.addEventListener("click", () => {
|
|
state.dishes = addDish(state.dishes, name);
|
|
$("name").value = "";
|
|
showNameError(null);
|
|
save(); renderSuggest(); render();
|
|
$("name").focus();
|
|
});
|
|
li.append(b);
|
|
return li;
|
|
}));
|
|
}
|
|
|
|
$("name").addEventListener("input", () => { showNameError(null); renderSuggest(); });
|
|
|
|
function showTuneError(key) {
|
|
$("tuneError").textContent = key ? t[key] : "";
|
|
$("tuneError").hidden = !key;
|
|
}
|
|
|
|
function tune(dir) {
|
|
const r = pickByDifficulty(state.dishes, state.last, dir);
|
|
state.last = r.dish;
|
|
save(); render();
|
|
showTuneError(r.error);
|
|
}
|
|
$("easier").addEventListener("click", () => tune(-1));
|
|
$("harder").addEventListener("click", () => tune(1));
|
|
|
|
$("pick").addEventListener("click", () => {
|
|
showTuneError(null);
|
|
state.last = pickDish(state.dishes, state.last);
|
|
save(); render();
|
|
});
|
|
|
|
render();
|