Afanasev_Stepan_PIbd-21_IP/ИП 3 лаба/js/lines.js

226 lines
6.1 KiB
JavaScript
Raw Permalink Normal View History

2024-01-10 16:11:49 +04:00
import { hideUpdateModal, showUpdateModal } from "./lines-modal";
import {
createLine, deleteLine, getAllItemTypes, getAllActions, getAllLines, getLine, updateLine,
} from "./lines-rest-api";
import {
cntrls, createItemsOption, createTableRow, imagePlaceholder,
} from "./lines-ui";
async function drawItemsSelect() {
const data = await getAllItemTypes();
cntrls.itemsType.innerHTML = "";
cntrls.itemsType.appendChild(createItemsOption("Выберите значение", "", true));
data.forEach((item) => {
cntrls.itemsType.appendChild(createItemsOption(item.name, item.id));
});
}
async function drawActionsSelect() {
const data = await getAllActions();
cntrls.action.innerHTML = "";
cntrls.action.appendChild(createItemsOption("Выберите значение", "", true));
data.forEach((item) => {
cntrls.action.appendChild(createItemsOption(item.name, item.id));
});
}
async function readFile(file) {
const reader = new FileReader();
return new Promise((resolve, reject) => {
reader.onloadend = () => {
const fileContent = reader.result;
resolve(fileContent);
};
reader.onerror = () => {
reject(new Error("oops, something went wrong with the file reader."));
};
reader.readAsDataURL(file);
});
}
async function drawLinesTable() {
console.info("Try to load data");
if (!cntrls.table) {
return;
}
const data = await getAllLines();
cntrls.table.innerHTML = "";
data.forEach((item, index) => {
cntrls.table.appendChild(
createTableRow(
item,
index,
() => showUpdateModal(item),
() => removeLine(item.id),
),
);
});
}
async function addLine(item, action, price, nick, duration, image) {
console.info("Try to add item");
const data = await createLine(item, action, price, nick, duration, image);
console.info("Added");
console.info(data);
drawLinesTable();
}
async function editLine(item, action, price, nick, duration, image) {
console.info("Try to update item");
const data = await updateLine(item, action, price, nick, duration, image);
console.info("Updated");
console.info(data);
drawLinesTable();
}
async function removeLine(id) {
if (!confirm("Do you really want to remove this item?")) {
console.info("Canceled");
return;
}
console.info("Try to remove item");
const data = await deleteLine(id);
console.info(data);
drawLinesTable();
}
async function updateImagePreview() {
const file = cntrls.image.files[0];
const fileContent = await readFile(file);
console.info("base64 ", fileContent);
cntrls.imagePreview.src = fileContent;
}
export function linesForm() {
console.info("linesForm");
drawItemsSelect();
drawActionsSelect();
drawLinesTable();
cntrls.image.addEventListener("change", () => updateImagePreview());
cntrls.button.addEventListener("click", () => showUpdateModal(null));
cntrls.form.addEventListener("submit", async (event) => {
console.info("Form onSubmit");
event.preventDefault();
event.stopPropagation();
if (!cntrls.form.checkValidity()) {
return;
}
let imageBase64 = "";
if (cntrls.imagePreview.src !== imagePlaceholder) {
const result = await fetch(cntrls.imagePreview.src);
const blob = await result.blob();
imageBase64 = await readFile(blob);
}
const currentId = cntrls.lineId.value;
if (!currentId) {
await addLine(
cntrls.itemsType.value,
cntrls.action.value,
cntrls.price.value,
cntrls.nick.value,
cntrls.duration.value,
imageBase64,
);
} else {
await editLine(
currentId,
cntrls.itemsType.value,
cntrls.action.value,
cntrls.price.value,
cntrls.nick.value,
cntrls.duration.value,
imageBase64,
);
}
hideUpdateModal();
});
}
export async function linesPageForm() {
console.info("linesPageForm");
drawItemsSelect();
drawActionsSelect();
const goBack = () => location.assign("/admin.html");
cntrls.image.addEventListener("change", () => updateImagePreview());
const urlParams = new URLSearchParams(location.search);
const currentId = urlParams.get("id");
if (currentId) {
try {
const line = await getLine(currentId);
cntrls.itemsType.value = line.itemsId;
cntrls.action.value = line.actionsId;
cntrls.price.value = line.price;
cntrls.nick.value = line.nick;
cntrls.duration.value = line.duration;
cntrls.imagePreview.src = line.image ? line.image : imagePlaceholder;
} catch {
goBack();
}
}
cntrls.form.addEventListener("submit", async (event) => {
console.info("Form onSubmit");
event.preventDefault();
event.stopPropagation();
if (!cntrls.form.checkValidity()) {
return;
}
let imageBase64 = "";
if (cntrls.imagePreview.src !== imagePlaceholder) {
const result = await fetch(cntrls.imagePreview.src);
const blob = await result.blob();
imageBase64 = await readFile(blob);
}
if (!currentId) {
await addLine(
cntrls.itemsType.value,
cntrls.action.value,
cntrls.price.value,
cntrls.nick.value,
cntrls.duration.value,
imageBase64,
);
} else {
await editLine(
currentId,
cntrls.itemsType.value,
cntrls.action.value,
cntrls.price.value,
cntrls.nick.value,
cntrls.duration.value,
imageBase64,
);
}
goBack();
});
}