1- import useSWR from 'swr' ;
1+ import useSWR , { mutate } from 'swr' ;
22import {
33 OpenContentResponse ,
44 ResidentEngagementProfile ,
55 ServerResponseOne ,
6+ ToastState ,
7+ User ,
8+ UserRole ,
69 ValidResident
710} from '@/common' ;
811import EngagementRateGraph from '@/Components/EngagementRateGraph' ;
@@ -13,11 +16,13 @@ import { useNavigate, useParams } from 'react-router-dom';
1316import OpenContentCardRow from '@/Components/cards/OpenContentCard' ;
1417import {
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' ;
2126import { useCheckResponse } from '@/Hooks/useCheckResponse' ;
2227import { VerifyResidentModal } from '@/Components/modals/VerifyResidentModal' ;
2328import API from '@/api/api' ;
@@ -26,6 +31,7 @@ import TransferSummaryPanel from '@/Components/TransferSummaryPanel';
2631import calculateEngagementMetrics from '@/Components/helperFunctions/calculateEngagementMetrics' ;
2732import ResidentPrograms from '@/Components/ResidentPrograms' ;
2833import ActivityHistoryCard from '@/Components/ActivityHistoryCard' ;
34+ import { useToast } from '@/Context/ToastCtx' ;
2935
3036function 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