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 , Textarea , TextInput , useForm } from "@quassel/ui" ;
6
5
import { useEffect } from "react" ;
6
+ import { QuestionnaireEntries } from "../../../../components/questionnaire/QuestionnaireEntries" ;
7
+ import { 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
+ } ) ;
9
23
10
24
function AdministrationQuestionnairesEdit ( ) {
11
25
const p = Route . useParams ( ) ;
26
+ const n = useNavigate ( ) ;
12
27
const q = useQueryClient ( ) ;
28
+
29
+ const t = useStore ( messages ) ;
30
+
13
31
const { data, isSuccess } = useSuspenseQuery (
14
32
$api . queryOptions ( "get" , "/questionnaires/{id}" , {
15
33
params : { path : { id : p . id } } ,
16
34
} )
17
35
) ;
18
- const n = useNavigate ( ) ;
36
+
19
37
const editQuestionnaireMutation = $api . useMutation ( "patch" , "/questionnaires/{id}" , {
20
38
onSuccess : ( ) => {
21
39
q . invalidateQueries (
@@ -26,27 +44,40 @@ function AdministrationQuestionnairesEdit() {
26
44
n ( { to : "/administration/questionnaires" } ) ;
27
45
} ,
28
46
} ) ;
47
+
29
48
const f = useForm < FormValues > ( ) ;
30
- const handleSubmit = ( values : FormValues ) => {
49
+ const handleSubmit = ( { startedAt , endedAt , ... rest } : FormValues ) => {
31
50
editQuestionnaireMutation . mutate ( {
32
- body : { ...values } ,
51
+ body : { ...rest , startedAt : startedAt . toISOString ( ) , endedAt : endedAt . toISOString ( ) } ,
33
52
params : { path : { id : p . id } } ,
34
53
} ) ;
35
54
} ;
36
55
37
56
useEffect ( ( ) => {
38
- f . setValues ( { title : data . title } ) ;
39
- f . resetDirty ( ) ;
57
+ const { title, remark , startedAt , endedAt } = data ;
58
+ f . initialize ( { title , remark , startedAt : new Date ( startedAt ) , endedAt : new Date ( endedAt ) } ) ;
40
59
} , [ isSuccess , data ] ) ;
41
60
42
61
return (
43
- < form autoComplete = "off" onSubmit = { f . onSubmit ( handleSubmit ) } >
44
- < TextInput label = "Name" type = "name" { ...f . getInputProps ( "title" ) } />
62
+ < Stack gap = "xl" >
63
+ < form autoComplete = "off" onSubmit = { f . onSubmit ( handleSubmit ) } >
64
+ < Stack >
65
+ < TextInput { ...f . getInputProps ( "title" ) } label = { t . labelTitle } />
66
+ < Group >
67
+ < DateInput { ...f . getInputProps ( "startedAt" ) } label = { t . labelStartedAt } flex = { 1 } />
68
+ < DateInput { ...f . getInputProps ( "endedAt" ) } label = { t . labelEndedAt } flex = { 1 } />
69
+ </ Group >
70
+
71
+ < Textarea { ...f . getInputProps ( "remark" ) } label = { t . labelRemarks } rows = { 8 } />
45
72
46
- < Button type = "submit" fullWidth mt = "xl" loading = { editQuestionnaireMutation . isPending } >
47
- Change
48
- </ Button >
49
- </ form >
73
+ < Button type = "submit" loading = { editQuestionnaireMutation . isPending } >
74
+ Change
75
+ </ Button >
76
+ </ Stack >
77
+ </ form >
78
+ < Divider />
79
+ < QuestionnaireEntries questionnaire = { data } />
80
+ </ Stack >
50
81
) ;
51
82
}
52
83
0 commit comments