@@ -57,20 +57,28 @@ export async function action({ context, params, request }: Route.ActionArgs) {
57
57
}
58
58
59
59
case 'next' : {
60
- const expiryYear = Number ( formData . get ( 'expiry-year' ) ) ;
61
- const expiryMonth = Number ( formData . get ( 'expiry-month' ) ) ;
60
+ const formValues = {
61
+ documentType : formData . get ( 'document-type' ) ?. toString ( ) ,
62
+ expiryYear : formData . get ( 'expiry-year' ) ?. toString ( ) ,
63
+ expiryMonth : formData . get ( 'expiry-month' ) ?. toString ( ) ,
64
+ } ;
62
65
63
- const parseResult = v . safeParse ( secondaryDocumentSchema , {
64
- documentType : String ( formData . get ( 'document-type' ) ) ,
65
- expiryYear : expiryYear ,
66
- expiryMonth : expiryMonth ,
67
- } ) ;
66
+ const parseResult = v . safeParse ( secondaryDocumentSchema , formValues ) ;
68
67
69
68
if ( ! parseResult . success ) {
70
- return data (
71
- { errors : v . flatten < typeof secondaryDocumentSchema > ( parseResult . issues ) . nested } ,
72
- { status : HttpStatusCodes . BAD_REQUEST } ,
73
- ) ;
69
+ const formErrors = v . flatten < typeof secondaryDocumentSchema > ( parseResult . issues ) . nested ;
70
+
71
+ machineActor . send ( {
72
+ type : 'setFormData' ,
73
+ data : {
74
+ secondaryDocument : {
75
+ values : formValues ,
76
+ errors : formErrors ,
77
+ } ,
78
+ } ,
79
+ } ) ;
80
+
81
+ return data ( { formValues : formValues , formErrors : formErrors } , { status : HttpStatusCodes . BAD_REQUEST } ) ;
74
82
}
75
83
76
84
machineActor . send ( { type : 'submitSecondaryDocuments' , data : parseResult . output } ) ;
@@ -90,27 +98,30 @@ export async function loader({ context, request }: Route.LoaderArgs) {
90
98
91
99
const { lang, t } = await getTranslation ( request , handle . i18nNamespace ) ;
92
100
const machineActor = loadMachineActor ( context . session , request , 'secondary-docs' ) ;
101
+ const machineContext = machineActor ?. getSnapshot ( ) . context ;
93
102
94
103
return {
95
104
documentTitle : t ( 'protected:secondary-identity-document.page-title' ) ,
96
105
localizedApplicantSecondaryDocumentChoices : getLocalizedApplicantSecondaryDocumentChoices ( lang ) ,
97
- defaultFormValues : machineActor ?. getSnapshot ( ) . context . secondaryDocument ,
106
+ formValues : machineContext ?. formData ?. secondaryDocument ?. values ?? machineContext ?. secondaryDocument ,
107
+ formErrors : machineContext ?. formData ?. secondaryDocument ?. errors ,
98
108
} ;
99
109
}
100
110
101
- export default function SecondaryDoc ( { loaderData , actionData , params } : Route . ComponentProps ) {
111
+ export default function SecondaryDoc ( { actionData , loaderData , params } : Route . ComponentProps ) {
102
112
const { t } = useTranslation ( handle . i18nNamespace ) ;
103
113
104
114
const fetcherKey = useId ( ) ;
105
115
const fetcher = useFetcher < Info [ 'actionData' ] > ( { key : fetcherKey } ) ;
106
-
107
116
const isSubmitting = fetcher . state !== 'idle' ;
108
- const errors = fetcher . data ?. errors ;
117
+
118
+ const formValues = fetcher . data ?. formValues ?? loaderData . formValues ;
119
+ const formErrors = fetcher . data ?. formErrors ?? loaderData . formErrors ;
109
120
110
121
const docOptions = loaderData . localizedApplicantSecondaryDocumentChoices . map ( ( { id, name } ) => ( {
111
122
value : id ,
112
123
children : name ,
113
- defaultChecked : id === loaderData . defaultFormValues ?. documentType ,
124
+ defaultChecked : id === loaderData . formValues ?. documentType ,
114
125
} ) ) ;
115
126
116
127
return (
@@ -126,11 +137,11 @@ export default function SecondaryDoc({ loaderData, actionData, params }: Route.C
126
137
name = "document-type"
127
138
options = { docOptions }
128
139
required
129
- errorMessage = { t ( getSingleKey ( errors ?. documentType ) ) }
140
+ errorMessage = { t ( getSingleKey ( formErrors ?. documentType ) ) }
130
141
/>
131
142
< DatePickerField
132
- defaultMonth = { loaderData . defaultFormValues ?. expiryMonth }
133
- defaultYear = { loaderData . defaultFormValues ?. expiryYear }
143
+ defaultMonth = { Number ( formValues ?. expiryMonth ) }
144
+ defaultYear = { Number ( formValues ?. expiryYear ) }
134
145
id = "expiry-date-id"
135
146
legend = { t ( 'protected:secondary-identity-document.expiry-date.title' ) }
136
147
required
@@ -139,8 +150,8 @@ export default function SecondaryDoc({ loaderData, actionData, params }: Route.C
139
150
year : 'expiry-year' ,
140
151
} }
141
152
errorMessages = { {
142
- year : t ( getSingleKey ( errors ?. expiryYear ) ) ,
143
- month : t ( getSingleKey ( errors ?. expiryMonth ) ) ,
153
+ year : t ( getSingleKey ( formErrors ?. expiryYear ) ) ,
154
+ month : t ( getSingleKey ( formErrors ?. expiryMonth ) ) ,
144
155
} }
145
156
/>
146
157
< InputFile
@@ -150,10 +161,6 @@ export default function SecondaryDoc({ loaderData, actionData, params }: Route.C
150
161
name = "document"
151
162
label = { t ( 'protected:secondary-identity-document.upload-document.title' ) }
152
163
required
153
- /*
154
- TODO: Enable file upload
155
- errorMessage={t(getSingleKey(errors?.document))}
156
- */
157
164
/>
158
165
</ div >
159
166
< div className = "mt-8 flex flex-row-reverse flex-wrap items-center justify-end gap-3" >
0 commit comments