New basket table, add empty state on basket page, new BasketItemModel

This commit is contained in:
2024-09-09 19:47:46 +02:00
parent 6ff577ece1
commit 7ebc3c1c77
14 changed files with 190 additions and 68 deletions

View File

@@ -0,0 +1,10 @@
export class BasketItemModel {
productId: number = -1
brand: string = ""
name: string = ""
categoryName: string = ""
categoryIcon: string = ""
price: number = 0
discount: number = 0
quantity: number = 1
}

View File

@@ -0,0 +1,6 @@
export class OrderedItemModel {
orderId: number = -1
productId: number = -1
quantity: number = 1
totalPrice: number = 0
}

View File

@@ -7,7 +7,6 @@ export class ProductModel {
price: number = 0
discount: number = 0
rating: number = 1
nrOfArticles: number = 2
imageUrl: string = ""
createdAt: string = ""
updatedAt: string = ""

View File

@@ -1,30 +1,39 @@
import { defineStore } from "pinia";
import { useLocalStorage } from "@vueuse/core";
import { ProductModel } from "../models/productModel";
import { calcProductPrice } from "@/scripts/productScripts";
import { BasketItemModel } from "../models/basketItemModel";
export const useBasketStore = defineStore('basket', {
state: () => ({
productsInBasket: useLocalStorage<Array<ProductModel>>("hackmycart/basketStore/productsInBasket", [])
itemsInBasket: useLocalStorage<Array<BasketItemModel>>("hackmycart/basketStore/productsInBasket", [])
}),
getters: {
getTotalPrice() {
let result = 0
for (let product of this.productsInBasket) {
result += calcProductPrice(product)
for (let item of this.itemsInBasket) {
result += calcProductPrice(item, item.quantity)
}
return result
return Math.round(result * 100) / 100
}
},
actions: {
removeProductFromBasket(product: ProductModel) {
this.productsInBasket = this.productsInBasket.filter((p: ProductModel) =>
p.id != product.id
removeItemFromBasket(item: BasketItemModel) {
this.itemsInBasket = this.itemsInBasket.filter((basketItemModel: BasketItemModel) =>
basketItemModel.productId != item.productId
)
},
addItemToBasket(item: BasketItemModel) {
// Product is already in the basket, increase number of items
if (this.itemsInBasket.find((basketItem) => basketItem.productId == item.productId)) {
this.itemsInBasket.find((basketItem) => basketItem.productId == item.productId).quantity += item.quantity
} else {
this.itemsInBasket.push(item)
}
}
}
})