1
- import { Button } from "@quassel/ui" ;
2
1
import { createFileRoute , useNavigate } from "@tanstack/react-router" ;
3
2
import { i18n } from "../../../../../stores/i18n" ;
4
3
import { useStore } from "@nanostores/react" ;
4
+ import { PeriodForm , PeriodFormValues } from "../../../../../components/questionnaire/PeriodForm" ;
5
+ import { $api } from "../../../../../stores/api" ;
6
+ import { useSuspenseQuery } from "@tanstack/react-query" ;
5
7
6
8
export const messages = i18n ( "questionnairePeriod" , {
7
9
title : "Period" ,
@@ -10,24 +12,54 @@ export const messages = i18n("questionnairePeriod", {
10
12
11
13
function QuestionnairePeriod ( ) {
12
14
const n = useNavigate ( ) ;
13
- const p = Route . useParams ( ) ;
15
+ const { id } = Route . useParams ( ) ;
14
16
15
17
const t = useStore ( messages ) ;
16
18
17
- const handleSubmit = ( ) => {
18
- n ( { to : "/questionnaire/$id/entries" , params : p } ) ;
19
+ const { data : questionnaire } = useSuspenseQuery (
20
+ $api . queryOptions ( "get" , "/questionnaires/{id}" , {
21
+ params : { path : { id } } ,
22
+ } )
23
+ ) ;
24
+
25
+ const period : PeriodFormValues = {
26
+ title : questionnaire . title ! ,
27
+ range : [ new Date ( Date . parse ( questionnaire . startedAt ! ) ) , new Date ( Date . parse ( questionnaire . endedAt ! ) ) ] ,
28
+ } ;
29
+
30
+ const updateQuestionnaireMutation = $api . useMutation ( "patch" , "/questionnaires/{id}" ) ;
31
+
32
+ const onSave = async ( form : PeriodFormValues ) => {
33
+ const {
34
+ title,
35
+ range : [ localStartedAt , localEndedAt ] ,
36
+ } = form ;
37
+
38
+ const startedAt = localStartedAt . toISOString ( ) ;
39
+ const endedAt = localEndedAt . toISOString ( ) ;
40
+
41
+ await updateQuestionnaireMutation . mutateAsync ( {
42
+ params : { path : { id } } ,
43
+ body : { title, startedAt, endedAt } ,
44
+ } ) ;
45
+
46
+ n ( { to : "/questionnaire/$id/entries" , params : { id } } ) ;
19
47
} ;
20
48
21
49
return (
22
50
< >
23
51
< h3 > { t . title } </ h3 >
24
- < form onSubmit = { handleSubmit } >
25
- < Button type = "submit" > { t . formAction } </ Button >
26
- </ form >
52
+ < PeriodForm onSave = { onSave } period = { period } actionLabel = { t . formAction } />
27
53
</ >
28
54
) ;
29
55
}
30
56
31
57
export const Route = createFileRoute ( "/_auth/questionnaire/_questionnaire/$id/period" ) ( {
58
+ loader : ( { params, context : { queryClient } } ) =>
59
+ queryClient . ensureQueryData (
60
+ $api . queryOptions ( "get" , "/questionnaires/{id}" , {
61
+ params : { path : { id : params . id } } ,
62
+ } )
63
+ ) ,
32
64
component : QuestionnairePeriod ,
33
65
} ) ;
0 commit comments