Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
17 lines (17 loc) · 4.94 KB
/
Copy pathscript.js
File metadata and controls
17 lines (17 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
"use strict";
const $=s=>document.querySelector(s),products=[
{id:1,name:"Wireless Headphones",category:"Electronics",price:89,emoji:"🎧",description:"Wireless sound with clean design."},
{id:2,name:"Mechanical Keyboard",category:"Electronics",price:129,emoji:"⌨️",description:"Comfortable mechanical switches."},
{id:3,name:"Smart Desk Lamp",category:"Home",price:45,emoji:"💡",description:"Adjustable lighting for work."},
{id:4,name:"Travel Backpack",category:"Accessories",price:64,emoji:"🎒",description:"Practical design for every trip."},
{id:5,name:"Ceramic Coffee Mug",category:"Home",price:22,emoji:"☕",description:"Minimal everyday companion."},
{id:6,name:"USB-C Docking Hub",category:"Electronics",price:79,emoji:"🔌",description:"Connect your workspace essentials."},
{id:7,name:"Digital Wristwatch",category:"Accessories",price:58,emoji:"⌚",description:"Simple and stylish accessory."},
{id:8,name:"Indoor Plant Set",category:"Home",price:36,emoji:"🪴",description:"Fresh green accents for a room."},
{id:9,name:"Laptop Sleeve",category:"Accessories",price:29,emoji:"💻",description:"Soft protection for a laptop."}
];const money=n=>"$"+n.toFixed(2),key="novaShopCart";let cart={};try{const parsed=JSON.parse(localStorage.getItem(key)||"{}");if(parsed&&typeof parsed==="object"&&!Array.isArray(parsed))cart=parsed}catch(e){}
function store(){localStorage.setItem(key,JSON.stringify(cart));renderCart()}
function renderProducts(){const term=$("#search").value.toLowerCase().trim(),category=$("#category").value;let rows=products.filter(p=>p.name.toLowerCase().includes(term)&&(category==="All"||category===p.category));if($("#sort").value==="low")rows.sort((a,b)=>a.price-b.price);if($("#sort").value==="high")rows.sort((a,b)=>b.price-a.price);$("#products").replaceChildren();if(!rows.length){const p=document.createElement("p");p.textContent="No products match your search.";$("#products").append(p)}rows.forEach(p=>{const card=document.createElement("article");card.className="card";card.style.marginBottom="0";const emoji=document.createElement("div");emoji.style.cssText="background:#23334a;border-radius:14px;text-align:center;font-size:76px;padding:22px";emoji.textContent=p.emoji;const title=document.createElement("h3");title.style.marginTop="15px";title.textContent=p.name;const desc=document.createElement("p");desc.className="muted";desc.textContent=p.description;const category=document.createElement("span");category.className="pill";category.textContent=p.category;const footer=document.createElement("div");footer.className="between";footer.style.marginTop="20px";const price=document.createElement("strong");price.textContent=money(p.price);const button=document.createElement("button");button.className="btn";button.textContent="Add to cart";button.onclick=()=>{cart[p.id]=(Number(cart[p.id])||0)+1;store();button.textContent="Added ✓";setTimeout(()=>button.textContent="Add to cart",1100)};footer.append(price,button);card.append(emoji,title,desc,category,footer);$("#products").append(card)})}
function renderCart(){let count=0,total=0;$("#cartItems").replaceChildren();products.forEach(p=>{const qty=Number(cart[p.id]||0);if(!Number.isInteger(qty)||qty<=0)return;count+=qty;total+=qty*p.price;const row=document.createElement("div");row.className="line between";const text=document.createElement("span");text.textContent=p.emoji+" "+p.name+" × "+qty;const controls=document.createElement("div");controls.className="row";const less=document.createElement("button");less.className="btn secondary";less.textContent="−";less.setAttribute("aria-label","Remove one "+p.name);less.onclick=()=>{if(qty<=1)delete cart[p.id];else cart[p.id]=qty-1;store()};const more=document.createElement("button");more.className="btn secondary";more.textContent="+";more.setAttribute("aria-label","Add one "+p.name);more.onclick=()=>{cart[p.id]=qty+1;store()};controls.append(less,more);row.append(text,controls);$("#cartItems").append(row)});$("#cartCount").textContent=count;$("#total").textContent=money(total);$("#checkout").disabled=!count;if(!count){const empty=document.createElement("p");empty.className="muted";empty.textContent="Your cart is empty.";$("#cartItems").append(empty)}}
$("#search").oninput=renderProducts;$("#category").onchange=renderProducts;$("#sort").onchange=renderProducts;$("#openCart").onclick=()=>{$("#cartDialog").hidden=false;$("#closeCart").focus()};$("#closeCart").onclick=()=>{$("#cartDialog").hidden=true;$("#openCart").focus()};document.addEventListener("keydown",e=>{if(e.key==="Escape")$("#cartDialog").hidden=true});
$("#checkout").onclick=()=>{const order={createdAt:new Date().toISOString(),items:{...cart}};let history=[];try{const past=JSON.parse(localStorage.getItem("novaShopOrders")||"[]");if(Array.isArray(past))history=past}catch(e){}history.push(order);localStorage.setItem("novaShopOrders",JSON.stringify(history));cart={};store();$("#cartDialog").hidden=true;alert("Your demo order was saved locally. No real purchase has been made.")};renderProducts();renderCart();