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
122 changes: 122 additions & 0 deletions hw3/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
'use strict';

const API_URL = 'https://raw.githubusercontent.com/GeekBrainsTutorial/online-store-api/master/responses/catalogData.json';

function makeGETRequest(url) {
return new Promise(function (resolve, reject) {
let xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open('GET', url, true);

xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
xhr.status >= 200 && xhr.status < 300 ? resolve(xhr.responseText) : reject(`${xhr.statusText}, ${xhr.responseText}`);
}
};
xhr.send();
})
}

class GoodsItem {
constructor(product_name, price) {
this.product_name = product_name;
this.price = price;
}
render() {
return `<div class="goods-item"><h5>${this.product_name}</h5><p>${this.price} руб.</p><button class="addButton" data-name="${this.product_name}">Добавить</button><button class="removeButton" data-name="${this.product_name}">Удалить</button></div>`;
}
}

class BasketItem {
constructor(product_name, price) {
this.product_name = product_name;
this.price = price;
}

renderBasket() {
return `<div class="basket-item"><h6>${this.product_name}</h6><p>${this.price} руб.</p></div>`;
}
}

class GoodsList {
constructor() {
this.goods = [];
this.basket = [];
}
fetchGoods() {
makeGETRequest(API_URL)
.then(response => {
this.goods = JSON.parse(response)
return this.goods;
})
.then(() => this.render())
.then(() => this.renderBasketList())
.then(() => this.addButtonsClick())
.then(() => this.removeButtonClick())
.catch(error => { throw new Error(error) })
}

render() {
let listHtml = '';
this.goods.forEach(good => {
const goodItem = new GoodsItem(good.product_name, good.price);
listHtml += goodItem.render();
});
document.querySelector('.goods-list').innerHTML = listHtml;
}

renderBasketList() {
let basketHTML = '';
this.basket.forEach(item => {
const basketItem = new BasketItem(item.product_name, item.price);
basketHTML += basketItem.renderBasket();
});
document.querySelector('.basket').innerHTML = basketHTML;
}

addButtonsClick() {
let addButtons = document.querySelectorAll('.addButton');
addButtons.forEach(item => {
item.addEventListener('click', (event) => this.addItem(event, this.goods));
})
}

removeButtonClick() {
let removeButtons = document.querySelectorAll('.removeButton');
removeButtons.forEach(item => {
item.addEventListener('click', (event) => this.removeItem(event, this.basket))
})
}

addItem(event, goods) {
let targetItem = goods.find(item => {
return item.product_name === event.target.dataset.name;
})
this.basket.push(targetItem);
this.renderBasketList();
}

removeItem(event, basket) {
let targetItem = basket.find(item => {
return item.product_name === event.target.dataset.name;
})
let i = this.basket.indexOf(targetItem);
this.basket.splice(i, 1);
this.renderBasketList();
}

// sumGoods() {
// let sum = 0;
// this.goods.forEach(item => {
// sum += parseInt(item.price);
// })
// return sum;
// }
}

const list = new GoodsList();
list.fetchGoods()
42 changes: 42 additions & 0 deletions hw3/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
<title>e-Shop</title>
</head>

<body>
<header>
<div class="dropdown mydropdown">
<button class="cart-button dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">Корзина</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="#">Ваша корзина</a>
<div class="basket"></div>
</div>
</div>
</header>
<main>
<div class="goods-list"></div>
</main>

<script src="code.js"></script>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
</body>

</html>
96 changes: 96 additions & 0 deletions hw3/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
body {
margin: auto;
width: 80%;
}

header {
background: lightgrey;
max-width: 100%;
min-height: 80px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 1);
position: relative;
}

.cart-button {
position: absolute;
right: 35px;
top: 18px;
width: 140px;
height: 40px;
border: 0.1px solid grey;
border-radius: 10px;
box-shadow: 0 0 40px 40px #FF8C00 inset, 0 0 0 0 #FF4500;
font-weight: bold;
letter-spacing: 2px;
color: white;
transition: .15s ease-in-out;
}

.cart-button:hover {
box-shadow: 0 0 10px 0 #FF4500 inset, 0 0 10px 4px #FF4500;
color: #FF4500;
cursor: pointer;
}

.goods-list {
margin-top: 20px;
margin: 20px auto auto;
max-width: 80%;
box-shadow: 0 0 10px 5px grey inset, 0 0 0 0 grey;
min-height: 400px;
border-radius: 15px;
}

.goods-item {
float: left;
display: inline-block;
border-radius: 10px;
margin-left: 30px;
margin-top: 30px;
min-width: 27%;
height: 130px;
text-align: center;
}

.goods-item h5 {
margin-top: 10px;
}

.basket {
width: 100%;
}

.addButton,
.removeButton {
border-radius: 3px;
background-color: transparent;
letter-spacing: 2px;
transition: .15s ease-in-out;
}

.addButton {
border: 0px solid green;
color: green;
}

.removeButton {
border: 0px solid darkred;
color: red;
}

.addButton:hover {
box-shadow: 0 0 5px 0 lightgreen inset, 0 0 5px darkgreen;
color: darkgreen;
cursor: pointer;
}

.removeButton:hover {
box-shadow: 0 0 5px 0 #DB5E5E inset, 0 0 5px #970000;
color: darkred;
cursor: pointer;
}

.basket-item {
text-align: center;
}