مشروع سلة متجر تجريبية بالجافاسكريبت: الكود كامل + شرح

مشروع سلة متجر تجريبية بالجافاسكريبت: الكود كامل + شرح

غلاف مشروع سلة متجر تجريبية بالجافاسكريبت

مشروع 5: سلة متجر تجريبية بجافاسكريبت من الصفر

كتالوج منتجات، Map للكميات، زيادة وتقليل وإزالة، حد للمخزون، وإجمالي بالقروش من غير لخبطة.

جرّب تضيف منتجا أكتر من مرة، توصل للحد المتاح، وتقلل الكمية للصفر.

تنبيه: كل الأسعار تجريبية. لا دفع ولا طلب حقيقي، والسلة ترجع فاضية بعد الريلود. في متجر حقيقي الخادم يراجع الأسعار والمخزون.

إيه اللي هتتعلمه في المشروع ده

هتتعلم تبني سلة تجريبية: كتالوج منتجات، إضافة وإزالة، كميات بحد أقصى حسب المخزون، وإجمالي بيتحدّث تلقائي. ومفيش دفع حقيقي في المشروع. هو تدريب على تنظيم الحالة (الكميات والمخزون) وتحديث الشاشة منها، وهي الفكرة اللي بتتبني عليها المتاجر الحقيقية.

إزاي الكود شغال

المنتجات في مصفوفة، والسلة هي Map بتربط رقم المنتج بالكمية. دالة change بتحسب الكمية الجديدة وترفضها لو عدّت المخزون، وتحذف المنتج من السلة لو وصلت للصفر. الأسعار بالقروش، والإجمالي بيتحسب من جديد في كل render بدل ما يتخزن لوحده، ودالة money هي اللي بتحوّله لجنيه للعرض.

غلطة شائعة

إنك تخزّن الإجمالي في متغير منفصل وتحدّثه بإيدك، فبعد كذا عملية يبقى مختلف عن اللي في السلة. الأضمن تحسبه من محتوى السلة كل مرة. وغلطة تانية إنك تسمح بكمية أكبر من المتاح أو أقل من صفر.

تمرين تكمل بيه

ضيف منتج رابع بمخزون مختلف، وبعدها زرار "تفريغ" لمنتج واحد. وللتحدي، جرّب تضيف كود خصم 10% بيتحسب على الإجمالي.

طريقة التشغيل

  1. اعمل مجلد جديد وسمّه project5.
  2. انسخ كل ملف من الأسفل واحفظه بنفس اسمه داخل المجلد.
  3. افتح index.html في متصفح حديث. أو شغّل خادم محلي من المجلد الذي يحتوي project5:
    python3 -m http.server 8000
    على ويندوز: py -m http.server 8000
    وافتح: http://localhost:8000/project5/

index.html

<!doctype html>
<html lang="ar" dir="rtl"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>سلة متجر تجريبية | Ayoub EduTech</title><link rel="stylesheet" href="style.css">
<script src="app.js" defer></script></head><body><main>
<small>Ayoub EduTech • مشروع 05</small><h1>سلة متجر تجريبية</h1>
<small>أسعار وبيانات تجريبية، لا دفع ولا طلب حقيقي.</small>
<section id="products"></section><section><h2>سلتك</h2><ul id="cart"></ul>
<div class="row"><span>الإجمالي</span><output id="total">0.00 ج.م</output></div>
<button id="clear">تفريغ السلة</button></section>
<p id="status" role="status" aria-live="polite"></p>
<small>مشروع تعليمي محلي • JavaScript</small></main></body></html>

style.css

