32 lines
1.0 KiB
JavaScript
32 lines
1.0 KiB
JavaScript
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 };
|
|
}
|