feat: create new order
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {defineStore} from "pinia";
|
||||
import {isNotEmpty} from "@/helpers.js";
|
||||
import {apiFetch} from "@/utils/ftch.js";
|
||||
import {addToCart, cartEditItem, cartRemoveItem, getCart} from "@/utils/ftch.js";
|
||||
|
||||
export const useCartStore = defineStore('cart', {
|
||||
state: () => ({
|
||||
@@ -26,7 +26,7 @@ export const useCartStore = defineStore('cart', {
|
||||
async getProducts() {
|
||||
try {
|
||||
this.isLoading = true;
|
||||
const data = await apiFetch('/index.php?route=extension/tgshop/handle/cart');
|
||||
const {data} = await getCart();
|
||||
this.items = data.products;
|
||||
this.productsCount = data.total_products_count;
|
||||
this.totals = data.totals;
|
||||
@@ -62,10 +62,7 @@ export const useCartStore = defineStore('cart', {
|
||||
}
|
||||
})
|
||||
|
||||
const response = await apiFetch('/index.php?route=checkout/cart/add', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
const response = await addToCart(formData);
|
||||
|
||||
if (response.error) {
|
||||
throw new Error(JSON.stringify(response.error));
|
||||
@@ -85,10 +82,7 @@ export const useCartStore = defineStore('cart', {
|
||||
this.isLoading = true;
|
||||
const formData = new FormData();
|
||||
formData.append('key', rowId);
|
||||
await apiFetch('/index.php?route=checkout/cart/remove', {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
await cartRemoveItem(formData);
|
||||
await this.getProducts();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
@@ -102,11 +96,7 @@ export const useCartStore = defineStore('cart', {
|
||||
this.isLoading = true;
|
||||
const formData = new FormData();
|
||||
formData.append(`quantity[${cartId}]`, quantity);
|
||||
await apiFetch('/index.php?route=checkout/cart/edit', {
|
||||
redirect: 'manual',
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
await cartEditItem(formData);
|
||||
await this.getProducts();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
@@ -115,4 +105,4 @@ export const useCartStore = defineStore('cart', {
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
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