Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
let toBasketBtns = document.querySelectorAll('.toBasketBtn');
toBasketBtns.forEach(function (btn) {
btn.addEventListener('click', function (event) {
let id = event.srcElement.dataset.id;
let price = event.srcElement.dataset.price;
let name = event.srcElement.dataset.name;
basket.addProduct({ id: id, price: price, name: name });
});
});

function get_prod_name() {
const entities = ['phone', 'Notebook', 'tv'];
const colors = ['Yellow', 'Black', 'Green', 'White', 'Blue'];
const title = colors[randomInt(0, colors.length - 1)] + ' ' + entities[randomInt(0, entities.length - 1)];
return title
Comment on lines +14 to +15
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

переменная title избыточна, можно было сразу вернуть все то, что присваивается title

}


function randomInt(min, max) {
let rand = min + Math.random() * (max + 1 - min);
return Math.floor(rand);
}


function render_prod_name() {
let all_names = document.querySelectorAll('.prod_name')
for (i=0;i<all_names.length;i++){
all_names[i].textContent = this.get_prod_name()
}
}

render_prod_name()


let basket = {
products: {},
addProduct(product) {
this.addProductToObject(product);
this.renderProductInBasket(product);
this.renderTotalSum();
this.addRemoveBtnsListeners();
},


addRemoveBtnsListeners() {
let btns = document.querySelectorAll('.productRemoveBtn');
for (let i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', this.removeProductListener);
}
},


renderTotalSum() {
document.querySelector('.total').textContent = this.getTotalSum();
},


getTotalSum() {
let sum = 0;
for (let key in this.products) {
sum += this.products[key].price * this.products[key].count;
}
return sum;
},


addProductToObject(product) {
if (this.products[product.id] == undefined) {
this.products[product.id] = {
price: product.price,
name: product.name,
count: 1
}
} else {
this.products[product.id].count++;
}
},

renderProductInBasket(product) {
let productExist = document.querySelector(`.productCount[data-id="${product.id}"]`);
if (productExist) {
productExist.textContent++;
return;
}
let = productRow = `
<tr>
Comment on lines +85 to +86
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

после let знак равно лишний

<th scope = "row">${product.id}</th>
<td>${product.name}</td>
<td>${product.price}</td>
<td class="productCount" data-id="${product.id}">1</td>
<td><i class="fas fa-trash-alt productRemoveBtn" data-id="${product.id}"></i></td>
</tr>
`;
let tbody = document.querySelector('tbody');
tbody.insertAdjacentHTML('beforeend', productRow);
},
};
82 changes: 82 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="styles/style.css">

</head>
<body>
<div class="container">
<nav class="navbar navbar-light bg-dark">
<div class="btn-group ml-auto">
<div class="dropdown-menu dropdown-menu-right basketPanel">
<table class="table table-hover">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Имя</th>
<th scope="col">Цена</th>
<th scope="col">Количество</th>
<th scope="col"></th>
</tr>
</thead>
<tbody></tbody>
<tfoot>
<tr>
<th colspan="2" scope="row">Итого:</th>
<td colspan="3">
<span class="total">0</span>
<i class="fas fa-ruble-sign"></i>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
</nav>

<br>


<div class="row">
<div class="col-4">
<div class="prod_name"></div>
<img src="https://placeimg.com/300/200/tech">
<div>
<span class="productPrice">120</span>
<i class="fas fa-ruble-sign"></i>
</div>
<button type="button" class="btn btn-success mt-3 toBasketBtn" data-id="1" data-price="120" data-name="Товар 1">
В корзину
</button>
</div>
<div class="prod_name"></div>
<img src="https://placeimg.com/300/200/tech/sepia">
<div>
<span class="productPrice">75</span>
<i class="fas fa-ruble-sign"></i>
</div>
<button type="button" class="btn btn-success mt-3 toBasketBtn" data-id="2" data-price="75" data-name="Товар 2">
В корзину
</button>
</div>
<div class="col-4">
<div class="prod_name"></div>
<img src="https://placeimg.com/300/200/tech/grayscale">
<div class="productPrice"></div>
<button type="button" class="btn btn-success mt-3 toBasketBtn" data-id="3" data-price="3000"
data-name="Товар 3">
В корзину
</button>
</div>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script src='app.js'></script>
</body>

</html>
75 changes: 75 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}

header {
height: 80px;
background: #444;
color: white;
display: flex;
justify-content: space-between;
align-items: center;
padding: 20px;
}

main {
display: flex;
justify-content: space-between;
padding: 20px;
flex-wrap: wrap;
}

.product-card {
width: 30%;
padding: 10px;
border: 1px solid #444;
display: flex;
flex-direction: column;
align-items: center;
margin: 20px;
}

.product-card__img-wrapper{
display: flex;
justify-content: space-around;
margin: 20px;
}

.product-card__img--small {
width: 30%
}

.product-card__title {
font-size: 20px;
color: #444;
margin: 10px;
}

.product-card__price {
font-size: 16px;
color: firebrick;
}

.product-card__btn {
background: firebrick;
margin: 10px;
padding: 10px 20px;
color: white;
border: 1px solid firebrick;
cursor: pointer;
}

.product-card__btn:hover {
background: white;
color: firebrick;
}
.bucketPanel {
width: 400px;
padding: 10px;
}

.productRemoveBtn:hover {
cursor: pointer;
}