From 229187dd4ec490b996a2e665b025769487074e82 Mon Sep 17 00:00:00 2001 From: Baryshev Dmitry Date: Thu, 25 Sep 2025 16:29:37 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=A0=D0=9E=D0=93=D0=A0=D0=95=D0=A1?= =?UTF-8?q?=D0=A1=20=D0=A3=D0=A0=D0=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basket.html | 52 +++++++++++++++++---------- catalog.html | 13 ++++--- dist/assets/controller-BWr64z2B.js | 49 +++++++++++++++++++++++++ dist/assets/page2-Cyv2CTPS.js | 1 + dist/assets/page5-Cyv2CTPS.js | 1 + dist/basket.html | 57 ++++++++++++++++-------------- dist/catalog.html | 18 +++------- 7 files changed, 129 insertions(+), 62 deletions(-) create mode 100644 dist/assets/controller-BWr64z2B.js create mode 100644 dist/assets/page2-Cyv2CTPS.js create mode 100644 dist/assets/page5-Cyv2CTPS.js diff --git a/basket.html b/basket.html index 5bf1195..003b571 100644 --- a/basket.html +++ b/basket.html @@ -8,6 +8,33 @@ + + - - -

Здесь будут лежать твои товары

@@ -101,12 +116,13 @@ - - - - - \ No newline at end of file + diff --git a/catalog.html b/catalog.html index fbd51ac..8ff1a32 100644 --- a/catalog.html +++ b/catalog.html @@ -354,12 +354,14 @@ }); - - - - + + + \ No newline at end of file diff --git a/dist/assets/controller-BWr64z2B.js b/dist/assets/controller-BWr64z2B.js new file mode 100644 index 0000000..ef97077 --- /dev/null +++ b/dist/assets/controller-BWr64z2B.js @@ -0,0 +1,49 @@ +(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const a of document.querySelectorAll('link[rel="modulepreload"]'))e(a);new MutationObserver(a=>{for(const i of a)if(i.type==="childList")for(const o of i.addedNodes)o.tagName==="LINK"&&o.rel==="modulepreload"&&e(o)}).observe(document,{childList:!0,subtree:!0});function s(a){const i={};return a.integrity&&(i.integrity=a.integrity),a.referrerPolicy&&(i.referrerPolicy=a.referrerPolicy),a.crossOrigin==="use-credentials"?i.credentials="include":a.crossOrigin==="anonymous"?i.credentials="omit":i.credentials="same-origin",i}function e(a){if(a.ep)return;a.ep=!0;const i=s(a);fetch(a.href,i)}})();class r{constructor(){this.apiUrl="http://localhost:3000"}async request(t,s={}){try{const e=await fetch(`${this.apiUrl}${t}`,{...s,headers:{"Content-Type":"application/json",...s.headers}});if(!e.ok)throw new Error(`HTTP error! status: ${e.status}`);return await e.json()}catch(e){throw console.error("Request failed:",e),e}}async getBasketItems(){try{return await this.request("/basket")}catch(t){return console.error("Ошибка при получении корзины:",t),[]}}async addToBasket(t){try{const e=(await this.getBasketItems()).find(a=>a.id===t.id);if(e)await this.updateBasketItem(t.id,e.quantity+1);else{const a={...t,quantity:1,addedAt:new Date().toISOString()};return await(await fetch(`${this.apiUrl}/basket`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(a)})).json()}}catch(s){console.error("Ошибка при добавлении в корзину:",s)}}async updateBasketItem(t,s){try{return await(await fetch(`${this.apiUrl}/basket/${t}`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({quantity:s})})).json()}catch(e){console.error("Ошибка при обновлении корзины:",e)}}async removeFromBasket(t){try{await fetch(`${this.apiUrl}/basket/${t}`,{method:"DELETE"})}catch(s){console.error("Ошибка при удалении из корзины:",s)}}async clearBasket(){try{const t=await this.getBasketItems();for(const s of t)await this.removeFromBasket(s.id)}catch(t){console.error("Ошибка при очистке корзины:",t)}}async getProducts(){try{return await(await fetch(`${this.apiUrl}/shmots`)).json()}catch(t){return console.error("Ошибка при получении товаров:",t),[]}}}class c{constructor(){this.basketContainer=document.getElementById("basketContainer"),this.emptyBasketElement=document.querySelector(".empty-basket")}showBasket(t){if(t.length===0){this.showEmptyBasket();return}this.hideEmptyBasket();const s=t.map(e=>this.createBasketItemHTML(e)).join("");this.basketContainer.innerHTML=` +
+
+
+
+
Корзина
+
+
+ ${s} +
+
Итого: $${this.calculateTotal(t).toFixed(2)}
+ +
+
+
+
+
+ `}createBasketItemHTML(t){return` +
+
+ ${t.name} +
+
+
${t.name}
+

${t.description}

+
+
+ $${t.price} +
+
+
+ + + +
+
+
+ $${(t.price*t.quantity).toFixed(2)} + +
+
+ `}showEmptyBasket(){this.emptyBasketElement&&(this.emptyBasketElement.style.display="block"),this.basketContainer&&(this.basketContainer.innerHTML="")}hideEmptyBasket(){this.emptyBasketElement&&(this.emptyBasketElement.style.display="none")}calculateTotal(t){return t.reduce((s,e)=>s+e.price*e.quantity,0)}showNotification(t,s="success"){const e=document.createElement("div");e.className=`alert alert-${s==="success"?"success":"danger"} alert-dismissible fade show`,e.style.cssText="position: fixed; top: 20px; right: 20px; z-index: 1050; min-width: 300px;",e.innerHTML=` + ${t} + + `,document.body.appendChild(e),setTimeout(()=>{e.parentNode&&e.remove()},3e3)}}class l{constructor(t,s){this.model=t,this.view=s,this.init()}async init(){window.location.pathname.includes("basket.html")&&(await this.loadBasket(),this.setupBasketEventListeners()),window.location.pathname.includes("catalog.html")&&this.setupCatalogEventListeners()}async loadBasket(){const t=await this.model.getBasketItems();this.view.showBasket(t)}setupBasketEventListeners(){document.addEventListener("click",async t=>{const s=t.target.closest(".basket-item");if(!s)return;const e=s.dataset.id;if(t.target.closest(".remove-btn")&&(await this.model.removeFromBasket(e),this.view.showNotification("Товар удален из корзины"),await this.loadBasket()),t.target.closest(".increase-btn")){const a=s.querySelector(".quantity-input"),i=parseInt(a.value)+1;a.value=i,await this.model.updateBasketItem(e,i),await this.loadBasket()}if(t.target.closest(".decrease-btn")){const a=s.querySelector(".quantity-input");let i=parseInt(a.value)-1;i<1&&(i=1),a.value=i,await this.model.updateBasketItem(e,i),await this.loadBasket()}}),document.addEventListener("change",async t=>{if(t.target.classList.contains("quantity-input")){const e=t.target.closest(".basket-item").dataset.id,a=parseInt(t.target.value)||1;if(a<1){t.target.value=1;return}await this.model.updateBasketItem(e,a),await this.loadBasket()}}),document.addEventListener("click",async t=>{if(t.target.id==="checkoutBtn"){if((await this.model.getBasketItems()).length===0){this.view.showNotification("Корзина пуста","error");return}this.view.showNotification("Заказ оформлен! Спасибо за покупку!"),await this.model.clearBasket(),await this.loadBasket()}})}setupCatalogEventListeners(){document.addEventListener("click",async t=>{if(t.target.closest(".btn")&&t.target.closest(".btn").textContent.includes("В корзину")){const s=t.target.closest(".card"),e=this.extractProductData(s);e&&(await this.model.addToBasket(e),this.view.showNotification("Товар добавлен в корзину!"))}})}extractProductData(t){try{const s=t.querySelector(".card-title").textContent,e=t.querySelector(".text-muted").textContent.replace("$",""),a=t.querySelector(".card-text").textContent,i=t.querySelector("img").src;return{id:btoa(`${s}-${e}`).substring(0,8),name:s.trim(),price:parseFloat(e),description:a.trim(),image:i}}catch(s){return console.error("Ошибка при извлечении данных товара:",s),null}}}export{l as C,r as M,c as V}; diff --git a/dist/assets/page2-Cyv2CTPS.js b/dist/assets/page2-Cyv2CTPS.js new file mode 100644 index 0000000..fe7ce6a --- /dev/null +++ b/dist/assets/page2-Cyv2CTPS.js @@ -0,0 +1 @@ +import{V as t,C as o,M as d}from"./controller-BWr64z2B.js";/* empty css */document.addEventListener("DOMContentLoaded",function(){if(document.getElementById("basketContainer")||document.getElementById("productsContainer")){const e=new d,n=new t;new o(e,n)}}); diff --git a/dist/assets/page5-Cyv2CTPS.js b/dist/assets/page5-Cyv2CTPS.js new file mode 100644 index 0000000..fe7ce6a --- /dev/null +++ b/dist/assets/page5-Cyv2CTPS.js @@ -0,0 +1 @@ +import{V as t,C as o,M as d}from"./controller-BWr64z2B.js";/* empty css */document.addEventListener("DOMContentLoaded",function(){if(document.getElementById("basketContainer")||document.getElementById("productsContainer")){const e=new d,n=new t;new o(e,n)}}); diff --git a/dist/basket.html b/dist/basket.html index d886d49..948494b 100644 --- a/dist/basket.html +++ b/dist/basket.html @@ -7,6 +7,35 @@ + + + + @@ -46,19 +75,7 @@
- - -

Здесь будут лежать твои товары

@@ -101,18 +118,6 @@ - - - - + - \ No newline at end of file + diff --git a/dist/catalog.html b/dist/catalog.html index e520ca1..62e7572 100644 --- a/dist/catalog.html +++ b/dist/catalog.html @@ -26,6 +26,8 @@ transition: text-shadow 0.2s ease; } + + @@ -354,18 +356,8 @@ }); - - - - + + + \ No newline at end of file