diff --git a/app/components/orders/OrdersKanban.vue b/app/components/orders/OrdersKanban.vue
index baab935..d2427bc 100644
--- a/app/components/orders/OrdersKanban.vue
+++ b/app/components/orders/OrdersKanban.vue
@@ -34,7 +34,7 @@ const emit = defineEmits(["selectOrder", "unselect"]);
-
+
@@ -69,7 +69,7 @@ async function removeFromCart() {
{
const newCart = await $insertapi('Cart', { notify: false });
activeCartId = newCart.id;
- getCarts();
+ posStore.getCarts();
}
"
class="button is-primary"
diff --git a/app/components/pos/VietQR.vue b/app/components/pos/VietQR.vue
index 1a38dec..8e027ce 100644
--- a/app/components/pos/VietQR.vue
+++ b/app/components/pos/VietQR.vue
@@ -3,7 +3,7 @@ const props = defineProps({
invoiceId: Number,
finalizeOrder: Function,
});
-const { activeCartItems } = inject("pos");
+const { activeCartItems } = useActiveCart();
const subtotal = computed(() => {
return activeCartItems.value?.reduce((prev, curr) => prev + curr.imei__variant__price, 0);
});
diff --git a/app/components/pos/composables/useActiveCart.js b/app/components/pos/composables/useActiveCart.js
index 026b45f..192a4a8 100644
--- a/app/components/pos/composables/useActiveCart.js
+++ b/app/components/pos/composables/useActiveCart.js
@@ -1,21 +1,12 @@
import { without } from "es-toolkit";
-export default function useActiveCart({ carts, cartItems, customers }) {
- const activeCartId = ref();
+export default function useActiveCart() {
+ const posStore = usePosStore();
+ const { carts, cartItems, customers, activeCartId } = storeToRefs(posStore);
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);
@@ -27,10 +18,5 @@ export default function useActiveCart({ carts, cartItems, customers }) {
return activeCartItems.value?.reduce((prev, curr) => prev + curr.imei__variant__price, 0);
});
- const invalidCartItems = ref([]);
-
- watch(activeCartId, () => {
- invalidCartItems.value = [];
- });
- return { activeCartId, activeCart, activeCartItems, customerIdsWithNoCart, subtotal, invalidCartItems };
+ return { activeCart, activeCartItems, customerIdsWithNoCart, subtotal };
}
diff --git a/app/components/pos/composables/useCartData.js b/app/components/pos/composables/useCartData.js
deleted file mode 100644
index db78694..0000000
--- a/app/components/pos/composables/useCartData.js
+++ /dev/null
@@ -1,22 +0,0 @@
-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 };
-}
diff --git a/app/components/pos/composables/useCustomer.js b/app/components/pos/composables/useCustomer.js
deleted file mode 100644
index 4b0e07c..0000000
--- a/app/components/pos/composables/useCustomer.js
+++ /dev/null
@@ -1,24 +0,0 @@
-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 };
-}
diff --git a/app/components/pos/composables/useOrderInfo.js b/app/components/pos/composables/useOrderInfo.js
index 50ab13c..6657928 100644
--- a/app/components/pos/composables/useOrderInfo.js
+++ b/app/components/pos/composables/useOrderInfo.js
@@ -1,12 +1,9 @@
-export default function useOrderInfo({ activeCartId, activeCart, activeCartItems, addresses, getAddresses, subtotal }) {
- const orderInfo = ref({
- address: null,
- receiver_name: null,
- receiver_phone: null,
- shipping_fee: null,
- deliveryMethod: null,
- paymentMethod: null,
- });
+import useActiveCart from "~/components/pos/composables/useActiveCart";
+
+export default function useOrderInfo() {
+ const posStore = usePosStore();
+ const { activeCartId, addresses, orderInfo } = storeToRefs(posStore);
+ const { activeCart, activeCartItems, subtotal } = useActiveCart();
const total = computed(() => subtotal.value + orderInfo.value.shipping_fee);
@@ -19,7 +16,7 @@ export default function useOrderInfo({ activeCartId, activeCart, activeCartItems
watch(activeCart, async (newVal, oldVal) => {
// set order info
if (newVal?.customer) {
- await getAddresses();
+ await posStore.getAddresses(newVal.customer);
if (!oldVal || !oldVal.customer || oldVal.customer !== newVal.customer) {
const defaultAddress = addresses.value.find((add) => add.is_default);
orderInfo.value.address = defaultAddress;
@@ -59,5 +56,5 @@ export default function useOrderInfo({ activeCartId, activeCart, activeCartItems
return `${orderInfo.value.address.address_detail}, ${orderInfo.value.address.ward}, ${orderInfo.value.address.district}, ${orderInfo.value.address.city}`;
});
- return { orderInfo, isOrderValid, fullAddress, total };
+ return { isOrderValid, fullAddress, total };
}
diff --git a/app/components/pos/composables/usePlaceOrder.js b/app/components/pos/composables/usePlaceOrder.js
index b6b0252..190c179 100644
--- a/app/components/pos/composables/usePlaceOrder.js
+++ b/app/components/pos/composables/usePlaceOrder.js
@@ -1,15 +1,14 @@
-export default function usePlaceOrder({
- activeCart,
- activeCartItems,
- invalidCartItems,
- orderInfo,
- fullAddress,
- subtotal,
- total,
- getCarts,
- onSuccess,
-}) {
+import useActiveCart from "~/components/pos/composables/useActiveCart";
+import useOrderInfo from "~/components/pos/composables/useOrderInfo";
+
+export default function usePlaceOrder({ onSuccess }) {
const { $dayjs, $id, $getdata, $insertapi, $patchapi, $deleteapi, $snackbar } = useNuxtApp();
+
+ const posStore = usePosStore();
+ const { invalidCartItems, orderInfo } = storeToRefs(posStore);
+ const { activeCart, activeCartItems, subtotal } = useActiveCart();
+ const { fullAddress, total } = useOrderInfo();
+
const isPending = ref(false);
const paidPaymentStatus = ref();
const invoice = ref();
@@ -206,7 +205,7 @@ export default function usePlaceOrder({
customer: null,
}),
]);
- getCarts();
+ posStore.getCarts();
}
async function placeOrder() {
diff --git a/app/stores/usePosStore.js b/app/stores/usePosStore.js
new file mode 100644
index 0000000..5a647d3
--- /dev/null
+++ b/app/stores/usePosStore.js
@@ -0,0 +1,82 @@
+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,
+ };
+});