11import { useEffect , useState } from 'react'
22import { useTasksTranslation } from '@/i18n/useTasksTranslation'
3- import type { CreatePatientInput , TaskType , UpdatePatientInput } from '@/api/gql/generated'
4- import { Sex , useCreatePatientMutation , useUpdatePatientMutation } from '@/api/gql/generated'
5- import { Input , LoadingButton , Select , SelectOption , Tab , TabView } from '@helpwave/hightide'
3+ import type { CreatePatientInput , UpdatePatientInput } from '@/api/gql/generated'
4+ import { Sex , useCreatePatientMutation , useUpdatePatientMutation , useGetPatientQuery } from '@/api/gql/generated'
5+ import {
6+ Input ,
7+ LoadingButton ,
8+ Select ,
9+ SelectOption ,
10+ Tab ,
11+ TabView ,
12+ LoadingContainer ,
13+ FormElementWrapper
14+ } from '@helpwave/hightide'
615import { useTasksContext } from '@/hooks/useTasksContext'
7- import { PopupDatePicker } from '@/components/ui/PopupDatePicker '
16+ import { DateInput } from '@/components/ui/DateInput '
817import { CheckCircle2 , Circle , Clock } from 'lucide-react'
918import { PropertyList } from '@/components/PropertyList'
1019
1120interface PatientDetailViewProps {
1221 patientId ?: string ,
13- initialData ?: Partial < CreatePatientInput > & {
14- tasks ?: TaskType [ ] ,
15- name ?: string ,
16- } ,
1722 onClose : ( ) => void ,
1823 onSuccess : ( ) => void ,
1924}
@@ -39,37 +44,38 @@ const FormField = ({ label, children }: { label: string, children: React.ReactNo
3944 </ div >
4045)
4146
42- export const PatientDetailView = ( { patientId, initialData , onClose, onSuccess } : PatientDetailViewProps ) => {
47+ export const PatientDetailView = ( { patientId, onClose, onSuccess } : PatientDetailViewProps ) => {
4348 const translation = useTasksTranslation ( )
4449 const { selectedLocationId } = useTasksContext ( )
4550 const isEditMode = ! ! patientId
4651
47- const [ fullName , setFullName ] = useState ( initialData ?. name || '' )
52+ const { data : patientData , isLoading : isLoadingPatient } = useGetPatientQuery (
53+ { id : patientId ! } ,
54+ { enabled : isEditMode }
55+ )
4856
57+ const [ fullName , setFullName ] = useState ( '' )
4958 const [ formData , setFormData ] = useState < CreatePatientInput > ( {
5059 firstname : '' ,
5160 lastname : '' ,
5261 sex : Sex . Female ,
5362 assignedLocationId : selectedLocationId ?? '' ,
54- ...initialData ,
55- birthdate : initialData ?. birthdate ? toISODate ( initialData . birthdate ) : getDefaultBirthdate ( )
63+ birthdate : getDefaultBirthdate ( )
5664 } )
5765
5866 useEffect ( ( ) => {
59- if ( initialData ) {
60- setFormData ( prev => ( {
61- ...prev ,
62- ...initialData ,
63- birthdate : initialData . birthdate ? toISODate ( initialData . birthdate ) : prev . birthdate
64- } ) )
65-
66- if ( initialData . firstname && initialData . lastname ) {
67- setFullName ( `${ initialData . firstname } ${ initialData . lastname } ` )
68- } else if ( initialData . name ) {
69- setFullName ( initialData . name )
70- }
67+ if ( patientData ?. patient ) {
68+ const { firstname, lastname, sex, birthdate, assignedLocation } = patientData . patient
69+ setFormData ( {
70+ firstname,
71+ lastname,
72+ sex,
73+ birthdate : toISODate ( birthdate ) ,
74+ assignedLocationId : assignedLocation ?. id ?? selectedLocationId ?? ''
75+ } )
76+ setFullName ( `${ firstname } ${ lastname } ` )
7177 }
72- } , [ initialData ] )
78+ } , [ patientData , selectedLocationId ] )
7379
7480 const { mutate : createPatient , isLoading : isCreating } = useCreatePatientMutation ( {
7581 onSuccess : ( ) => {
@@ -84,7 +90,6 @@ export const PatientDetailView = ({ patientId, initialData, onClose, onSuccess }
8490 }
8591 } )
8692
87-
8893 const persistChanges = ( updates : Partial < UpdatePatientInput > ) => {
8994 if ( updates . firstname !== undefined && ! updates . firstname ?. trim ( ) ) return
9095 if ( updates . lastname !== undefined && ! updates . lastname ?. trim ( ) ) return
@@ -117,22 +122,26 @@ export const PatientDetailView = ({ patientId, initialData, onClose, onSuccess }
117122 { label : translation ( 'diverse' ) , value : Sex . Unknown }
118123 ]
119124
120- const tasks = initialData ?. tasks || [ ]
125+ const tasks = patientData ?. patient ?. tasks || [ ]
121126 const openTasks = tasks . filter ( t => ! t . done )
122127 const closedTasks = tasks . filter ( t => t . done )
123128
129+ if ( isEditMode && isLoadingPatient ) {
130+ return < LoadingContainer />
131+ }
132+
124133 return (
125- < div className = "flex flex -col h-full bg-surface" >
134+ < div className = "flex-col-0 h-full bg-surface" >
126135 < div className = "flex-none mb-6" >
127136 < div className = "typography-title-lg text-primary" >
128- { fullName }
137+ { fullName || translation ( 'newPatient' ) }
129138 </ div >
130139 </ div >
131140
132- < div className = "flex-grow overflow-hidden flex flex-col " >
133- < TabView className = "h-full flex flex -col" >
141+ < div className = "flex-col-0 flex-grow overflow-hidden " >
142+ < TabView className = "h-full flex-col-0 " >
134143 < Tab label = { translation ( 'overview' ) } className = "h-full overflow-x-visible pr-2" >
135- < div className = "flex flex -col gap -6 pt-4" >
144+ < div className = "flex-col-6 pt-4" >
136145 < div className = "grid grid-cols-2 gap-4" >
137146 < FormField label = { translation ( 'firstName' ) } >
138147 < Input
@@ -160,15 +169,20 @@ export const PatientDetailView = ({ patientId, initialData, onClose, onSuccess }
160169 </ FormField >
161170 </ div >
162171
163- < PopupDatePicker
164- label = { translation ( 'birthdate' ) }
165- date = { new Date ( formData . birthdate as string ) }
166- onDateChange = { date => {
167- const dateStr = toISODate ( date )
168- updateLocalState ( { birthdate : dateStr } )
169- persistChanges ( { birthdate : dateStr } )
170- } }
171- />
172+ < FormElementWrapper label = { translation ( 'birthdate' ) } >
173+ { ( bag ) => (
174+ < DateInput
175+ { ...bag }
176+ date = { new Date ( formData . birthdate as string ) }
177+ onValueChange = { date => {
178+ const dateStr = toISODate ( date )
179+ updateLocalState ( { birthdate : dateStr } )
180+ persistChanges ( { birthdate : dateStr } )
181+ } }
182+ />
183+ ) }
184+ </ FormElementWrapper >
185+
172186
173187 < FormField label = { translation ( 'sex' ) } >
174188 < Select
@@ -253,4 +267,4 @@ export const PatientDetailView = ({ patientId, initialData, onClose, onSuccess }
253267 ) }
254268 </ div >
255269 )
256- }
270+ }
0 commit comments