From 8e874b0690bbffec5f8cfcf2372e68332f8e8600 Mon Sep 17 00:00:00 2001
From: Marte Fossum <marte.fossum@usit.uio.no>
Date: Tue, 24 Jan 2023 13:56:40 +0100
Subject: [PATCH] Reload guests when period changed

---
 .../src/routes/sponsor/guest/guestRoleInfo/index.tsx | 12 ++++++++++--
 frontend/src/routes/sponsor/guest/index.tsx          |  6 +++++-
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/frontend/src/routes/sponsor/guest/guestRoleInfo/index.tsx b/frontend/src/routes/sponsor/guest/guestRoleInfo/index.tsx
index 8d5fe0bc..f8b43d3c 100644
--- a/frontend/src/routes/sponsor/guest/guestRoleInfo/index.tsx
+++ b/frontend/src/routes/sponsor/guest/guestRoleInfo/index.tsx
@@ -30,9 +30,14 @@ import useOus from '../../../../hooks/useOus'
 interface GuestRoleInfoProps {
   guest: Guest
   reloadGuest: () => void
+  reloadGuests: () => void
 }
 
-const endPeriodPost = (id: string, data: { end_date: Date }) => {
+const endPeriodPost = (
+  id: string,
+  data: { end_date: Date },
+  reloadGuests: () => void
+) => {
   const payload = {
     end_date: format(data.end_date as Date, 'yyyy-MM-dd'),
   }
@@ -48,6 +53,7 @@ const endPeriodPost = (id: string, data: { end_date: Date }) => {
     .then((result) => {
       if (result !== null) {
         console.log('result', result)
+        reloadGuests()
       }
     })
     .catch((error) => {
@@ -107,6 +113,7 @@ const TableContainer = styled(TableContainerMui)({
 export default function GuestRoleInfo({
   guest,
   reloadGuest,
+  reloadGuests,
 }: GuestRoleInfoProps) {
   const { pid, id } = useParams<GuestRoleInfoParams>()
   const { ous } = useOus()
@@ -145,7 +152,7 @@ export default function GuestRoleInfo({
       setBadEndDate(true)
     } else {
       role.end_date = newEndDate
-      endPeriodPost(role_id, { end_date: newEndDate })
+      endPeriodPost(role_id, { end_date: newEndDate }, reloadGuests)
       // Go back to guest overview page
       navigate(`/sponsor/guest/${guest.pid}`, {
         state: {
@@ -192,6 +199,7 @@ export default function GuestRoleInfo({
           console.log('result', result)
         }
         // Reload the guest so that the information on the overview page is updated
+        reloadGuests()
         reloadGuest()
         // A submit does not clear the dirty state, so resetting values in the form
         // to the values just submitted to clear the dirty-flag
diff --git a/frontend/src/routes/sponsor/guest/index.tsx b/frontend/src/routes/sponsor/guest/index.tsx
index 3ecccbaa..41ecd2ff 100644
--- a/frontend/src/routes/sponsor/guest/index.tsx
+++ b/frontend/src/routes/sponsor/guest/index.tsx
@@ -66,7 +66,11 @@ function GuestRoutes({ reloadGuests }: GuestRoutesProps) {
       <Route
         path="/roles/:id"
         element={
-          <GuestRoleInfo guest={guestInfo} reloadGuest={reloadGuestInfo} />
+          <GuestRoleInfo
+            guest={guestInfo}
+            reloadGuest={reloadGuestInfo}
+            reloadGuests={reloadGuests}
+          />
         }
       />
       <Route
-- 
GitLab