83 lines
1.8 KiB
JavaScript
83 lines
1.8 KiB
JavaScript
import { defineStore } from "pinia";
|
|
|
|
export const usePosStore = defineStore("pos", () => {
|
|
const { $findapi, $getapi, $getdata, $patchapi } = useNuxtApp();
|
|
|
|
const carts = ref([]);
|
|
const cartItems = ref([]);
|
|
const customers = ref([]);
|
|
const isPending = ref(false);
|
|
|
|
const activeCartId = ref();
|
|
const invalidCartItems = ref([]);
|
|
const isChangingCus = ref(false);
|
|
const addresses = ref([]);
|
|
const orderInfo = ref({
|
|
address: null,
|
|
receiver_name: null,
|
|
receiver_phone: null,
|
|
shipping_fee: null,
|
|
deliveryMethod: null,
|
|
paymentMethod: null,
|
|
});
|
|
|
|
async function getCarts() {
|
|
isPending.value = true;
|
|
const apis = $findapi(["Cart", "Cart_Item", "customer"]);
|
|
const [cartsRes, cartItemsRes, customersRes] = await $getapi(apis);
|
|
isPending.value = false;
|
|
|
|
carts.value = cartsRes.data.rows || [];
|
|
cartItems.value = cartItemsRes.data.rows || [];
|
|
customers.value = customersRes.data.rows || [];
|
|
}
|
|
|
|
onMounted(getCarts);
|
|
|
|
async function changeCustomer(cusId) {
|
|
isChangingCus.value = true;
|
|
const updatedCart = await $patchapi("Cart", {
|
|
id: activeCartId.value,
|
|
customer: cusId,
|
|
});
|
|
await getCarts();
|
|
isChangingCus.value = false;
|
|
}
|
|
|
|
async function getAddresses(activeCartCustomerId) {
|
|
addresses.value =
|
|
(await $getdata("Customer_Address", {
|
|
filter: { customer: activeCartCustomerId },
|
|
})) || [];
|
|
}
|
|
|
|
watch(
|
|
carts,
|
|
(newVal) => {
|
|
if (!activeCartId.value && newVal.length) {
|
|
activeCartId.value = newVal[0].id;
|
|
}
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
|
|
watch(activeCartId, () => {
|
|
invalidCartItems.value = [];
|
|
});
|
|
|
|
return {
|
|
carts,
|
|
cartItems,
|
|
customers,
|
|
isPending,
|
|
getCarts,
|
|
getAddresses,
|
|
changeCustomer,
|
|
activeCartId,
|
|
invalidCartItems,
|
|
isChangingCus,
|
|
addresses,
|
|
orderInfo,
|
|
};
|
|
});
|