wip: shopping cart

This commit is contained in:
Nikita Kiselev
2025-07-23 18:40:57 +03:00
parent bb2ee38118
commit 49d41747d3
22 changed files with 545 additions and 141 deletions

View File

@@ -1,25 +1,38 @@
import {defineStore} from "pinia";
import ftch from "@/utils/ftch.js";
import {$fetch} from "ofetch";
import {isNotEmpty} from "@/helpers.js";
import {apiFetch} from "@/utils/ftch.js";
export const useCartStore = defineStore('cart', {
state: () => ({
items: [],
products: [],
productsCount: 0,
total: 0,
isLoading: false,
reason: null,
error_warning: '',
attention: '',
success: '',
}),
getters: {
canCheckout: (state) => {
if (state.isLoading) {
return false;
}
},
},
actions: {
async getProducts() {
try {
this.isLoading = true;
const {data} = await ftch('cart');
this.products = data.products;
this.productsCount = data.count;
this.total = data.total;
const data = await apiFetch('/index.php?route=extension/tgshop/handle/cart');
this.items = data.products;
this.productsCount = data.total_products_count;
this.totals = data.totals;
this.error_warning = data.error_warning;
this.attention = data.attention;
this.success = data.success;
} catch (error) {
console.error(error);
} finally {
@@ -49,7 +62,7 @@ export const useCartStore = defineStore('cart', {
}
})
const response = await $fetch('/index.php?route=checkout/cart/add', {
const response = await apiFetch('/index.php?route=checkout/cart/add', {
method: 'POST',
body: formData,
});
@@ -72,7 +85,7 @@ export const useCartStore = defineStore('cart', {
this.isLoading = true;
const formData = new FormData();
formData.append('key', rowId);
await $fetch('/index.php?route=checkout/cart/remove', {
await apiFetch('/index.php?route=checkout/cart/remove', {
method: 'POST',
body: formData,
});
@@ -89,7 +102,7 @@ export const useCartStore = defineStore('cart', {
this.isLoading = true;
const formData = new FormData();
formData.append(`quantity[${cartId}]`, quantity);
await $fetch('/index.php?route=checkout/cart/edit', {
await apiFetch('/index.php?route=checkout/cart/edit', {
redirect: 'manual',
method: 'POST',
body: formData,