This commit is contained in:
2023-12-22 09:03:01 +03:00
parent 5650c55a67
commit fab58b2e30
4 changed files with 182 additions and 140 deletions

View File

@@ -4,12 +4,35 @@ header nav {
font-family: Santa Catarina; font-family: Santa Catarina;
} }
.book {
text-align: center;
margin-bottom: 20px;
flex: 0 0 calc(25% - 30px);
}
.book img {
max-width: 60%;
max-height: 340px;
border-radius: 5px;
margin-bottom: 10px;
}
.book h3 {
color: black;
text-align: center;
font-size: 30px;
font-style: normal;
font-weight: 300;
line-height: normal;
margin-bottom: 10px;
height: 100px;
}
@media (min-width: 768px) { @media (min-width: 768px) {
header nav { header nav {
font-family: 'Santa Catarina', Arial, sans-serif; font-family: 'Santa Catarina', Arial, sans-serif;
height: 64px; height: 64px;
} }
} }
body { body {
overflow-x: hidden; overflow-x: hidden;
@@ -35,7 +58,7 @@ footer {
color: white; color: white;
text-align: center; text-align: center;
padding: 10px 0; padding: 10px 0;
bottom: 0; bottom: 0;
left: 0; left: 0;
} }

View File

@@ -4,133 +4,113 @@
const serverUrl = "http://localhost:8081"; const serverUrl = "http://localhost:8081";
// функция возвращает объект нужной структуры для отправки на сервер // функция возвращает объект нужной структуры для отправки на сервер
function createLineObject(item, author, name, desc, count, date, image) { // eslint-disable-next-line max-len
return { function createLineObject(_name, _authorsType, _genresType, _year, _description, _count, _date, _image) {
itemsId: item, return {
authorsId: author, nameBook: _name,
name, authorsId: _authorsType,
desc, genresId: _genresType,
count, year: _year,
date, description: _description,
image, count: _count,
}; date: _date,
image: _image,
};
} }
// обращение к серверу для получения всех типов товара (get) /// обращение к серверу для получения всех типов товара (get)
export async function getAllItemTypes() { export async function getAllAuthorTypes() {
const response = await fetch(`${serverUrl}/items`); const response = await fetch(`${serverUrl}/authors`);
if (!response.ok) { if (!response.ok) {
throw response.statusText; throw response.statusText;
} }
return response.json(); return response.json();
} }
export async function getAllGengeTypes() {
// обращение к серверу для получения всех типов товара (get) const response = await fetch(`${serverUrl}/genres`);
export async function getAllItemAuthors() { if (!response.ok) {
const response = await fetch(`${serverUrl}/authors`); throw response.statusText;
if (!response.ok) { }
throw response.statusText; return response.json();
}
return response.json();
} }
// обращение к серверу для получения всех записей (get) // обращение к серверу для получения всех записей (get)
export async function getAllLines() { export async function getAllLines() {
const response = await fetch(`${serverUrl}/lines?_expand=items&_expand=authors`); const response = await fetch(`${serverUrl}/lines?_expand=authors&_expand=genres`);
if (!response.ok) { if (!response.ok) {
throw response.statusText; throw response.statusText;
} }
return response.json(); return response.json();
} }
// обращение к серверу для получения записи по первичному ключу (id) (get) // обращение к серверу для получения записи по первичному ключу (id) (get)
// id передается в качестве части пути URL get-запроса // id передается в качестве части пути URL get-запроса
export async function getLine(id) { export async function getLine(id) {
const response = await fetch(`${serverUrl}/lines/${id}?_expand=items&_expand=authors`); const response = await fetch(`${serverUrl}/lines/${id}?_expand=genres&_expand=authors`);
if (!response.ok) { if (!response.ok) {
throw response.statusText; throw response.statusText;
} }
return response.json(); return response.json();
} }
// обращение к серверу для создания записи (post) // обращение к серверу для создания записи (post)
// объект отправляется в теле запроса (body) // объект отправляется в теле запроса (body)
export async function createLine(item, author, name, desc, count, date, image) { // eslint-disable-next-line max-len
const itemObject = createLineObject( export async function createLine(_name, _authorsType, _genresType, _year, _description, _count, _date, _image) {
item, // eslint-disable-next-line max-len
author, const itemObject = createLineObject(_name, _authorsType, _genresType, _year, _description, _count, _date, _image);
name,
desc,
count,
date,
image,
);
const options = { const options = {
method: "POST", method: "POST",
body: JSON.stringify(itemObject), body: JSON.stringify(itemObject),
headers: { headers: {
Accept: "application/json", "Accept": "application/json",
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
}; };
const response = await fetch(`${serverUrl}/lines`, options); const response = await fetch(`${serverUrl}/lines`, options);
if (!response.ok) { if (!response.ok) {
throw response.statusText; throw response.statusText;
} }
return response.json(); return response.json();
} }
// обращение к серверу для обновления записи по id (put) // обращение к серверу для обновления записи по id (put)
// объект отправляется в теле запроса (body) // объект отправляется в теле запроса (body)
// id передается в качестве части пути URL get-запроса // id передается в качестве части пути URL get-запроса
export async function updateLine( // eslint-disable-next-line max-len
id, export async function updateLine(id, _name, _authorsType, _genresType, _year, _description, _count, _date, _image) {
item, // eslint-disable-next-line max-len
author, const itemObject = createLineObject(_name, _authorsType, _genresType, _year, _description, _count, _date, _image);
name,
desc,
count,
date,
image,
) {
const itemObject = createLineObject(
item,
author,
name,
desc,
count,
date,
image,
);
const options = { const options = {
method: "PUT", method: "PUT",
body: JSON.stringify(itemObject), body: JSON.stringify(itemObject),
headers: { headers: {
Accept: "application/json", "Accept": "application/json",
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
}; };
const response = await fetch(`${serverUrl}/lines/${id}`, options); const response = await fetch(`${serverUrl}/lines/${id}`, options);
if (!response.ok) { if (!response.ok) {
throw response.statusText; throw response.statusText;
} }
return response.json(); return response.json();
} }
// обращение к серверу для удаления записи по id (delete) // обращение к серверу для удаления записи по id (delete)
// id передается в качестве части пути URL get-запроса // id передается в качестве части пути URL get-запроса
export async function deleteLine(id) { export async function deleteLine(id) {
const options = { const options = {
method: "DELETE", method: "DELETE",
}; };
const response = await fetch(`${serverUrl}/lines/${id}`, options); const response = await fetch(`${serverUrl}/lines/${id}`, options);
if (!response.ok) { if (!response.ok) {
throw response.statusText; throw response.statusText;
} }
return response.json(); await response.json();
location.reload();
} }

View File

@@ -4,14 +4,16 @@
// при обращении к атрибуту объекта вызывается // при обращении к атрибуту объекта вызывается
// нужная функция для поиска элемента // нужная функция для поиска элемента
export const cntrls = { export const cntrls = {
container: document.getElementById("my-id-for-text"),
button: document.getElementById("items-add"), button: document.getElementById("items-add"),
table: document.querySelector("#items-table tbody"), table: document.querySelector("#items-table tbody"),
form: document.getElementById("items-form"), form: document.getElementById("items-form"),
lineId: document.getElementById("items-line-id"), lineId: document.getElementById("items-line-id"),
itemsType: document.getElementById("item"), genresType: document.getElementById("genre"),
author: document.getElementById("author"), nameBook: document.getElementById("nameBook"),
name: document.getElementById("name"), authorsType: document.getElementById("author"),
desc: document.getElementById("desc"), year: document.getElementById("yearPicker"),
description: document.getElementById("description"),
count: document.getElementById("count"), count: document.getElementById("count"),
date: document.getElementById("date"), date: document.getElementById("date"),
image: document.getElementById("image"), image: document.getElementById("image"),
@@ -23,7 +25,15 @@ export const imagePlaceholder = "https://via.placeholder.com/200";
// функция создает тег option для select // функция создает тег option для select
// <option value="" selected>name</option> // <option value="" selected>name</option>
export function createItemsOption(name, value = "", isSelected = false) { export function createGenresOption(name, value = "", isSelected = false) {
const option = document.createElement("option");
option.value = value || "";
option.selected = isSelected;
option.text = name;
return option;
}
export function createAuthorsOption(name, value = "", isSelected = false) {
const option = document.createElement("option"); const option = document.createElement("option");
option.value = value || ""; option.value = value || "";
option.selected = isSelected; option.selected = isSelected;
@@ -44,10 +54,10 @@ function createTableAnchor(icon, callback) {
a.href = "#"; a.href = "#";
a.appendChild(i); a.appendChild(i);
a.onclick = (event) => { a.onclick = (event) => {
// чтобы в URL не добавлялась решетка // чтобы в URL не добавлялась решетка
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
callback(); callback();
}; };
const td = document.createElement("td"); const td = document.createElement("td");
@@ -74,13 +84,7 @@ function createTableColumn(value) {
// <td><a href="#" onclick="editPageCallback()"><i class="fa-solid fa-pen-to-square"></i></a></td> // <td><a href="#" onclick="editPageCallback()"><i class="fa-solid fa-pen-to-square"></i></a></td>
// <td><a href="#" onclick="deleteCallback()"><i class="fa-solid fa-trash"></i></a></td> // <td><a href="#" onclick="deleteCallback()"><i class="fa-solid fa-trash"></i></a></td>
// </tr> // </tr>
export function createTableRow( export function createTableRow(item, index, editCallback, deleteCallback) {
item,
index,
editCallback,
editPageCallback,
deleteCallback,
) {
const rowNumber = document.createElement("th"); const rowNumber = document.createElement("th");
rowNumber.scope = "row"; rowNumber.scope = "row";
rowNumber.textContent = index + 1; rowNumber.textContent = index + 1;
@@ -89,17 +93,47 @@ export function createTableRow(
row.id = `line-${item.id}`; row.id = `line-${item.id}`;
row.appendChild(rowNumber); row.appendChild(rowNumber);
row.appendChild(createTableColumn(item.items.name)); row.appendChild(createTableColumn(item.nameBook));
row.appendChild(createTableColumn(item.authors.name)); row.appendChild(createTableColumn(item.authors.name));
row.appendChild(createTableColumn(item.name)); row.appendChild(createTableColumn(item.genres.name));
row.appendChild(createTableColumn(item.desc)); row.appendChild(createTableColumn(item.year));
row.appendChild(createTableColumn(item.description));
row.appendChild(createTableColumn(item.count)); row.appendChild(createTableColumn(item.count));
row.appendChild(createTableColumn(item.date)); row.appendChild(createTableColumn(item.date));
// редактировать в модальном окне // редактировать в модальном окне
row.appendChild(createTableAnchor("fa-pencil", editCallback)); row.appendChild(createTableAnchor("fa-pencil", editCallback));
// редактировать на странице page-edit
row.appendChild(createTableAnchor("fa-pen-to-square", editPageCallback));
// удаление // удаление
row.appendChild(createTableAnchor("fa-trash", deleteCallback)); row.appendChild(createTableAnchor("fa-trash", deleteCallback));
return row; return row;
} }
export function createTableRowOnIndex(item) {
console.log(item);
const bookCard = createBookCard(item);
return bookCard;
}
function createBookCard(item) {
const card = document.createElement("div");
card.classList.add("book");
const image = document.createElement("img");
image.src = item.image ? item.image : imagePlaceholder;
image.alt = "Обложка книги";
const title = document.createElement("h3");
title.textContent = `${item.nameBook}, ${item.authors.name}`;
card.appendChild(image);
card.appendChild(title);
const div = document.createElement("div");
div.classList.add("col-12");
div.classList.add("col-md-4");
div.classList.add("col-lg-4");
div.classList.add("col-xl-3");
div.appendChild(card);
return div;
}

View File

@@ -34,29 +34,34 @@
</div> </div>
</nav> </nav>
</header> </header>
<article class="container py-5"> <main class="container main_pannel">
<h2 class="col-md-3 mb-4 text-center mx-auto">Бестселлеры</h2> <div id="my-id-for-text" class="row">
<div class="row justify-content-center">
<div class="col-md-3 mb-4 text-center">
<img src="images/kafka.jpg" alt="" class="img-fluid" style="max-width: 260px; max-height: 372px;">
<h3 class="h7 mt-3">Ф.Кафка “Превращение”</h3>
</div> </div>
<div class="col-md-3 mb-4 text-center"> </main>
<img src="images/pandp.jpg" alt="" class="img-fluid" style="max-width: 260px; max-height: 372px;">
<h3 class="h7 mt-3">Д.Остен “Гордость и предубеждение”</h3> <div class="modal" id="my-modal">
</div> <div class="modal-box">
<div class="col-md-3 mb-4 text-center"> <button id="close-modal-btn" class="modal-close">
<img src="images/leot.jpg" alt="" class="img-fluid" style="max-width: 260px; max-height: 372px;"> <svg width="24" height="24" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
<h3 class="h7 mt-3">Л.Толстой “Смерть Ивана Ильича”</h3> <path fill-rule="evenodd" clip-rule="evenodd" d="M4.11 2.697L2.698 4.11 6.586 8l-3.89 3.89 1.415 1.413L8 9.414l3.89 3.89 1.413-1.415L9.414 8l3.89-3.89-1.415-1.413L8 6.586l-3.89-3.89z" fill="#000"></path>
</div> </svg>
<div class="col-md-3 mb-4 text-center"> <ul>
<img src="images/stranger.jpg" alt="" class="img-fluid" style="max-width: 260px; max-height: 372px;"> </ul>
<h3 class="h7 mt-3">А.Камю “Посторонний”</h3>
</div> </div>
</div> </div>
</article>
<footer class="footer mt-auto d-flex flex-shrink-0 justify-content-center align-items-center fixed-bottom"> <footer class="footer mt-auto d-flex flex-shrink-0 justify-content-center align-items-center">
Жирнова Алена ПИбд-21 Жирнова Алена ПИбд-21
</footer> </footer>
<script type="module">
import validation from "./scripts/validation";
import { linesForm } from "./scripts/lines";
import {linesFormOnIndex} from "./scripts/lines";
document.addEventListener('DOMContentLoaded', () => {
validation();
linesFormOnIndex();
//linesForm();
});
</script>
</body> </body>
</html> </html>