From 5caf82fad7adabebcd32d47729f948294ef90997 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 13 Dec 2024 13:32:40 +0100 Subject: [PATCH] fix: get total from api --- .../Order/Card/OrderCatalogItemDialog.vue | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/pages/Order/Card/OrderCatalogItemDialog.vue b/src/pages/Order/Card/OrderCatalogItemDialog.vue index 39b0b0120..0d55b7de1 100644 --- a/src/pages/Order/Card/OrderCatalogItemDialog.vue +++ b/src/pages/Order/Card/OrderCatalogItemDialog.vue @@ -20,21 +20,15 @@ const props = defineProps({ }); const state = useState(); -const total = computed(() => state.get('orderTotal')); +const orderData = computed(() => state.get('orderData')); const prices = ref((props.item.prices || []).map((item) => ({ ...item, quantity: 0 }))); const isLoading = ref(false); -const calculateTotals = (items) => { - return items.reduce( - (acc, item) => { - acc.totalQuantity += item.quantity; - acc.totalSum += item.quantity * item.price; - return acc; - }, - { totalQuantity: 0, totalSum: 0 } - ); -}; +const totalQuantity = (items) => + items.reduce((acc, item) => { + return acc + item.quantity; + }, 0); const addToOrder = async () => { if (isLoading.value) return; isLoading.value = true; @@ -44,11 +38,18 @@ const addToOrder = async () => { orderFk: Number(route.params.id), }); - const { totalQuantity, totalSum } = calculateTotals(items); + const { data: orderTotal } = await axios.get( + `Orders/${Number(route.params.id)}/getTotal` + ); - state.set('orderTotal', total.value + totalSum); + state.set('orderTotal', orderTotal); + const rows = orderData.value.rows.push(...items) || []; + state.set('orderData', { + ...orderData.value, + rows, + }); notify(t('globals.dataSaved'), 'positive'); - emit('added', -totalQuantity); + emit('added', -totalQuantity(items)); isLoading.value = false; }; const canAddToOrder = () => {