* {
  box-sizing:border-box
}
body {
  margin:0;
  background:#140b26;
  color:#f7f2ff;
  font:18px "Noto Sans Arabic",Arial;
  direction:rtl
}
main {
  max-width:740px;
  margin:32px auto;
  padding:24px
}
h1 {
  font-size:30px;
  margin:8px 0
}
small {
  color:#c5b2de
}
section,article {
  background:#24133d;
  border:1px solid #614480;
  border-radius:18px;
  padding:20px;
  margin:18px 0
}
label {
  display:block;
  margin:12px 0
}
input,select,button {
  font:inherit;
  border-radius:10px;
  padding:10px;
  border:1px solid #8867a7
}
input,select {
  background:#10091b;
  color:white;
  width:100%
}
button {
  background:#f5ca52;
  color:#211234;
  cursor:pointer;
  margin:5px
}
button:disabled {
  opacity:.45;
  cursor:not-allowed
}
button:focus-visible,input:focus-visible,select:focus-visible {
  outline:3px solid #68e1d2
}
output,strong {
  color:#7ce6d5
}
.row {
  display:flex;
  gap:10px;
  align-items:center;
  justify-content:space-between
}
.row>* {
  min-width:0
}
ul {
  padding:0;
  list-style:none
}
li {
  padding:12px 0;
  border-bottom:1px solid #59416c
}
.error {
  color:#ffb6bb
}
#status {
  min-height:32px
}
#clock {
  font:64px monospace;
  text-align:center;
  direction:ltr
}
#choices button {
  display:block;
  width:100%;
  text-align:right
}
#choices button.correct {
  background:#68d7b8
}
#choices button.wrong {
  background:#ee9ca9
}
#products article {
  margin:10px 0
}
a {
  color:#7ce6d5
}
@media(max-width:500px) {
  main {
    padding:16px;
    margin:10px auto
  }
  .row {
    flex-wrap:wrap
  }
  h1 {
    font-size:26px
  }
}

app.js

const $ = id => document.getElementById(id);
const products = [
  { id: 1, name: 'دفتر', cents: 3500, stock: 3 },
  { id: 2, name: 'قلم', cents: 1250, stock: 5 },
  { id: 3, name: 'حقيبة', cents: 18000, stock: 2 }
];
const cart = new Map();
const money = cents => `${(cents / 100).toFixed(2)} ج.م`;
function change(id, delta) {
  const product = products.find(p => p.id === id);
  if (!product) return;
  const next = (cart.get(id) || 0) + delta;
  if (next > product.stock) { $('status').textContent = 'وصلت للكمية المتاحة.'; return; }
  if (next <= 0) cart.delete(id); else cart.set(id, next);
  $('status').textContent = 'تم تحديث السلة التجريبية'; render();
}
function button(label, action, disabled = false) {
  const b = document.createElement('button');
  b.textContent = label; b.disabled = disabled;
  b.addEventListener('click', action); return b;
}
function render() {
  $('products').replaceChildren(); $('cart').replaceChildren();
  let total = 0;
  products.forEach(product => {
    const quantity = cart.get(product.id) || 0;
    const article = document.createElement('article');
    const label = document.createElement('p');
    label.textContent = `${product.name} • ${money(product.cents)} • المتاح ${product.stock}`;
    article.append(label, button('أضف للسلة', () => change(product.id, 1), quantity >= product.stock));
    $('products').append(article);
    if (!quantity) return;
    const li = document.createElement('li');
    const text = document.createElement('span');
    text.textContent = `${product.name} × ${quantity} = ${money(product.cents * quantity)}`;
    li.append(text,
      button('+', () => change(product.id, 1), quantity >= product.stock),
      button('−', () => change(product.id, -1)),
      button('إزالة', () => { cart.delete(product.id); render(); }));
    $('cart').append(li); total += product.cents * quantity;
  });
  if (!cart.size) $('cart').textContent = 'السلة فارغة.';
  $('total').textContent = money(total); $('clear').disabled = !cart.size;
}
$('clear').addEventListener('click', () => {
  cart.clear(); $('status').textContent = 'تم تفريغ السلة'; render();
});
render();

تحميل المشاريع

تحميل ملف ZIP للمشاريع الخمسة (Google Drive)

تابع Ayoub EduTech وطبّق بإيدك.