Skip to content
39 changes: 38 additions & 1 deletion 8-module/1-task/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,43 @@ export default class CartIcon {
}

updatePosition() {
// ваш код ...
if (!this.elem.offsetWidth || !this.elem.offsetHeight) return;

if (!this.initialTopCoord) {
this.initialTopCoord = this.elem.getBoundingClientRect().top + window.pageYOffset;
}

if (document.documentElement.clientWidth <= 767) {
Object.assign(this.elem.style, {
position: '',
top: '',
left: '',
zIndex: ''
});
return;
}

if (window.pageYOffset > this.initialTopCoord) {
const containerRight = document.querySelector('.container').getBoundingClientRect().right;

const leftIndent = Math.min(
containerRight + 20,
document.documentElement.clientWidth - this.elem.offsetWidth - 10
) + 'px';

Object.assign(this.elem.style, {
position: 'fixed',
top: '50px',
zIndex: 1000,
left: leftIndent
});
} else {
Object.assign(this.elem.style, {
position: '',
top: '',
left: '',
zIndex: ''
});
}
}
}
52 changes: 52 additions & 0 deletions 8-module/2-task/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,57 @@ export default class ProductGrid {
constructor(products) {
this.products = products;
this.filters = {};
this.elem = this._render();
this._renderCards(this.products);
}

_render() {
return createElement(`
<div class="products-grid">
<div class="products-grid__inner"></div>
</div>
`);
}

_renderCards(products) {
const container = this.elem.querySelector('.products-grid__inner');
container.innerHTML = '';

for (let product of products) {
const card = new ProductCard(product);
container.append(card.elem);
}
}

updateFilter(filters) {
Object.assign(this.filters, filters);

let filtered = this.products.filter(product => {
if (this.filters.noNuts && product.nuts) {
return false;
}

if (this.filters.vegeterianOnly && !product.vegeterian) {
return false;
}

if (
this.filters.maxSpiciness !== undefined &&
product.spiciness > this.filters.maxSpiciness
) {
return false;
}

if (
this.filters.category &&
product.category !== this.filters.category
) {
return false;
}

return true;
});

this._renderCards(filtered);
}
}
34 changes: 26 additions & 8 deletions 8-module/3-task/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
export default class Cart {
cartItems = []; // [product: {...}, count: N]
cartItems = [];

constructor(cartIcon) {
this.cartIcon = cartIcon;
}

addProduct(product) {
// ваш код
if (!product) return;

let cartItem = this.cartItems.find(item => item.product.id === product.id);

if (cartItem) {
cartItem.count++;
} else {
cartItem = { product, count: 1 };
this.cartItems.push(cartItem);
}

this.onProductUpdate(cartItem);
}

updateProductCount(productId, amount) {
// ваш код
let cartItem = this.cartItems.find(item => item.product.id === productId);
if (!cartItem) return;

cartItem.count += amount;

if (cartItem.count === 0) {
this.cartItems = this.cartItems.filter(item => item.product.id !== productId);
}

this.onProductUpdate(cartItem);
}

isEmpty() {
// ваш код
return this.cartItems.length === 0;
}

getTotalCount() {
// ваш код
return this.cartItems.reduce((total, item) => total + item.count, 0);
}

getTotalPrice() {
// ваш код
return this.cartItems.reduce((total, item) => total + item.product.price * item.count, 0);
}

onProductUpdate(cartItem) {
// реализуем в следующей задаче

this.cartIcon.update(this);
}
}
Expand Down
Loading
Loading