Skip to content

Commit 1a5cd13

Browse files
committed
fix: added delete resident logic back to residentProfile
1 parent d3da2ea commit 1a5cd13

1 file changed

Lines changed: 70 additions & 3 deletions

File tree

frontend/src/Pages/ResidentProfile.tsx

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import useSWR from 'swr';
1+
import useSWR, { mutate } from 'swr';
22
import {
33
OpenContentResponse,
44
ResidentEngagementProfile,
55
ServerResponseOne,
6+
ToastState,
7+
User,
8+
UserRole,
69
ValidResident
710
} from '@/common';
811
import EngagementRateGraph from '@/Components/EngagementRateGraph';
@@ -13,11 +16,13 @@ import { useNavigate, useParams } from 'react-router-dom';
1316
import OpenContentCardRow from '@/Components/cards/OpenContentCard';
1417
import {
1518
closeModal,
19+
CRUDActions,
1620
showModal,
21+
TargetItem,
1722
TextModalType,
1823
TextOnlyModal
1924
} from '@/Components/modals';
20-
import { useRef, useState } from 'react';
25+
import { useEffect, useRef, useState } from 'react';
2126
import { useCheckResponse } from '@/Hooks/useCheckResponse';
2227
import { VerifyResidentModal } from '@/Components/modals/VerifyResidentModal';
2328
import API from '@/api/api';
@@ -26,6 +31,7 @@ import TransferSummaryPanel from '@/Components/TransferSummaryPanel';
2631
import calculateEngagementMetrics from '@/Components/helperFunctions/calculateEngagementMetrics';
2732
import ResidentPrograms from '@/Components/ResidentPrograms';
2833
import ActivityHistoryCard from '@/Components/ActivityHistoryCard';
34+
import { useToast } from '@/Context/ToastCtx';
2935

3036
function UserProfileInfoRow({
3137
column,
@@ -102,6 +108,46 @@ const ResidentProfile = () => {
102108
const handleShowLibraryClick = (id: number) => {
103109
navigate(`/viewer/libraries/${id}`);
104110
};
111+
// start delete logic
112+
const [targetUser, setTargetUser] = useState<TargetItem<User> | null>(null);
113+
const deleteUserModal = useRef<HTMLDialogElement>(null);
114+
const { toaster } = useToast();
115+
116+
const checkResponseForDelete = useCheckResponse({
117+
mutate: mutate,
118+
refModal: deleteUserModal
119+
});
120+
121+
useEffect(() => {
122+
const ref =
123+
targetUser?.action === CRUDActions.Delete ? deleteUserModal : null;
124+
if (ref) {
125+
showModal(ref);
126+
}
127+
}, [targetUser]);
128+
129+
const deleteUser = async () => {
130+
const user = targetUser?.target;
131+
if (user?.role === UserRole.SystemAdmin) {
132+
toaster(
133+
'This is the primary administrator and cannot be deleted',
134+
ToastState.error
135+
);
136+
return;
137+
}
138+
139+
const response = await API.delete('users/' + user?.id);
140+
141+
checkResponseForDelete(
142+
response.success,
143+
'Failed to delete user',
144+
'User deleted successfully'
145+
);
146+
if (response.success) {
147+
navigate('/residents');
148+
}
149+
closeModal(deleteUserModal);
150+
};
105151

106152
//start transfer logic
107153
const verifyResidentModal = useRef<HTMLDialogElement>(null);
@@ -135,6 +181,7 @@ const ResidentProfile = () => {
135181
}
136182
}
137183
}
184+
138185
//end transfer logic
139186
return (
140187
<div className="overflow-x-hidden px-5 pb-4">
@@ -188,7 +235,17 @@ const ResidentProfile = () => {
188235
}
189236
/>
190237
<div className="flex flex-row gap-2 mt-4 justify-center">
191-
<button className="button-grey bg-grey-1 hover:bg-grey-2 text-red-4">
238+
<button
239+
className="button-grey bg-grey-1 hover:bg-grey-2 text-red-4"
240+
onClick={(e) => {
241+
e.stopPropagation();
242+
setTargetUser({
243+
action: CRUDActions.Delete,
244+
target: data.data.user
245+
});
246+
showModal(deleteUserModal);
247+
}}
248+
>
192249
Delete Resident
193250
</button>
194251
{user && canSwitchFacility(user) && (
@@ -363,6 +420,16 @@ const ResidentProfile = () => {
363420
setResident(null);
364421
}}
365422
/>
423+
<TextOnlyModal
424+
ref={deleteUserModal}
425+
type={TextModalType.Delete}
426+
title={'Delete Resident'}
427+
text={
428+
'Are you sure you would like to delete this resident? This action cannot be undone.'
429+
}
430+
onSubmit={() => void deleteUser()}
431+
onClose={() => void closeModal(deleteUserModal)}
432+
/>
366433
</div>
367434
);
368435
};

0 commit comments

Comments
 (0)