This commit is contained in:
Вячеслав Иванов 2023-11-03 15:20:56 +04:00
parent c986ccca9f
commit 9fea16dfe0
8 changed files with 84 additions and 58 deletions

View File

@ -40,21 +40,20 @@
<main class="container-fluid p-2">
<h1 class="text-warning text-center font-weight-bold">Панель администратора</h1>
<div class="btn-group" role="group">
<button id="items-add" class="btn btn-primary">Добавить товар (диалог)</button>
<a class="btn btn-danger" href="/page-edit.html">Добавить товар (страница)</a>
<a class="btn btn-warning" href="/page-edit.html">Добавить товар</a>
</div>
<div>
<h2 class="text-warning text-center font-weight-bold" style="padding-top: 30px;">Таблица данных</h2>
<h2 class="text-warning text-center font-weight-bold" style="padding-top: 10px;">Таблица данных</h2>
<table id="items-table" class="table table-striped">
<thead>
<th scope="col"></th>
<th scope="col" class="w-25">Товар</th>
<th scope="col" class="w-25">Цена</th>
<th scope="col" class="w-10">Акция</th>
<th scope="col" class="w-25">Количество</th>
<th scope="col" class="w-25">Сумма</th>
<th scope="col"></th>
<th scope="col"></th>
<th scope="col"></th>
</thead>
<tbody></tbody>
</table>
@ -87,6 +86,11 @@
<input id="price" name="price" class="form-control" type="number" value="99.00" min="99.00"
step="1.00" required>
</div>
<div class="mb-2">
<label class="form-label" for="stock">Акция</label>
<input id="stock" name="price" class="form-control" type="number" value="0" min="0"
step="100" required>
</div>
<div class="mb-2">
<label class="form-label" for="count">Количество</label>
<input id="count" name="count" class="form-control" type="number" value="0" min="1" step="1"

File diff suppressed because one or more lines are too long

View File

@ -1,37 +0,0 @@
import { Modal } from "bootstrap";
import { cntrls, imagePlaceholder } from "./lines-ui";
const modal = document.getElementById("items-update");
const myModal = modal ? new Modal(modal, {}) : null;
const modalTitle = document.getElementById("items-update-title");
function resetValues() {
cntrls.lineId.value = "";
cntrls.itemsType.value = "";
cntrls.price.value = parseFloat(0).toFixed(2);
cntrls.count.value = 0;
cntrls.image.value = "";
cntrls.imagePreview.src = imagePlaceholder;
}
export function showUpdateModal(item) {
modalTitle.innerHTML = item === null ? "Добавить" : "Изменить";
console.info(item);
if (item) {
cntrls.lineId.value = item.id;
cntrls.itemsType.value = item.itemsId;
cntrls.price.value = item.price;
cntrls.count.value = item.count;
cntrls.imagePreview.src = item.image ? item.image : imagePlaceholder;
} else {
resetValues();
}
myModal.show();
}
export function hideUpdateModal() {
resetValues();
cntrls.form.classList.remove("was-validated");
myModal.hide();
}

View File

