78 lines
2.4 KiB
JavaScript
78 lines
2.4 KiB
JavaScript
function esc(v) {
|
||
return String(v == null ? "" : v)
|
||
.replace(/&/g, "&")
|
||
.replace(/</g, "<")
|
||
.replace(/>/g, ">")
|
||
.replace(/"/g, """)
|
||
.replace(/'/g, "'");
|
||
}
|
||
|
||
function fmtPhone(v) {
|
||
var d = String(v || "").replace(/\D/g, "");
|
||
return d.length ? "+" + d : "";
|
||
}
|
||
|
||
function load() {
|
||
fetch("api/items").then(function (r) { return r.json(); }).then(render).catch(function () {});
|
||
}
|
||
|
||
function render(items) {
|
||
var c = document.getElementById("tb-count");
|
||
var list = document.getElementById("reg-list");
|
||
if (!c || !list) return;
|
||
c.textContent = items.length;
|
||
if (!items.length) {
|
||
list.innerHTML = '<p style="color:var(--kt-ai-fg-muted)">Пока никто не зарегистрировался. Будь первым!</p>';
|
||
return;
|
||
}
|
||
var rows = items.slice().reverse().map(function (r) {
|
||
return "<tr>" +
|
||
"<td>" + esc(r.name) + "</td>" +
|
||
"<td>" + esc(fmtPhone(r.phone)) + "</td>" +
|
||
"<td>" + esc(r.dept) + "</td>" +
|
||
(r.comment ? "<td>" + esc(r.comment) + "</td>" : "<td> </td>") +
|
||
"</tr>";
|
||
}).join("");
|
||
list.innerHTML =
|
||
"<table class='tb-reg-table'>" +
|
||
"<thead><tr><th>ФИО</th><th>Телефон</th><th>Департамент</th><th>Пожелания</th></tr></thead>" +
|
||
"<tbody>" + rows + "</tbody></table>";
|
||
}
|
||
|
||
document.getElementById("reg-form").addEventListener("submit", function (e) {
|
||
e.preventDefault();
|
||
var f = e.target;
|
||
var st = document.getElementById("form-status");
|
||
var data = {
|
||
name: f.name.value.trim(),
|
||
phone: f.phone.value.trim(),
|
||
dept: f.dept.value.trim(),
|
||
comment: f.comment.value.trim()
|
||
};
|
||
if (!data.name || !data.phone || !data.dept) {
|
||
st.textContent = "Заполни обязательные поля";
|
||
st.className = "err";
|
||
return;
|
||
}
|
||
st.textContent = "Отправляем…";
|
||
st.className = "";
|
||
fetch("api/items", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify(data)
|
||
})
|
||
.then(function (r) { return r.json(); })
|
||
.then(function () {
|
||
st.textContent = "Ты в списке! До встречи 15 ноября.";
|
||
st.className = "ok";
|
||
f.reset();
|
||
load();
|
||
})
|
||
.catch(function () {
|
||
st.textContent = "Не получилось отправить. Попробуй ещё раз.";
|
||
st.className = "err";
|
||
});
|
||
});
|
||
|
||
load();
|