1
1
import { Button , Group , isSameOrAfter , Stack , Textarea , useForm } from "@quassel/ui" ;
2
- import { createFileRoute , Link , useNavigate } from "@tanstack/react-router" ;
2
+ import { createFileRoute , useNavigate } from "@tanstack/react-router" ;
3
3
import { i18n } from "../../../../../stores/i18n" ;
4
4
import { useStore } from "@nanostores/react" ;
5
5
import { $api } from "../../../../../stores/api" ;
@@ -8,11 +8,11 @@ import { components } from "../../../../../api.gen";
8
8
import { useQueryClient } from "@tanstack/react-query" ;
9
9
10
10
export const messages = i18n ( "questionnaireRemarks" , {
11
+ actionBack : "Back (Save)" ,
12
+ actionContinueNextPeriod : "Continue with next period" ,
13
+ actionCompleteQuestionnaire : "Complete questionnaire" ,
11
14
title : "Add remarks" ,
12
15
remarkDescription : "Use this field to point out any exceptions to the language exposure like vacations etc..." ,
13
- backAction : "Back" ,
14
- saveAction : "Save" ,
15
- formAction : "Save and complete" ,
16
16
} ) ;
17
17
18
18
type FormValues = {
@@ -36,6 +36,8 @@ function QuestionnaireRemarks() {
36
36
const { data : questionnaire } = $api . useSuspenseQuery ( "get" , "/questionnaires/{id}" , { params : { path : { id : p . id } } } ) ;
37
37
const updateMutation = $api . useMutation ( "patch" , "/questionnaires/{id}" ) ;
38
38
39
+ const isLastQuestionnaire = isSameOrAfter ( new Date ( Date . parse ( questionnaire . endedAt ) ) , new Date ( ) , "month" ) ;
40
+
39
41
useEffect ( ( ) => {
40
42
if ( questionnaire ) {
41
43
f . setValues ( questionnaire ) ;
@@ -46,10 +48,15 @@ function QuestionnaireRemarks() {
46
48
const onSave = ( values : components [ "schemas" ] [ "QuestionnaireMutationDto" ] ) =>
47
49
updateMutation . mutateAsync ( { params : { path : p } , body : values } ) ;
48
50
51
+ const handleBackAndSave = async ( ) => {
52
+ await onSave ( f . getValues ( ) ) ;
53
+ n ( { to : "/questionnaire/$id/entries" , params : p } ) ;
54
+ } ;
55
+
49
56
const handleSubmit = async ( values : FormValues ) => {
50
57
await onSave ( { ...values , completedAt : new Date ( ) . toISOString ( ) } ) ;
51
58
52
- if ( isSameOrAfter ( new Date ( Date . parse ( questionnaire . endedAt ) ) , new Date ( ) , "month" ) ) {
59
+ if ( isLastQuestionnaire ) {
53
60
n ( { to : "/questionnaire/completed" } ) ;
54
61
} else {
55
62
await c . invalidateQueries (
@@ -67,14 +74,11 @@ function QuestionnaireRemarks() {
67
74
< Textarea { ...f . getInputProps ( "remark" ) } description = { t . remarkDescription } rows = { 8 } />
68
75
69
76
< Group >
70
- < Link to = "/questionnaire/$id/entries" params = { p } >
71
- < Button variant = "light" > { t . backAction } </ Button >
72
- </ Link >
73
- < Button variant = "outline" onClick = { ( ) => onSave ( f . getValues ( ) ) } loading = { updateMutation . isPending } >
74
- { t . saveAction }
77
+ < Button variant = "light" onClick = { handleBackAndSave } loading = { updateMutation . isPending } >
78
+ { t . actionBack }
75
79
</ Button >
76
80
< Button type = "submit" loading = { updateMutation . isPending } >
77
- { t . formAction }
81
+ { isLastQuestionnaire ? t . actionCompleteQuestionnaire : t . actionContinueNextPeriod }
78
82
</ Button >
79
83
</ Group >
80
84
</ Stack >
0 commit comments