Skip to content

Commit

Permalink
Add the function of adding items to the cart
Browse files Browse the repository at this point in the history
  • Loading branch information
rayc2045 committed May 10, 2024
1 parent 9cd01fa commit c285962
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const prefer = {

const STORAGE_KEY = `${BRAND_NAME.replaceAll(' ', '-')}-hyper-app`;
const localStore = {
fetch() {
return JSON.parse(localStorage.getItem(STORAGE_KEY));
get(name) {
return JSON.parse(localStorage.getItem(`${STORAGE_KEY}-${name}`));
},
save(id) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(id));
set(name, data) {
localStorage.setItem(`${STORAGE_KEY}-${name}`, JSON.stringify(data));
},
remove() {
localStorage.removeItem(STORAGE_KEY);
remove(name) {
localStorage.removeItem(`${STORAGE_KEY}-${name}`);
},
};

Expand Down Expand Up @@ -338,9 +338,11 @@ const shop = {
};

const cart = {
items: [],
storeName: 'cart-items',
items: localStore.get(this.storeName) || [],
addItem(item) {
this.items.push(item);
localStore.set(this.storeName, cart.items);
},
};

Expand Down
8 changes: 8 additions & 0 deletions src/pages/shop/shop.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@
x-html="svg('bag')"
class="mt-4 ml-1"
:class="btn.roundedLight"
@click="cart.addItem({
name: product.name,
color: currentColor,
category: product.category,
price: product.price,
discount: product.discount,
SKU: product.SKU,
})"
></button>
</template>
</div>
Expand Down

0 comments on commit c285962

Please sign in to comment.