@ -1,11 +1,12 @@
const serverUrl = "http://localhost:8081";
function createLineObject(item, price, count, image) {
function createLineObject(item, price, stock, count, image) {
return {
itemsId: item,
price: parseFloat(price).toFixed(2),
stock,
count,
sum: parseFloat(price * count).toFixed(2),
sum: parseFloat((price * ((100 - stock) / 100)) * count).toFixed(2),
image,
};
}
@ -34,8 +35,8 @@ export async function getLine(id) {
return response.json();
}
export async function createLine(item, price, count, image) {
const itemObject = createLineObject(item, price, count, image);
export async function createLine(item, price, stock, count, image) {
const itemObject = createLineObject(item, price, stock, count, image);
const options = {
method: "POST",
@ -53,8 +54,8 @@ export async function createLine(item, price, count, image) {
return response.json();
}
export async function updateLine(id, item, price, count, image) {
const itemObject = createLineObject(item, price, count, image);
export async function updateLine(id, item, price, stock, count, image) {
const itemObject = createLineObject(item, price, stock, count, image);
const options = {
method: "PUT",

View File

@ -5,6 +5,7 @@ export const cntrls = {
lineId: document.getElementById("items-line-id"),
itemsType: document.getElementById("item"),
price: document.getElementById("price"),
stock: document.getElementById("stock"),
count: document.getElementById("count"),
image: document.getElementById("image"),
imagePreview: document.getElementById("image-preview"),
@ -41,7 +42,7 @@ function createTableColumn(value) {
return td;
}
export function createTableRow(item, index, editCallback, editPageCallback, deleteCallback) {
export function createTableRow(item, index, editCallback, deleteCallback) {
const rowNumber = document.createElement("th");
rowNumber.scope = "row";
rowNumber.textContent = index + 1;
@ -50,10 +51,10 @@ export function createTableRow(item, index, editCallback, editPageCallback, dele
row.appendChild(rowNumber);
row.appendChild(createTableColumn(item.items.name));
row.appendChild(createTableColumn(parseFloat(item.price).toFixed(2)));
row.appendChild(createTableColumn(item.stock != null ? item.stock + "%" : ""));
row.appendChild(createTableColumn(item.count));
row.appendChild(createTableColumn(parseFloat(item.sum).toFixed(2)));
row.appendChild(createTableAnchor("fa-pencil", editCallback));
row.appendChild(createTableAnchor("fa-pen-to-square", editPageCallback));
row.appendChild(createTableAnchor("fa-trash", deleteCallback));
return row;
}

View File

@ -1,4 +1,3 @@
import { hideUpdateModal, showUpdateModal } from "./lines-modal";
import {
createLine, deleteLine, getAllItemTypes, getAllLines, getLine, updateLine,
} from "./lines-rest-api";
@ -27,7 +26,6 @@ async function drawLinesTable() {
createTableRow(
item,
index,
() => showUpdateModal(item),
() => location.assign(`page-edit.html?id=${item.id}`),
() => removeLine(item.id),
),
@ -35,17 +33,17 @@ async function drawLinesTable() {
});
}
async function addLine(item, price, count, image) {
async function addLine(item, price, stock, count, image) {
console.info("Try to add item");
const data = await createLine(item, price, count, image);
const data = await createLine(item, price, stock, count, image);
console.info("Added");
console.info(data);
drawLinesTable();
}
async function editLine(id, item, price, count, image) {
async function editLine(id, item, price, stock, count, image) {
console.info("Try to update item");
const data = await updateLine(id, item, price, count, image);
const data = await updateLine(id, item, price, stock, count, image);
console.info("Updated");
console.info(data);
drawLinesTable();
@ -88,7 +86,6 @@ export function linesForm() {
drawItemsSelect();
drawLinesTable();
cntrls.image.addEventListener("change", () => updateImagePreview());
cntrls.button.addEventListener("click", () => showUpdateModal(null));
cntrls.form.addEventListener("submit", async (event) => {
console.info("Form onSubmit");
event.preventDefault();
@ -107,6 +104,7 @@ export function linesForm() {
await addLine(
cntrls.itemsType.value,
cntrls.price.value,
cntrls.stock.value,
cntrls.count.value,
imageBase64,
);
@ -115,11 +113,11 @@ export function linesForm() {
currentId,
cntrls.itemsType.value,
cntrls.price.value,
cntrls,stock.value,
cntrls.count.value,
imageBase64,
);
}
hideUpdateModal();
});
}
@ -135,6 +133,7 @@ export async function linesPageForm() {
const line = await getLine(currentId);
cntrls.itemsType.value = line.itemsId;
cntrls.price.value = line.price;
cntrls.stock.value = line.stock;
cntrls.count.value = line.count;
cntrls.imagePreview.src = line.image ? line.image : imagePlaceholder;
} catch {
@ -159,6 +158,7 @@ export async function linesPageForm() {
await addLine(
cntrls.itemsType.value,
cntrls.price.value,
cntrls.stock.value,
cntrls.count.value,
imageBase64,
);
@ -167,6 +167,7 @@ export async function linesPageForm() {
currentId,
cntrls.itemsType.value,
cntrls.price.value,
cntrls.stock.value,
cntrls.count.value,
imageBase64,
);

View File

@ -55,6 +55,11 @@
<input id="price" name="price" class="form-control" type="number" value="0.00" min="1000.00" step="0.50"
required>
</div>
<div class="mb-2">
<label class="form-label" for="stock">Акция</label>
<input id="stock" name="price" class="form-control" type="number" value="0" min="0"
step="1" required>
</div>
<div class="mb-2">
<label class="form-label" for="count">Количество</label>
<input id="count" name="count" class="form-control" type="number" value="0" min="1" step="1" required>

Binary file not shown.