Фазa 3: баллы на сервер (форма/таблица) + панель напоминаний по кладовой
This commit is contained in:
parent
87b806cafb
commit
f0f09d7102
196
index.html
196
index.html
@ -76,6 +76,23 @@
|
||||
.clean-week[data-compact] .clean-day .clean-tip-pop[hidden] { display: none; }
|
||||
.clean-week[data-compact] .clean-day .clean-tip-pop::before { content: ""; position: absolute; top: -9px; left: 22px; border: 5px solid transparent; border-bottom-color: var(--kt-ai-border); }
|
||||
.clean-week[data-compact] .clean-day .clean-tip-pop::after { content: ""; position: absolute; top: -7px; left: 22px; border: 4px solid transparent; border-bottom-color: var(--kt-ai-bg-elevated); }
|
||||
.points-ui { display: grid; gap: var(--kt-ai-space-4); }
|
||||
.points-ui-row { display: grid; grid-template-columns: auto 110px 130px 1fr auto; gap: var(--kt-ai-space-3); align-items: center; padding: var(--kt-ai-space-3) 0; border-top: 1px solid var(--kt-ai-border); font-size: var(--kt-ai-text-md); }
|
||||
.points-ui-row:first-child { border-top: 0; }
|
||||
.points-ui-row-head { color: var(--kt-ai-fg-faint); font-size: var(--kt-ai-text-xs); text-transform: uppercase; letter-spacing: 0.06em; font-weight: var(--kt-ai-weight-medium); }
|
||||
.points-val-cell { font-size: var(--kt-ai-text-xl); font-weight: var(--kt-ai-weight-semibold); text-align: right; }
|
||||
.points-note-cell { color: var(--kt-ai-fg-muted); font-size: var(--kt-ai-text-sm); min-width: 0; }
|
||||
.points-note-cell span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.points-actions { display: flex; gap: var(--kt-ai-space-2); flex-wrap: wrap; }
|
||||
.points-form-row { display: grid; grid-template-columns: 1fr 1fr 90px 1.4fr auto; gap: var(--kt-ai-space-3); align-items: end; }
|
||||
.points-empty { padding: var(--kt-ai-space-5); text-align: center; color: var(--kt-ai-fg-muted); font-size: var(--kt-ai-text-md); }
|
||||
.remind-list { display: grid; gap: var(--kt-ai-space-2); }
|
||||
.remind-item { padding: var(--kt-ai-space-3); border-radius: var(--kt-ai-radius-md); font-size: var(--kt-ai-text-md); }
|
||||
.remind-item-red { background: var(--kt-ai-chip-red-bg); color: var(--kt-ai-chip-red-fg); }
|
||||
.remind-item-orange { background: var(--kt-ai-chip-orange-bg); color: var(--kt-ai-chip-orange-fg); }
|
||||
.remind-item-green { background: var(--kt-ai-chip-green-bg); color: var(--kt-ai-chip-green-fg); }
|
||||
.remind-empty { padding: var(--kt-ai-space-4); text-align: center; color: var(--kt-ai-fg-muted); font-size: var(--kt-ai-text-md); }
|
||||
@media (max-width: 720px) { .points-ui-row { grid-template-columns: auto 110px 1fr; } .points-ui-row .points-note-cell { grid-column: 1 / -1; } .points-ui-row .points-actions { grid-column: 1 / -1; } .points-form-row { grid-template-columns: 1fr 1fr; } .points-form-row .kt-ai-field:nth-child(4) { grid-column: 1 / -1; } .points-form-row .kt-ai-field:nth-child(5) { grid-column: 1 / -1; } }
|
||||
.clean-day.is-today { border-color: var(--kt-ai-primary); background: var(--kt-ai-primary-subtle); }
|
||||
.family-invite { padding: var(--kt-ai-space-6); }
|
||||
.family-head { display: flex; align-items: center; gap: var(--kt-ai-space-4); margin-bottom: var(--kt-ai-space-2); flex-wrap: wrap; }
|
||||
@ -503,6 +520,63 @@
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="kt-ai-section">
|
||||
<div class="kt-ai-wrap" data-width="narrow">
|
||||
<div class="kt-ai-section-head">
|
||||
<h2>Считаем баллы: что записано в этой таблице</h2>
|
||||
<p>Доступно на этой платформе. Записи сохраняются на сервере, таблица видна всем членам семьи через Предпросмотр или в разделе «Данные». Лимиты на 10/25/50/100 из карточек ниже — обмениваются раз в воскресенье.</p>
|
||||
</div>
|
||||
<div class="kt-ai-card points-ui" style="margin-top:var(--kt-ai-space-7)">
|
||||
<form id="points-form" class="points-form" novalidate>
|
||||
<div class="points-form-row">
|
||||
<div class="kt-ai-field">
|
||||
<label for="pt-who">Кто</label>
|
||||
<select class="kt-ai-select" id="pt-who" required>
|
||||
<option value="">выберите</option>
|
||||
<option>Ваня</option>
|
||||
<option>Ася</option>
|
||||
<option>Мам</option>
|
||||
<option>Пап</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="kt-ai-field">
|
||||
<label for="pt-kind">За что</label>
|
||||
<select class="kt-ai-select" id="pt-kind" required>
|
||||
<option value="">выберите</option>
|
||||
<option>Комната (свой день)</option>
|
||||
<option>Посуда после ужина</option>
|
||||
<option>Полить / мусор</option>
|
||||
<option>Помощь с готовкой</option>
|
||||
<option>Глубокая уборка</option>
|
||||
<option>Взял и сделал</option>
|
||||
<option>Помощь младшему</option>
|
||||
<option>Вернуть (списать)</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="kt-ai-field">
|
||||
<label for="pt-amount">Баллы</label>
|
||||
<input class="kt-ai-input" id="pt-amount" type="number" inputmode="numeric" min="1" step="5" value="10" required>
|
||||
</div>
|
||||
<div class="kt-ai-field">
|
||||
<label for="pt-note">Примечание</label>
|
||||
<input class="kt-ai-input" id="pt-note" type="text" maxlength="120" placeholder="Комната 2-го этажа, уборка" autocomplete="off">
|
||||
</div>
|
||||
<div class="kt-ai-field" style="align-self:end">
|
||||
<button class="kt-ai-btn" type="submit" data-variant="primary">Записать</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="buy-outcome" id="points-outcome" hidden></div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="kt-ai-card" style="margin-top:var(--kt-ai-space-5)">
|
||||
<div class="points-ui-row points-ui-row-head">
|
||||
<span>Кто</span><span>Баллы (накоплено)</span><span>Последние операции</span><span> </span><span></span>
|
||||
</div>
|
||||
<div id="points-table"></div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="kt-ai-section">
|
||||
<div class="kt-ai-wrap" data-width="narrow">
|
||||
<div class="kt-ai-section-head">
|
||||
@ -613,7 +687,11 @@
|
||||
<p>Календарь свежести продуктов и ежедневные 10 секунд перед обедом: что «горит» — то и готовим.</p>
|
||||
</div>
|
||||
|
||||
<div class="kt-ai-card fresh-wrap" style="margin-top:var(--kt-ai-space-9)">
|
||||
<div class="kt-ai-card" style="margin-top:var(--kt-ai-space-9)">
|
||||
<h3 style="margin:0 0 var(--kt-ai-space-3);font-size:var(--kt-ai-text-lg);font-weight:var(--kt-ai-weight-semibold);">Сегодня: что горит (из кладовой)</h3>
|
||||
<div id="remind-panel"></div>
|
||||
</div>
|
||||
<div class="kt-ai-card fresh-wrap" style="margin-top:var(--kt-ai-space-5)">
|
||||
<div class="fresh-week">
|
||||
<div class="fresh-day"><span class="fresh-day-name">Пн<br>22</span></div>
|
||||
<div class="fresh-day"><span class="fresh-day-name">Вт<br>23</span></div>
|
||||
@ -875,6 +953,37 @@
|
||||
var c1 = document.createElement("span"); c1.className = "kt-ai-chip"; c1.setAttribute("data-tone", "blue"); c1.textContent = "В доме: " + open;
|
||||
var c2 = document.createElement("span"); c2.className = "kt-ai-chip"; c2.setAttribute("data-tone", urgent ? "red" : "green"); c2.textContent = urgent ? "Горит: " + urgent + " поз." : "Критичных нет";
|
||||
COUNTS.appendChild(c1); COUNTS.appendChild(c2);
|
||||
renderRemind(sorted);
|
||||
}
|
||||
function renderRemind(sorted) {
|
||||
var rem = document.getElementById("remind-panel");
|
||||
if (!rem) return;
|
||||
rem.innerHTML = "";
|
||||
var red = [], orange = [], rest = [];
|
||||
sorted.forEach(function (p) {
|
||||
if (p.consumed) return;
|
||||
var d = daysLeft(p.shelf);
|
||||
var line = '<b>' + esc(p.name) + '</b>' + (p.qty ? ' ' + esc(p.qty) : '') + ' — ' + shelfLabel(d) + (p.meal ? ' → ' + esc(p.meal) : '');
|
||||
if (d === 0) red.push(line); else if (d === 1) red.push(line); else if (d <= 3) orange.push(line); else rest.push(line + ' (ещё в запасе)');
|
||||
});
|
||||
if (!red.length && !orange.length) {
|
||||
rem.innerHTML = '<div class="remind-empty">Всё спокойно: ничего не горит.</div>';
|
||||
return;
|
||||
}
|
||||
var make = function (arr, cls) {
|
||||
var box = document.createElement("div");
|
||||
box.className = "remind-list";
|
||||
arr.forEach(function (line) {
|
||||
var item = document.createElement("div");
|
||||
item.className = "remind-item " + cls;
|
||||
item.innerHTML = line;
|
||||
box.appendChild(item);
|
||||
});
|
||||
return box;
|
||||
};
|
||||
if (red.length) rem.appendChild(make(red, "remind-item-red"));
|
||||
if (orange.length) rem.appendChild(make(orange, "remind-item-orange"));
|
||||
if (rest.length && (red.length + orange.length) > 0) rem.appendChild(make(rest.slice(0, 3), "remind-item-green"));
|
||||
}
|
||||
STORE.addEventListener("click", async function (e) {
|
||||
var btn = e.target.closest("button[data-act]");
|
||||
@ -1016,6 +1125,91 @@
|
||||
card.appendChild(btn);
|
||||
});
|
||||
loadClean();
|
||||
|
||||
/* Баллы: таблица + форма */
|
||||
var MEMBERS = ["Ваня", "Ася", "Мам", "Пап"];
|
||||
async function loadPoints() {
|
||||
var recs = [];
|
||||
try { recs = await (await fetch("api/points")).json(); } catch (_) {}
|
||||
var byWho = {};
|
||||
(recs || []).forEach(function (r) {
|
||||
byWho[r.who] = (byWho[r.who] || 0) + r.amount;
|
||||
});
|
||||
var order = MEMBERS.concat(Object.keys(byWho).filter(function (k) { return MEMBERS.indexOf(k) === -1; }));
|
||||
var lastByWho = {};
|
||||
(recs || []).slice().reverse().forEach(function (r) {
|
||||
if (!lastByWho[r.who] || lastByWho[r.who].when < r.when) lastByWho[r.who] = r;
|
||||
});
|
||||
var tbody = document.getElementById("points-table");
|
||||
tbody.innerHTML = "";
|
||||
if (!recs.length) {
|
||||
var empty = document.createElement("div");
|
||||
empty.className = "points-empty";
|
||||
empty.textContent = "Пока нет записей — добавьте первую через форму выше.";
|
||||
tbody.appendChild(empty);
|
||||
return;
|
||||
}
|
||||
order.forEach(function (who) {
|
||||
var total = byWho[who] || 0;
|
||||
var row = document.createElement("div");
|
||||
row.className = "points-ui-row";
|
||||
var tone = total >= 100 ? "green" : total >= 50 ? "blue" : total >= 25 ? "orange" : "blue";
|
||||
var last = lastByWho[who];
|
||||
row.innerHTML =
|
||||
'<span><b>' + esc(who) + '</b></span>' +
|
||||
'<span class="points-val-cell"><span class="kt-ai-chip" data-tone="' + tone + '">' + total + ' баллов</span></span>' +
|
||||
'<span class="points-note-cell">' + (last ? (last.amount > 0 ? '+ ' + last.amount + ' за ' + esc(last.kind) + (last.note ? ' · ' + esc(last.note) : '') : '- ' + Math.abs(last.amount) + ' (' + esc(last.note || last.kind) + ')') : '—') + '</span>' +
|
||||
'<span class="kt-ai-chip" data-tone="' + tone + '" style="visibility:hidden">' + (tone === "green" ? "100 — крупная награда" : tone === "blue" ? "накопление" : tone === "orange" ? "25 — выбор фильма" : "накопление") + '</span>' +
|
||||
'<span class="points-actions">' +
|
||||
'<button type="button" class="kt-ai-btn" data-size="sm" data-variant="ghost" data-who="' + esc(who) + '">+ 1</button>' +
|
||||
'<button type="button" class="kt-ai-btn" data-size="sm" data-variant="ghost" data-who="' + esc(who) + '">- 1</button>' +
|
||||
'</span>';
|
||||
tbody.appendChild(row);
|
||||
});
|
||||
tbody.querySelectorAll("button[data-who]").forEach(function (b) {
|
||||
b.addEventListener("click", function () {
|
||||
var who = b.getAttribute("data-who");
|
||||
var amt = b.textContent.trim();
|
||||
var n = parseInt(amt, 10);
|
||||
submitPoint({ who: who, kind: amt > 0 ? "Бонус" : "Вернуть (списать)", amount: n, note: "клик из таблицы" });
|
||||
});
|
||||
});
|
||||
}
|
||||
async function submitPoint(p) {
|
||||
var out = document.getElementById("points-outcome");
|
||||
try {
|
||||
var res = await fetch("api/points", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(p) });
|
||||
var data = await res.json();
|
||||
if (!res.ok || !data.item) throw new Error();
|
||||
out.hidden = false;
|
||||
out.className = "buy-outcome buy-ok";
|
||||
out.textContent = (p.amount > 0 ? "Записали: " : "Списали: ") + p.who + " — " + Math.abs(p.amount) + " (" + p.kind + (p.note ? ", " + p.note : "") + ")";
|
||||
document.getElementById("points-form").reset();
|
||||
loadPoints();
|
||||
} catch (_) {
|
||||
out.hidden = false;
|
||||
out.className = "buy-outcome buy-err";
|
||||
out.textContent = "Не получилось сохранить запись. Попробуйте ещё раз.";
|
||||
}
|
||||
}
|
||||
document.getElementById("points-form").addEventListener("submit", async function (e) {
|
||||
e.preventDefault();
|
||||
var who = document.getElementById("pt-who").value;
|
||||
var kind = document.getElementById("pt-kind").value;
|
||||
var amountRaw = document.getElementById("pt-amount").value;
|
||||
var amount = parseInt(amountRaw, 10) || 0;
|
||||
var note = document.getElementById("pt-note").value.trim();
|
||||
if (!who || !kind || !amount) {
|
||||
var out = document.getElementById("points-outcome");
|
||||
out.hidden = false;
|
||||
out.className = "buy-outcome buy-err";
|
||||
out.textContent = "Заполните кто, за что и количество (не 0).";
|
||||
return;
|
||||
}
|
||||
var signed = kind === "Вернуть (списать)" ? -Math.abs(amount) : Math.abs(amount);
|
||||
submitPoint({ who: who, kind: kind, amount: signed, note: note });
|
||||
});
|
||||
loadPoints();
|
||||
})();
|
||||
</script>
|
||||
|
||||
|
||||
36
server.js
36
server.js
@ -4,7 +4,7 @@ const path = require("path");
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
const DATA = path.join(__dirname, "data.json");
|
||||
const EMPTY = { purchases: [], clean: [], log: [] };
|
||||
const EMPTY = { purchases: [], clean: [], points: [], log: [] };
|
||||
|
||||
function load() {
|
||||
try { return JSON.parse(fs.readFileSync(DATA, "utf8")); } catch (_) { return { ...EMPTY }; }
|
||||
@ -92,6 +92,40 @@ http.createServer(async (req, res) => {
|
||||
return res.end(JSON.stringify({ ok: true }));
|
||||
}
|
||||
|
||||
if (req.method === "GET" && url === "/api/points") {
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
return res.end(JSON.stringify(load().points || []));
|
||||
}
|
||||
|
||||
if (req.method === "POST" && url === "/api/points") {
|
||||
const p = await body(req);
|
||||
const when = new Date().toISOString();
|
||||
const who = String(p.who || "").slice(0, 40);
|
||||
const kind = String(p.kind || "").slice(0, 40);
|
||||
const amount = Math.round(Number(p.amount) || 0);
|
||||
const note = String(p.note || "").slice(0, 120);
|
||||
if (!who || !kind || amount === 0) {
|
||||
res.writeHead(400, { "Content-Type": "application/json" });
|
||||
return res.end(JSON.stringify({ error: "who, kind and non-zero amount required" }));
|
||||
}
|
||||
const d = load();
|
||||
if (!Array.isArray(d.points)) d.points = [];
|
||||
const rec = {
|
||||
id: Date.now(),
|
||||
who,
|
||||
kind,
|
||||
amount,
|
||||
note,
|
||||
when
|
||||
};
|
||||
d.points.push(rec);
|
||||
const verb = amount > 0 ? "получил(а)" : "вернул(а)";
|
||||
d.log.push({ when, what: "баллы: " + who + " " + verb + " " + Math.abs(amount) + (amount < 0 ? " (вернуло " + note + ")" : " за " + kind), who: "юзер" });
|
||||
save(d);
|
||||
res.writeHead(200, { "Content-Type": "application/json" });
|
||||
return res.end(JSON.stringify({ ok: true, item: rec }));
|
||||
}
|
||||
|
||||
let file = url === "/" ? "/index.html" : url;
|
||||
const full = path.join(__dirname, file);
|
||||
if (full.startsWith(__dirname) && fs.existsSync(full) && fs.statSync(full).isFile()) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user