Écrans branchés : planning du jour, semaine, activités, poids, données, profil, analyses, propositions avec aperçu, gamification
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
077f2dc58d
commit
41d5e44daa
@@ -0,0 +1,188 @@
|
||||
// Activités : sports en dehors du programme (foot, escalade…), avec leurs zones
|
||||
// sollicitées, leur intensité et leur durée. Elles font partie du programme :
|
||||
// les modifier adopte une nouvelle version du programme (ProgramAdopted, source
|
||||
// « app »), qui garde tout le reste à l'identique.
|
||||
|
||||
import { h, paintWhenIdle, replace } from "../dom.js";
|
||||
import { ZONES, INTENSITIES } from "../../domain/vocabulary.js";
|
||||
import { mondayOf } from "../../domain/dates.js";
|
||||
import { INTENSITY_LABELS, ZONE_LABELS, withoutActivity } from "../home-model.js";
|
||||
import { screenHeader } from "../layout.js";
|
||||
import { href } from "../router.js";
|
||||
import { confirmDialog, toast } from "../feedback.js";
|
||||
|
||||
/** @typedef {import("../../domain/program.js").Activity} Activity */
|
||||
/** @typedef {import("../../domain/program.js").Program} Program */
|
||||
|
||||
/**
|
||||
* Identifiant d'une activité tiré de son nom : minuscules, chiffres et tirets.
|
||||
* @param {string} name
|
||||
* @param {string[]} taken
|
||||
*/
|
||||
export function activityId(name, taken) {
|
||||
const base = name.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 28) || "activite";
|
||||
let id = base, n = 2;
|
||||
while (taken.includes(id)) id = `${base}-${n++}`;
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Le programme avec une activité ajoutée, remplacée ou retirée. Les autres
|
||||
* champs du programme sont gardés tels quels.
|
||||
* @param {Program} program
|
||||
* @param {{id: string, activity: Activity|null}} change activity null : retrait
|
||||
* @returns {Program}
|
||||
*/
|
||||
export function withActivity(program, { id, activity }) {
|
||||
const list = program.activities ?? [];
|
||||
const next = activity
|
||||
? (list.some((a) => a.id === id) ? list.map((a) => (a.id === id ? activity : a)) : [...list, activity])
|
||||
: list.filter((a) => a.id !== id);
|
||||
return { ...structuredClone(program), activities: structuredClone(next) };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{store: import("../store.js").Store, nav: import("../router.js").Navigation, today: () => string}} ctx
|
||||
* @returns {import("../router.js").Screen}
|
||||
*/
|
||||
export function activitiesScreen({ store, nav, today }) {
|
||||
const list = h("div");
|
||||
const back = { label: "Accueil", onClick: () => nav.back(href.home()) };
|
||||
if (!store.getState().program) {
|
||||
return {
|
||||
title: "Activités",
|
||||
node: h("div", { class: "screen" }, screenHeader({ back, title: "Activités" }),
|
||||
h("p", { class: "empty" }, "Adopte d'abord un programme : les activités en font partie.")),
|
||||
};
|
||||
}
|
||||
|
||||
// ---- formulaire (jamais reconstruit pendant la saisie) ----------------------
|
||||
/** @type {string|null} */
|
||||
let editing = null;
|
||||
const zones = new Set(["cardio"]);
|
||||
let intensity = "moderate";
|
||||
const formTitle = h("h2", { class: "tape" }, "Ajouter");
|
||||
const name = h("input", { id: "act-name", class: "field__input", type: "text", autocomplete: "off", maxlength: "40" });
|
||||
const minutes = h("input", { id: "act-min", class: "field__input", type: "text", inputmode: "numeric", value: "90" });
|
||||
const status = h("p", { class: "status", role: "status", "aria-live": "polite" });
|
||||
const zoneBtns = ZONES.map((z) => h("button", { type: "button", class: "segmented__opt", "aria-pressed": "false", data: { zone: z } }, ZONE_LABELS[z]));
|
||||
const intBtns = INTENSITIES.map((i) => h("button", { type: "button", class: "segmented__opt", "aria-pressed": "false", data: { intensity: i } }, INTENSITY_LABELS[i]));
|
||||
const paintChoices = () => {
|
||||
zoneBtns.forEach((b) => b.setAttribute("aria-pressed", String(zones.has(/** @type {string} */ (b.dataset.zone)))));
|
||||
intBtns.forEach((b) => b.setAttribute("aria-pressed", String(b.dataset.intensity === intensity)));
|
||||
};
|
||||
zoneBtns.forEach((b) => b.addEventListener("click", () => {
|
||||
const z = /** @type {string} */ (b.dataset.zone);
|
||||
if (zones.has(z)) zones.delete(z); else zones.add(z);
|
||||
paintChoices();
|
||||
}));
|
||||
intBtns.forEach((b) => b.addEventListener("click", () => { intensity = /** @type {string} */ (b.dataset.intensity); paintChoices(); }));
|
||||
const cancel = h("button", { type: "button", class: "btn btn--ghost btn--block", hidden: true }, "Annuler la modification");
|
||||
|
||||
const reset = () => {
|
||||
editing = null;
|
||||
zones.clear();
|
||||
zones.add("cardio");
|
||||
intensity = "moderate";
|
||||
name.value = "";
|
||||
minutes.value = "90";
|
||||
formTitle.textContent = "Ajouter";
|
||||
cancel.hidden = true;
|
||||
paintChoices();
|
||||
};
|
||||
cancel.addEventListener("click", reset);
|
||||
|
||||
/** @param {Activity} a */
|
||||
const load = (a) => {
|
||||
editing = a.id;
|
||||
zones.clear();
|
||||
a.zones.forEach((z) => zones.add(z));
|
||||
intensity = a.intensity;
|
||||
name.value = a.name;
|
||||
minutes.value = String(a.minutes);
|
||||
formTitle.textContent = `Modifier · ${a.name}`;
|
||||
cancel.hidden = false;
|
||||
paintChoices();
|
||||
name.focus();
|
||||
};
|
||||
|
||||
const submit = () => {
|
||||
const program = /** @type {Program} */ (store.getState().program);
|
||||
const label = name.value.trim();
|
||||
const min = Math.round(Number(minutes.value.replace(",", ".")));
|
||||
status.className = "status status--err";
|
||||
if (!label) { status.textContent = "Donne un nom à l'activité."; return; }
|
||||
if (!zones.size) { status.textContent = "Choisis au moins une zone."; return; }
|
||||
if (!(min >= 5 && min <= 600)) { status.textContent = "Durée : de 5 à 600 minutes."; return; }
|
||||
const ids = (program.activities ?? []).map((a) => a.id);
|
||||
const old = editing ? program.activities?.find((a) => a.id === editing) : null;
|
||||
const id = editing ?? activityId(label, ids);
|
||||
/** @type {Activity} */
|
||||
const activity = { ...(old ?? {}), id, name: label, zones: /** @type {any} */ ([...zones]), intensity: /** @type {any} */ (intensity), minutes: min };
|
||||
try {
|
||||
store.dispatch("ProgramAdopted", { program: withActivity(program, { id, activity }), source: "app", reason: `${editing ? "Activité modifiée" : "Activité ajoutée"} : ${label}` });
|
||||
} catch (e) {
|
||||
status.textContent = e instanceof Error ? e.message : String(e);
|
||||
return;
|
||||
}
|
||||
status.className = "status status--ok";
|
||||
status.textContent = editing ? `« ${label} » mise à jour.` : `« ${label} » ajoutée. Place-la dans ta semaine.`;
|
||||
reset();
|
||||
};
|
||||
|
||||
/** @param {Activity} a */
|
||||
const remove = async (a) => {
|
||||
const ok = await confirmDialog({
|
||||
title: `Supprimer « ${a.name} » ?`, lines: ["Les jours qui lui étaient réservés redeviennent automatiques. L'historique reste."],
|
||||
confirm: "Supprimer", cancel: "Garder", danger: true,
|
||||
});
|
||||
if (!ok) return;
|
||||
const state = store.getState();
|
||||
store.dispatch("ProgramAdopted", { program: withActivity(/** @type {Program} */ (state.program), { id: a.id, activity: null }), source: "app", reason: `Activité retirée : ${a.name}` });
|
||||
// Les jours imposés qui la désignaient tombent : réglage par défaut et semaines à venir.
|
||||
const monday = mondayOf(today());
|
||||
if (state.planSettings) {
|
||||
const next = withoutActivity(state.planSettings, a.id);
|
||||
if (next) store.dispatch("PlanSettingsChanged", { week: null, ...next });
|
||||
}
|
||||
for (const [week, settings] of Object.entries(state.weekPlanSettings)) {
|
||||
if (week < monday) continue;
|
||||
const next = withoutActivity(settings, a.id);
|
||||
if (next) store.dispatch("PlanSettingsChanged", { week, ...next });
|
||||
}
|
||||
if (editing === a.id) reset();
|
||||
toast(`« ${a.name} » supprimée.`);
|
||||
};
|
||||
|
||||
const paint = () => {
|
||||
const activities = store.getState().program?.activities ?? [];
|
||||
replace(list, activities.length
|
||||
? h("ul", { class: "rows" }, activities.map((a) => h("li", { class: "row" },
|
||||
h("span", { class: "row__main" },
|
||||
h("span", { class: "row__title" }, a.name),
|
||||
h("span", { class: "row__meta" }, `${a.zones.map((z) => ZONE_LABELS[z]).join(", ")} · ${INTENSITY_LABELS[a.intensity].toLowerCase()} · ${a.minutes} min`)),
|
||||
h("span", { class: "row__actions" },
|
||||
h("button", { type: "button", class: "btn btn--sm", "aria-label": `Modifier ${a.name}`, on: { click: () => load(a) } }, "Modifier"),
|
||||
h("button", { type: "button", class: "btn btn--sm btn--ghost", "aria-label": `Supprimer ${a.name}`, on: { click: () => void remove(a) } }, "✕")))))
|
||||
: h("p", { class: "empty" }, "Aucune activité. Ajoute-la ci-dessous, puis place-la dans ta semaine."));
|
||||
};
|
||||
paint();
|
||||
paintChoices();
|
||||
|
||||
const node = h("div", { class: "screen" },
|
||||
screenHeader({ back, kicker: "Hors programme", title: "Activités", sub: "Foot, escalade, vélo… Le planning en tient compte : une activité cardio remplace une sortie, une activité intense éloigne la séance qui travaille les mêmes zones." }),
|
||||
list,
|
||||
formTitle,
|
||||
h("div", { class: "field" }, h("label", { class: "field__label", for: "act-name" }, "Nom"), name),
|
||||
h("p", { class: "field__label", id: "act-zones" }, "Zones sollicitées"),
|
||||
h("div", { class: "segmented", role: "group", "aria-labelledby": "act-zones" }, zoneBtns),
|
||||
h("p", { class: "field__label act-gap", id: "act-int" }, "Intensité"),
|
||||
h("div", { class: "segmented", role: "group", "aria-labelledby": "act-int" }, intBtns),
|
||||
h("div", { class: "field act-gap" }, h("label", { class: "field__label", for: "act-min" }, "Durée (minutes)"), minutes),
|
||||
status,
|
||||
h("div", { class: "btn-stack" },
|
||||
h("button", { type: "button", class: "btn btn--primary btn--block", on: { click: submit } }, "Enregistrer l'activité"),
|
||||
cancel));
|
||||
return { node, title: "Activités", destroy: store.subscribe(paintWhenIdle(list, paint)) };
|
||||
}
|
||||
Reference in New Issue
Block a user