From 95d531c8c356cc8e8011b7995b6ef4419c9dc383 Mon Sep 17 00:00:00 2001
From: Vidhu Kant Sharma <vidhukant@vidhukant.com>
Date: Mon, 4 Dec 2023 15:48:52 +0530
Subject: refreshing items when new item is added to invoice

---
 src/views/EditInvoice.vue | 43 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 34 insertions(+), 9 deletions(-)

(limited to 'src/views/EditInvoice.vue')

diff --git a/src/views/EditInvoice.vue b/src/views/EditInvoice.vue
index 7a508f8..958846e 100644
--- a/src/views/EditInvoice.vue
+++ b/src/views/EditInvoice.vue
@@ -1,5 +1,5 @@
 <script setup lang="ts">
-import { ref, toRaw, onMounted } from 'vue'
+import { ref, onMounted, toRaw } from 'vue'
 import { useRoute } from "vue-router"
 import { useToast } from 'vue-toast-notification'
 import axios from 'axios'
@@ -18,24 +18,43 @@ const route = useRoute()
 
 const invoiceId = route.params.id
 const invoice = ref(new Invoice())
-const isLoading = ref(true)
+const items = ref([])
+
+const invoiceIsLoading = ref(true)
+const itemsTableIsLoading = ref(true)
 
 const getInvoice = async () => {
-  isLoading.value = true
+  invoiceIsLoading.value = true
+  itemsTableIsLoading.value = true
 
   try {
     const r = await axios.get(`/invoice/${invoiceId}`)
     invoice.value = r.data.data
+    items.value = r.data.data.Items
   } catch (err) {
     toast.error('An unhandled exception occoured. Please check logs')
     console.error(err)
   }
 
-  isLoading.value = false
+  invoiceIsLoading.value = false
+  itemsTableIsLoading.value = false
 }
 
-const refreshItems = () => {
-   toast.success("Item was added but what happens after that hasn't been implemented yet!")
+const refreshItems = async () => {
+  items.value = []
+  itemsTableIsLoading.value = true
+
+  try {
+    const res = await axios.get(`/invoice/${invoiceId}/item`)
+    if (res.status === 200) {
+      items.value = res.data.data
+    }
+  } catch (err) {
+    toast.error('An unhandled exception occoured. Please check logs')
+    console.error(err)
+  }
+
+  itemsTableIsLoading.value = false
 }
 
 onMounted(() => {
@@ -44,7 +63,13 @@ onMounted(() => {
 </script>
 
 <template>
-  <invoiceHeader :invoice="invoice" />
-  <itemSelector :invoiceId="invoiceId" @added="refreshItems()"/>
-  <invoiceItemsTable :invoiceId="invoiceId" />
+  <invoiceHeader
+    :invoice="invoice" />
+  <itemSelector
+    :invoiceId="invoiceId"
+    @added="refreshItems()"/>
+  <invoiceItemsTable
+    :items="items"
+    :isLoading="itemsTableIsLoading"
+    @refresh="refreshItems()" />
 </template>
-- 
cgit v1.2.3

