feat: create new order
This commit is contained in:
44
spa/src/stores/CheckoutStore.js
Normal file
44
spa/src/stores/CheckoutStore.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import {defineStore} from "pinia";
|
||||
import {isNotEmpty} from "@/helpers.js";
|
||||
import {storeOrder} from "@/utils/ftch.js";
|
||||
import {useCartStore} from "@/stores/CartStore.js";
|
||||
|
||||
export const useCheckoutStore = defineStore('checkout', {
|
||||
state: () => ({
|
||||
customer: {
|
||||
firstName: "Иван",
|
||||
lastName: "Васильевич",
|
||||
email: "ival_vasil@mail.ru",
|
||||
phone: "+79999999999",
|
||||
address: "Москва, Красная площадь, 1",
|
||||
comment: "Доставить срочно❗️",
|
||||
},
|
||||
|
||||
validationErrors: {},
|
||||
}),
|
||||
|
||||
getters: {
|
||||
hasError: (state) => {
|
||||
return (field) => isNotEmpty(state.validationErrors[field]);
|
||||
},
|
||||
},
|
||||
|
||||
actions: {
|
||||
async makeOrder() {
|
||||
await storeOrder(this.customer)
|
||||
.catch(error => {
|
||||
if (error.response?.status === 422) {
|
||||
this.validationErrors = error.response._data.data;
|
||||
} else {
|
||||
console.error('Unexpected error', error);
|
||||
}
|
||||
});
|
||||
|
||||
await useCartStore().getProducts();
|
||||
},
|
||||
|
||||
clearError(field) {
|
||||
this.validationErrors[field] = null;
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user