1
1
import React , { useEffect , useState } from 'react'
2
2
3
- import { Link , Redirect , withRouter } from "react-router-dom"
3
+ import { Link , withRouter } from "react-router-dom"
4
4
import forms from "./forms.json"
5
5
import { Grid , Button , Divider , Typography } from '@material-ui/core'
6
6
import { routes } from '../../lib/router.js'
@@ -9,9 +9,8 @@ import {Page} from '../shared'
9
9
10
10
import FormInput from './FormInput.jsx'
11
11
import { useStore } from '../../hooks'
12
- import { addHours } from 'date-fns'
12
+ import { addHours , format } from 'date-fns'
13
13
14
- import initialValues from "../../db/initialValues.json"
15
14
16
15
/**
17
16
* Form component
@@ -21,28 +20,27 @@ import initialValues from "../../db/initialValues.json"
21
20
* @param {'DEP'|'DCA'|'POR' } props.match.params.type - Type of form
22
21
* @param {number } props.match.params.messageId - id of message if form is used for editing
23
22
*/
24
- export const Form = ( { match : { path, params : { type, messageId} } } ) => {
25
- const { fields, isEnRoute , handleFieldChange, messages, position, handleDialog, submitMessage} = useStore ( )
23
+ export const Form = ( { history , match : { path, params : { type, /* messageId*/ } } } ) => {
24
+ const { fields, handleFieldChange, messages, position, handleDialog, submitMessage, trips , toggleDCAStart } = useStore ( )
26
25
const [ t ] = useTranslation ( "forms" )
27
26
28
- const [ canEdit , setCanEdit ] = useState ( false )
27
+ // const [setCanEdit] = useState(false)
29
28
const [ baseMessage , setBaseMessage ] = useState ( { } )
30
29
31
30
const [ validType , setValidType ] = useState ( false )
32
31
33
32
34
33
useEffect ( ( ) => {
35
- setCanEdit ( messageId && type === "DCA" && messages . find ( m => m . TM === "DCA" && m . RN === parseInt ( messageId , 10 ) ) )
36
34
const newValidType = Object . keys ( forms ) . includes ( type )
37
35
setValidType ( newValidType )
38
36
if ( newValidType )
39
37
setBaseMessage ( messages . find ( m => m . TM === type ) || { } )
40
- } , [ type , messageId ] )
38
+ } , [ ] )
41
39
42
40
43
41
useEffect ( ( ) => {
44
42
if ( ! validType ) return
45
- let newFields = { ...initialValues . fields } // Start with empty fields, to prevent rogue values.
43
+ let newFields = { } //{ ...initialValues.fields} // Start with empty fields, to prevent rogue values.
46
44
const now = new Date ( )
47
45
switch ( type ) {
48
46
case "DEP" :
@@ -63,11 +61,27 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
63
61
}
64
62
break
65
63
66
- case "DCA " :
64
+ case "DCA0 " :
67
65
newFields = { // These values will be preset, no matter if there is a base message.
68
66
...newFields ,
69
67
fishingStart : now ,
70
- expectedFishingStart : addHours ( now , 2 )
68
+ startFishingSpot : position ,
69
+ GE : "DRB"
70
+ }
71
+ if ( Object . keys ( baseMessage ) . length ) {
72
+ newFields = { // Preset from base (previous message, with the same type)
73
+ ...newFields ,
74
+ AC : baseMessage . AC ,
75
+ GS : baseMessage . GS ,
76
+ QI : baseMessage . QI
77
+ }
78
+ }
79
+ break
80
+ case "DCA" :
81
+ newFields = { // These values will be preset, no matter if there is a base message.
82
+ ...newFields ,
83
+ endFishingSpot : position ,
84
+ DU : fields . fishingStart ? parseInt ( format ( Date . now ( ) - new Date ( fields . fishingStart ) . getTime ( ) , "m" ) , 10 ) : 0
71
85
}
72
86
if ( Object . keys ( baseMessage ) . length ) {
73
87
newFields = { // Preset from base (previous message, with the same type)
@@ -79,10 +93,7 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
79
93
CA : baseMessage . CA ,
80
94
GE : baseMessage . GE ,
81
95
GS : baseMessage . GS ,
82
- DU : baseMessage . DU ,
83
- QI : baseMessage . QI ,
84
- startFishingSpot : position ,
85
- endFishingSpot : baseMessage . endFishingSpot
96
+ QI : baseMessage . QI
86
97
}
87
98
}
88
99
break
@@ -92,11 +103,19 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
92
103
portArrival : now // NOTE: try to calculate from position, speed and PO (port)
93
104
}
94
105
if ( Object . keys ( baseMessage ) . length ) {
106
+ const activeTrip = trips . find ( t => ! t . isFinished )
107
+ const sum = activeTrip ? activeTrip . DCAList . reduce ( ( acc , d ) => {
108
+ Object . entries ( d . CA ) . forEach ( ( [ type , weight ] ) => {
109
+ acc [ type ] ? acc [ type ] += weight : acc [ type ] = weight
110
+ } )
111
+ return acc
112
+ } , { } ) : { }
113
+
95
114
newFields = { // Preset from base (previous message, with the same type)
96
115
...newFields ,
97
- KG : baseMessage . KG ,
116
+ KG : sum ,
98
117
LS : baseMessage . LS ,
99
- OB : baseMessage . OB ,
118
+ OB : sum ,
100
119
PO : baseMessage . PO
101
120
}
102
121
}
@@ -116,7 +135,7 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
116
135
117
136
// /** REVIEW: When this componentDidMount is called,
118
137
// * messages is probably still empty,
119
- // * if the user opens the form in a new tab, instead of coming from the dashboard .
138
+ // * if the user opens the form in a new tab, instead of coming from the homepage .
120
139
// */
121
140
122
141
// const now = new Date()
@@ -186,18 +205,22 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
186
205
*/
187
206
const handleSubmit = e => {
188
207
e . preventDefault ( )
208
+ if ( type === "DCA0" ) {
209
+ toggleDCAStart ( true )
210
+ history . push ( routes . DASHBOARD )
211
+ return
212
+ }
189
213
handleDialog ( { type, submit : ( ) => submitMessage ( type ) } )
190
214
}
191
215
192
216
const form = forms [ type ] // Extract form from forms.json
193
217
194
- const isAllowed = ( isEnRoute ? [ "DCA" , "POR" ] . includes ( type ) : type === "DEP" ) || ( canEdit && type === "DCA" )
195
218
196
219
return (
197
220
< Page style = { { marginBottom : 64 } } title = { t ( `${ type } .title` ) } >
198
221
< Grid alignItems = "center" container direction = "column" spacing = { 16 } >
199
222
< Grid component = "form" item onSubmit = { handleSubmit } >
200
- { isAllowed ? form . map ( ( { id, step} ) => // If a valid form, iterate over its steps
223
+ { form . map ( ( { id, step} ) => // If a valid form, iterate over its steps
201
224
< Grid container direction = "column" key = { id } spacing = { 16 } style = { { paddingBottom : 32 } } >
202
225
< Grid component = { Typography } item variant = "subtitle2" xs = { 12 } > { t ( `${ type } .steps.${ id } ` ) } </ Grid >
203
226
{ step . map ( ( { id, dataId, type, dependent, options= { } } ) => // Iterate over all the input fields in a Form step
@@ -217,8 +240,7 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
217
240
) }
218
241
< Divider style = { { marginTop : 16 } } />
219
242
</ Grid >
220
- ) :
221
- < Redirect to = { routes . DASHBOARD } /> // If the form is invalid, redirect to the dashboard
243
+ )
222
244
}
223
245
</ Grid >
224
246
< Grid container item justify = "center" >
@@ -227,7 +249,7 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
227
249
color = "secondary"
228
250
component = { Link }
229
251
size = "large"
230
- to = { routes . DASHBOARD }
252
+ to = { routes . HOMEPAGE }
231
253
>
232
254
{ t ( "links.back" ) }
233
255
</ Button >
0 commit comments