1
1
import { createFileRoute , useNavigate } from "@tanstack/react-router" ;
2
- import { components } from "../../../../api.gen" ;
3
2
import { $api } from "../../../../stores/api" ;
4
3
import { useQueryClient , useSuspenseQuery } from "@tanstack/react-query" ;
5
- import { Button , TextInput , useForm } from "@quassel/ui" ;
4
+ import { Button , DateInput , Divider , Group , Stack , Table , Textarea , TextInput , useForm } from "@quassel/ui" ;
6
5
import { useEffect } from "react" ;
6
+ import { QuestionnaireEntries } from "../../../../components/questionnaire/QuestionnaireEntries" ;
7
+ import { format , i18n } from "../../../../stores/i18n" ;
8
+ import { useStore } from "@nanostores/react" ;
7
9
8
- type FormValues = components [ "schemas" ] [ "QuestionnaireMutationDto" ] ;
10
+ type FormValues = {
11
+ startedAt : Date ;
12
+ endedAt : Date ;
13
+ title : string ;
14
+ remark ?: string ;
15
+ } ;
16
+
17
+ const messages = i18n ( "questionnairesEdit" , {
18
+ labelRemarks : "Remarks" ,
19
+ labelTitle : "Title" ,
20
+ labelEndedAt : "End date" ,
21
+ labelStartedAt : "Start date" ,
22
+ labelParticipant : "Participant" ,
23
+ labelStudy : "Study" ,
24
+ } ) ;
9
25
10
26
function AdministrationQuestionnairesEdit ( ) {
11
27
const p = Route . useParams ( ) ;
28
+ const n = useNavigate ( ) ;
12
29
const q = useQueryClient ( ) ;
30
+
31
+ const t = useStore ( messages ) ;
32
+ const { time } = useStore ( format ) ;
33
+
13
34
const { data, isSuccess } = useSuspenseQuery (
14
35
$api . queryOptions ( "get" , "/questionnaires/{id}" , {
15
36
params : { path : { id : p . id } } ,
16
37
} )
17
38
) ;
18
- const n = useNavigate ( ) ;
39
+
19
40
const editQuestionnaireMutation = $api . useMutation ( "patch" , "/questionnaires/{id}" , {
20
41
onSuccess : ( ) => {
21
42
q . invalidateQueries (
@@ -26,27 +47,55 @@ function AdministrationQuestionnairesEdit() {
26
47
n ( { to : "/administration/questionnaires" } ) ;
27
48
} ,
28
49
} ) ;
50
+
29
51
const f = useForm < FormValues > ( ) ;
30
- const handleSubmit = ( values : FormValues ) => {
52
+ const handleSubmit = ( { startedAt , endedAt , ... rest } : FormValues ) => {
31
53
editQuestionnaireMutation . mutate ( {
32
- body : { ...values } ,
54
+ body : { ...rest , startedAt : startedAt . toISOString ( ) , endedAt : endedAt . toISOString ( ) } ,
33
55
params : { path : { id : p . id } } ,
34
56
} ) ;
35
57
} ;
36
58
37
59
useEffect ( ( ) => {
38
- f . setValues ( { title : data . title } ) ;
39
- f . resetDirty ( ) ;
60
+ const { title, remark , startedAt , endedAt } = data ;
61
+ f . initialize ( { title , remark , startedAt : new Date ( startedAt ) , endedAt : new Date ( endedAt ) } ) ;
40
62
} , [ isSuccess , data ] ) ;
41
63
42
64
return (
43
- < form autoComplete = "off" onSubmit = { f . onSubmit ( handleSubmit ) } >
44
- < TextInput label = "Name" type = "name" { ...f . getInputProps ( "title" ) } />
65
+ < Stack gap = "xl" >
66
+ < Table >
67
+ < Table . Tbody >
68
+ < Table . Tr >
69
+ < Table . Th > { t . labelParticipant } </ Table . Th >
70
+ < Table . Td > { data . participant . id } </ Table . Td >
71
+ < Table . Td > { data . participant . birthday && time ( new Date ( data . participant . birthday ) ) } </ Table . Td >
72
+ </ Table . Tr >
73
+ < Table . Tr >
74
+ < Table . Th > { t . labelStudy } </ Table . Th >
75
+ < Table . Td > { data ?. study . id } </ Table . Td >
76
+ < Table . Td > { data ?. study . title } </ Table . Td >
77
+ </ Table . Tr >
78
+ </ Table . Tbody >
79
+ </ Table >
80
+
81
+ < form autoComplete = "off" onSubmit = { f . onSubmit ( handleSubmit ) } >
82
+ < Stack >
83
+ < TextInput { ...f . getInputProps ( "title" ) } label = { t . labelTitle } />
84
+ < Group >
85
+ < DateInput { ...f . getInputProps ( "startedAt" ) } label = { t . labelStartedAt } flex = { 1 } />
86
+ < DateInput { ...f . getInputProps ( "endedAt" ) } label = { t . labelEndedAt } flex = { 1 } />
87
+ </ Group >
88
+
89
+ < Textarea { ...f . getInputProps ( "remark" ) } label = { t . labelRemarks } rows = { 8 } />
45
90
46
- < Button type = "submit" fullWidth mt = "xl" loading = { editQuestionnaireMutation . isPending } >
47
- Change
48
- </ Button >
49
- </ form >
91
+ < Button type = "submit" loading = { editQuestionnaireMutation . isPending } >
92
+ Change
93
+ </ Button >
94
+ </ Stack >
95
+ </ form >
96
+ < Divider />
97
+ < QuestionnaireEntries questionnaire = { data } />
98
+ </ Stack >
50
99
) ;
51
100
}
52
101
0 commit comments