Implement ordering process

This commit is contained in:
2024-09-24 15:40:16 +02:00
parent 5b8f1fbead
commit 8329a6ae09
19 changed files with 274 additions and 160 deletions

View File

@@ -6,6 +6,7 @@ import { useFeedbackStore } from "./feedbackStore";
import { loginAccount, registerAccount, updateAccount } from "../api/accountApi";
import { getUserOrders } from "../api/orderApi";
import { BannerStateEnum } from "../enums/bannerStateEnum";
import { calcPrice } from "@/scripts/productScripts";
export const useAccountStore = defineStore("accountStore", {
state: () => ({
@@ -20,18 +21,12 @@ export const useAccountStore = defineStore("accountStore", {
await loginAccount(username, password)
.then(async result => {
this.userAccount = result.data
this.loggedIn = true
feedbackStore.changeBanner(BannerStateEnum.ACCOUNTLOGINSUCCESSFUL)
await getUserOrders(result.data.id)
.then(result => {
this.orders = result.data
})
this.refreshOrders()
})
.catch(error => {
this.loggedIn = false
if (error.status == 400) {
feedbackStore.changeBanner(BannerStateEnum.ACCOUNTLOGINERROR)
} else if (error.status == 401) {
@@ -76,6 +71,24 @@ export const useAccountStore = defineStore("accountStore", {
this.loggedIn = false
feedbackStore.changeBanner(BannerStateEnum.ACCOUNTLOGOUTSUCCESSFUL)
},
async refreshOrders() {
await getUserOrders(this.userAccount.id)
.then(result => {
this.orders = result.data
})
},
getOrderTotalPrice(orderId: number) {
let totalPrice = 0
let order: OrderModel = this.orders.find((order: OrderModel) => order.id == orderId)
for (let item of order.orderItems) {
totalPrice += calcPrice(item.orderPrice, 0, item.quantity)
}
return Math.round(totalPrice * 100) / 100
}
}
})

View File

@@ -1,11 +1,13 @@
import { defineStore } from "pinia";
import { useLocalStorage } from "@vueuse/core";
import { calcProductPrice } from "@/scripts/productScripts";
import { calcPrice } from "@/scripts/productScripts";
import { BasketItemModel } from "../models/basketItemModel";
import { useFeedbackStore } from "./feedbackStore";
import { BannerStateEnum } from "../enums/bannerStateEnum";
import { addOrder } from "../api/orderApi";
import { useAccountStore } from "./accountStore";
import { ProductModel } from "../models/productModel";
import { useProductStore } from "./productStore";
export const useBasketStore = defineStore('basketStore', {
state: () => ({
@@ -13,11 +15,16 @@ export const useBasketStore = defineStore('basketStore', {
}),
getters: {
/**
* Calculate price of all items in the basket with discount
*
* @returns Total price of basket
*/
getTotalPrice() {
let result = 0
for (let item of this.itemsInBasket) {
result += calcProductPrice(item, item.quantity)
result += calcPrice(item.product.price, item.product.discount, item.quantity)
}
return Math.round(result * 100) / 100
@@ -25,6 +32,11 @@ export const useBasketStore = defineStore('basketStore', {
},
actions: {
/**
* Remove an item from the basket
*
* @param item Item to remove
*/
removeItemFromBasket(item: BasketItemModel) {
const feedbackStore = useFeedbackStore()
feedbackStore.changeBanner(BannerStateEnum.BASKETPRODUCTREMOVED)
@@ -34,29 +46,39 @@ export const useBasketStore = defineStore('basketStore', {
)
},
addItemToBasket(item: BasketItemModel) {
/**
* Add an item to the basket. If the product is already in the basket, the quantity will increase
*
* @param product Product to add
* @param quantity Quantity of the product
*/
addItemToBasket(product: ProductModel, quantity: number) {
const feedbackStore = useFeedbackStore()
feedbackStore.changeBanner(BannerStateEnum.BASKETPRODUCTADDED)
// Product is already in the basket, increase number of items
if (this.itemsInBasket.find((basketItem) => basketItem.productId == item.product.id)) {
this.itemsInBasket.find((basketItem) =>
basketItem.productId == item.product.id).quantity += item.quantity
if (this.itemsInBasket.find((basketItem: BasketItemModel) =>
basketItem.product.id == product.id))
{
this.itemsInBasket.find((basketItem: BasketItemModel) =>
basketItem.product.id == product.id).quantity += quantity
} else {
this.itemsInBasket.push(item)
this.itemsInBasket.push(new BasketItemModel(quantity, product))
}
},
takeOrder() {
/**
* Take an order to the server. Sends all articles in the basket and creates an order entry in the backend database
*/
async takeOrder() {
const accountStore = useAccountStore()
//
// const order = new OrderModel()
// order.accountId = userStore.userAccount.id
// order.orderItem = this.itemsInBasket
//
// console.log(order)
const productStore = useProductStore()
addOrder(accountStore.userAccount.id, this.itemsInBasket)
await addOrder(accountStore.userAccount.id, this.itemsInBasket)
this.itemsInBasket = []
await accountStore.refreshOrders()
await productStore.fetchAllProducts()
}
}
})