New basket table, add empty state on basket page, new BasketItemModel
This commit is contained in:
10
software/src/data/models/basketItemModel.ts
Normal file
10
software/src/data/models/basketItemModel.ts
Normal 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
|
||||
}
|
||||
6
software/src/data/models/orderedItemModel.ts
Normal file
6
software/src/data/models/orderedItemModel.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export class OrderedItemModel {
|
||||
orderId: number = -1
|
||||
productId: number = -1
|
||||
quantity: number = 1
|
||||
totalPrice: number = 0
|
||||
}
|
||||
@@ -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 = ""
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user