From bf5da9ecb096fef7c9d9f1f0f8fcceec3a881aaf Mon Sep 17 00:00:00 2001 From: Lixian Zhou Date: Thu, 12 Mar 2026 16:57:01 -0400 Subject: [PATCH] fix --- .../components/customers/UpdateCustomer.js | 65 ++++++++++++++----- 1 file changed, 47 insertions(+), 18 deletions(-) diff --git a/client/src/components/customers/UpdateCustomer.js b/client/src/components/customers/UpdateCustomer.js index 84c12c8..ad53472 100644 --- a/client/src/components/customers/UpdateCustomer.js +++ b/client/src/components/customers/UpdateCustomer.js @@ -101,6 +101,8 @@ const UpdateCustomer = () => { const [dischargeConfirmReasonOther, setDischargeConfirmReasonOther] = useState(''); const [isDischarging, setIsDischarging] = useState(false); const [isReactivating, setIsReactivating] = useState(false); + const [canDischargeCustomer, setCanDischargeCustomer] = useState(AuthService.canDischargeCustomer()); + const [canReactivateCustomer, setCanReactivateCustomer] = useState(AuthService.canReactivateCustomer()); // Care & Services const [dietaryRestrictions, setDietaryRestrictions] = useState([]); @@ -200,22 +202,47 @@ const UpdateCustomer = () => { }, [activeTab]); useEffect(() => { - if (!AuthService.canEditAnyCustomerTab()) { - window.alert('You haven\'t login yet OR this user does not have access to this page. Please change an admin account to login.') - AuthService.logout(); - navigate(`/login`); - } - if (!currentCustomer) { - CustomerService.getCustomer(urlParams.id).then((data) => { - setCurrentCustomer(data.data); - }) - } - ResourceService.getAll('pharmacy').then(data => { - setPharmacies(data.data); - }); - ResourceService.getAll('provider').then(data => { - setProviders(data.data); - }); + let isMounted = true; + const initPage = async () => { + try { + // Re-check permissions whenever Update Customer loads. + await AuthService.refreshCurrentUserPermissions(); + } catch (error) { + console.log(error); + } finally { + if (!isMounted) return; + setCanDischargeCustomer(AuthService.canDischargeCustomer()); + setCanReactivateCustomer(AuthService.canReactivateCustomer()); + } + + if (!AuthService.canEditAnyCustomerTab()) { + window.alert('You haven\'t login yet OR this user does not have access to this page. Please change an admin account to login.') + AuthService.logout(); + navigate(`/login`); + return; + } + if (!currentCustomer) { + CustomerService.getCustomer(urlParams.id).then((data) => { + if (isMounted) { + setCurrentCustomer(data.data); + } + }) + } + ResourceService.getAll('pharmacy').then(data => { + if (isMounted) { + setPharmacies(data.data); + } + }); + ResourceService.getAll('provider').then(data => { + if (isMounted) { + setProviders(data.data); + } + }); + }; + initPage(); + return () => { + isMounted = false; + }; }, []); const formatDateForBackend = (date) => { @@ -251,6 +278,7 @@ const UpdateCustomer = () => { }; const handleDischarge = async () => { + if (!canDischargeCustomer) return; if (!dischargeConfirmDate || !dischargeConfirmReason) { alert('Please fill in Discharge Date and Discharge Reason.'); return; @@ -285,6 +313,7 @@ const UpdateCustomer = () => { }; const handleReactivate = async () => { + if (!canReactivateCustomer) return; if (!currentCustomer?.id) return; setIsReactivating(true); @@ -1320,7 +1349,7 @@ const UpdateCustomer = () => { )} )} - {!isCustomerActive() && AuthService.canReactivateCustomer() && ( + {!isCustomerActive() && canReactivateCustomer && (