@@ -7,9 +7,10 @@ import Step3 from "../../components/Step3";
7
7
import Step4 from "../../components/Step4" ;
8
8
import Receipt from "../../components/Receipt" ;
9
9
import CancelButton from "../../components/CancelButton" ;
10
- import { format , getISOWeek } from "date-fns" ;
10
+ import { format , getISOWeek , parseISO } from "date-fns" ;
11
11
import { Dispatch , FormEventHandler , SetStateAction , useState } from "react" ;
12
- import { ActivityType , Data , Day , LoadedData , SavedDates } from "../../models/Data" ;
12
+ import { ActivityType , Data , Day , SavedDates } from "../../models/Data" ;
13
+ import { LoadedData } from "../../models/LoadedData" ;
13
14
14
15
type InitialState = {
15
16
currentId : string ;
@@ -55,11 +56,18 @@ export async function getServerSideProps() {
55
56
questionProceed : null
56
57
}
57
58
58
- const response = await fetch ( process . env . DP_RAPP_API_URL + '/api/v1/get/' + currentId ) ;
59
- if ( response . ok ) {
60
- loadedData = await response . json ( ) ;
59
+ // Get saved values
60
+ try {
61
+ const response = await fetch ( process . env . DP_RAPP_API_URL + '/api/v1/get/' + currentId ) ;
62
+ if ( response . ok ) {
63
+ loadedData = await response . json ( ) ;
64
+ }
65
+ } catch ( e ) {
66
+ console . warn ( "Kunne ikke hente lagrede verdier" , e ) ;
61
67
}
62
68
69
+
70
+ // Data comes to page as props
63
71
return {
64
72
props : {
65
73
currentId,
@@ -77,15 +85,17 @@ export default function Page(props: InitialState) {
77
85
const [ showReceipt , setShowReceipt ] = useState < boolean > ( false ) ;
78
86
const [ error , setError ] = useState < string > ( '' ) ;
79
87
88
+ // Pre-fetched data (initial state) comes to the page as props
80
89
const { currentId, loadedData } = props ;
81
90
82
- // Use loaded data as initial state
91
+ // Convert loaded days to SavedDates
83
92
const loadedSavedDates : SavedDates = { }
84
93
loadedData ?. days . forEach ( ( day ) => {
85
94
// @ts -ignore
86
- loadedSavedDates [ Date . parse ( day . date + " 12 :00:00" ) ] = { type : ActivityType [ day . type ] , hours : day . hours }
95
+ loadedSavedDates [ parseISO ( day . date + "T12 :00:00" ) . getTime ( ) ] = { type : ActivityType [ day . type ] , hours : day . hours }
87
96
} ) ;
88
97
98
+ // Use loaded data as initial state for variables
89
99
const [ questionWork , setQuestionWork ] = useState < boolean | null > ( loadedData . questionWork ) ;
90
100
const [ questionMeasures , setQuestionMeasures ] = useState < boolean | null > ( loadedData . questionMeasures ) ;
91
101
const [ questionIllness , setQuestionIllness ] = useState < boolean | null > ( loadedData . questionIllness ) ;
@@ -109,13 +119,39 @@ export default function Page(props: InitialState) {
109
119
}
110
120
} ;
111
121
112
- const nextStep = ( ) => {
122
+ const nextStep = async ( ) => {
113
123
if ( currentStep < maxStep ) {
124
+ await save ( ) ;
114
125
setCurrentStep ( currentStep + 1 ) ;
115
126
}
116
127
} ;
117
128
129
+ const save = async ( ) => {
130
+ const response = await postData ( '/api/save' ) ;
131
+
132
+ if ( ! response . ok ) {
133
+ setError ( 'Feil i vårt baksystem. Kunne ikke lagre data' ) ;
134
+ }
135
+
136
+ // Hide loader
137
+ setShowLoader ( false ) ;
138
+ }
139
+
118
140
const send = async ( ) => {
141
+ const response = await postData ( '/api/send' ) ;
142
+
143
+ if ( response . ok ) {
144
+ setCurrentStep ( currentStep + 1 ) ;
145
+ setShowReceipt ( true ) ;
146
+ } else {
147
+ setError ( 'Feil i vårt baksystem. Prøv senere' ) ;
148
+ }
149
+
150
+ // Hide loader
151
+ setShowLoader ( false ) ;
152
+ }
153
+
154
+ const postData = async ( endpoint : string ) => {
119
155
// Reset error
120
156
setError ( '' ) ;
121
157
@@ -146,9 +182,6 @@ export default function Page(props: InitialState) {
146
182
// Send the data to the server in JSON format.
147
183
const JSONdata = JSON . stringify ( data ) ;
148
184
149
- // API endpoint where we send form data.
150
- const endpoint = '/api/save' ;
151
-
152
185
// Form the request for sending data to the server.
153
186
const options = {
154
187
// The method is POST because we are sending data.
@@ -162,19 +195,7 @@ export default function Page(props: InitialState) {
162
195
} ;
163
196
164
197
// Send the form data to our forms API on Vercel and get a response.
165
- const response = await fetch ( endpoint , options ) ;
166
-
167
- if ( response . ok ) {
168
- // Get the response data from server as JSON.
169
- // const result = await response.json();
170
- setCurrentStep ( currentStep + 1 ) ;
171
- setShowReceipt ( true ) ;
172
- } else {
173
- setError ( 'Feil i vårt baksystem. Prøv senere' ) ;
174
- }
175
-
176
- // Hide loader
177
- setShowLoader ( false ) ;
198
+ return await fetch ( endpoint , options ) ;
178
199
}
179
200
180
201
const commonFormProps : CommonFormProps = {
@@ -204,7 +225,8 @@ export default function Page(props: InitialState) {
204
225
return (
205
226
< main >
206
227
< Heading level = "1" size = "xlarge" > Dagpenger rapportering</ Heading >
207
- < Heading level = "2" size = "medium" > Uke { getISOWeek ( startDate ) } - { getISOWeek ( endDate ) } ({ startDateStr } - { endDateStr } )</ Heading >
228
+ < Heading level = "2"
229
+ size = "medium" > Uke { getISOWeek ( startDate ) } - { getISOWeek ( endDate ) } ({ startDateStr } - { endDateStr } )</ Heading >
208
230
209
231
< Divider />
210
232
0 commit comments