This commit is contained in:
Viet An
2026-06-16 11:35:43 +07:00
parent a9c37cfff5
commit a8aa9a3dce
6 changed files with 153 additions and 109 deletions

View File

@@ -0,0 +1,31 @@
import { without } from "es-toolkit";
export default function useActiveCart({ carts, cartItems, customers }) {
const activeCartId = ref();
const activeCart = computed(() => carts.value.find((c) => c.id === activeCartId.value));
const activeCartItems = computed(() => cartItems.value.filter((ci) => ci.cart === activeCartId.value));
watch(
carts,
(newVal) => {
if (!activeCartId.value && newVal.length) {
activeCartId.value = newVal[0].id;
}
},
{ immediate: true },
);
const customerIdsWithNoCart = computed(() => {
const cusIds = customers.value.map((c) => c.id);
const cusIdsWithCart = carts.value.filter((c) => c.customer).map((c) => c.customer);
const cusIdsWithNoCart = without(cusIds, ...cusIdsWithCart);
return cusIdsWithNoCart;
});
const subtotal = computed(() => {
return activeCartItems.value?.reduce((prev, curr) => prev + curr.imei__variant__price, 0);
});
return { activeCartId, activeCart, activeCartItems, customerIdsWithNoCart, subtotal };
}

View File

@@ -0,0 +1,22 @@
export default function useCartData() {
const { $findapi, $getapi } = useNuxtApp();
const carts = ref([]);
const cartItems = ref([]);
const customers = ref([]);
const isPending = ref(false);
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);
return { carts, cartItems, customers, getCarts, isPending };
}

View File

@@ -0,0 +1,23 @@
export default function useCustomer({ activeCart, getCarts }) {
const { $getdata, $patchapi } = useNuxtApp();
const addresses = ref([]);
async function getAddresses() {
addresses.value = await $getdata("Customer_Address", {
filter: { customer: activeCart.value.customer },
});
}
const isChangingCus = ref(false);
async function changeCustomer(cusId) {
isChangingCus.value = true;
const updatedCart = await $patchapi("Cart", {
id: activeCart.value.id,
customer: cusId,
});
await getCarts();
isChangingCus.value = false;
}
return { addresses, getAddresses, isChangingCus, changeCustomer };
}

View File

@@ -0,0 +1,49 @@
export default function useOrderInfo({ activeCartId, activeCart, activeCartItems, addresses, getAddresses }) {
const orderInfo = ref({
address: null,
deliveryMethod: null,
paymentMethod: null,
});
watch(activeCartId, () => {
// reset fields when tab is switched
orderInfo.value.deliveryMethod = null;
orderInfo.value.paymentMethod = null;
});
watch(activeCart, async (newVal, oldVal) => {
// set order info
if (newVal?.customer) {
await getAddresses();
if (!oldVal || !oldVal.customer || oldVal.customer !== newVal.customer) {
const defaultAddress = addresses.value.find((add) => add.is_default);
orderInfo.value.address = defaultAddress;
}
} else {
addresses.value = null;
orderInfo.value.address = null;
}
});
watch(
() => orderInfo.value.deliveryMethod,
(newVal) => {
if (newVal?.code === "HOME_DELIVERY") {
const defaultAddress = addresses.value.find((add) => add.is_default);
orderInfo.value.address = defaultAddress;
}
},
);
const isOrderValid = computed(() => {
if (activeCartItems.value?.length === 0) return false;
if (!activeCart.value?.customer) return false;
if (!orderInfo.value.deliveryMethod) return false;
if (!orderInfo.value.paymentMethod) return false;
if (orderInfo.value.deliveryMethod.code === "HOME_DELIVERY" && !orderInfo.value.address) return false;
return true;
});
return { orderInfo, isOrderValid };
}