11import React , { useEffect , useState } from 'react'
22
3- import { Link , Redirect , withRouter } from "react-router-dom"
3+ import { Link , withRouter } from "react-router-dom"
44import forms from "./forms.json"
55import { Grid , Button , Divider , Typography } from '@material-ui/core'
66import { routes } from '../../lib/router.js'
@@ -9,9 +9,8 @@ import {Page} from '../shared'
99
1010import FormInput from './FormInput.jsx'
1111import { useStore } from '../../hooks'
12- import { addHours } from 'date-fns'
12+ import { addHours , format } from 'date-fns'
1313
14- import initialValues from "../../db/initialValues.json"
1514
1615/**
1716 * Form component
@@ -21,28 +20,27 @@ import initialValues from "../../db/initialValues.json"
2120 * @param {'DEP'|'DCA'|'POR' } props.match.params.type - Type of form
2221 * @param {number } props.match.params.messageId - id of message if form is used for editing
2322 */
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 ( )
2625 const [ t ] = useTranslation ( "forms" )
2726
28- const [ canEdit , setCanEdit ] = useState ( false )
27+ // const [setCanEdit] = useState(false)
2928 const [ baseMessage , setBaseMessage ] = useState ( { } )
3029
3130 const [ validType , setValidType ] = useState ( false )
3231
3332
3433 useEffect ( ( ) => {
35- setCanEdit ( messageId && type === "DCA" && messages . find ( m => m . TM === "DCA" && m . RN === parseInt ( messageId , 10 ) ) )
3634 const newValidType = Object . keys ( forms ) . includes ( type )
3735 setValidType ( newValidType )
3836 if ( newValidType )
3937 setBaseMessage ( messages . find ( m => m . TM === type ) || { } )
40- } , [ type , messageId ] )
38+ } , [ ] )
4139
4240
4341 useEffect ( ( ) => {
4442 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.
4644 const now = new Date ( )
4745 switch ( type ) {
4846 case "DEP" :
@@ -63,11 +61,27 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
6361 }
6462 break
6563
66- case "DCA " :
64+ case "DCA0 " :
6765 newFields = { // These values will be preset, no matter if there is a base message.
6866 ...newFields ,
6967 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
7185 }
7286 if ( Object . keys ( baseMessage ) . length ) {
7387 newFields = { // Preset from base (previous message, with the same type)
@@ -79,10 +93,7 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
7993 CA : baseMessage . CA ,
8094 GE : baseMessage . GE ,
8195 GS : baseMessage . GS ,
82- DU : baseMessage . DU ,
83- QI : baseMessage . QI ,
84- startFishingSpot : position ,
85- endFishingSpot : baseMessage . endFishingSpot
96+ QI : baseMessage . QI
8697 }
8798 }
8899 break
@@ -92,11 +103,19 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
92103 portArrival : now // NOTE: try to calculate from position, speed and PO (port)
93104 }
94105 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+
95114 newFields = { // Preset from base (previous message, with the same type)
96115 ...newFields ,
97- KG : baseMessage . KG ,
116+ KG : sum ,
98117 LS : baseMessage . LS ,
99- OB : baseMessage . OB ,
118+ OB : sum ,
100119 PO : baseMessage . PO
101120 }
102121 }
@@ -116,7 +135,7 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
116135
117136 // /** REVIEW: When this componentDidMount is called,
118137 // * 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 .
120139 // */
121140
122141 // const now = new Date()
@@ -186,18 +205,22 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
186205 */
187206 const handleSubmit = e => {
188207 e . preventDefault ( )
208+ if ( type === "DCA0" ) {
209+ toggleDCAStart ( true )
210+ history . push ( routes . DASHBOARD )
211+ return
212+ }
189213 handleDialog ( { type, submit : ( ) => submitMessage ( type ) } )
190214 }
191215
192216 const form = forms [ type ] // Extract form from forms.json
193217
194- const isAllowed = ( isEnRoute ? [ "DCA" , "POR" ] . includes ( type ) : type === "DEP" ) || ( canEdit && type === "DCA" )
195218
196219 return (
197220 < Page style = { { marginBottom : 64 } } title = { t ( `${ type } .title` ) } >
198221 < Grid alignItems = "center" container direction = "column" spacing = { 16 } >
199222 < 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
201224 < Grid container direction = "column" key = { id } spacing = { 16 } style = { { paddingBottom : 32 } } >
202225 < Grid component = { Typography } item variant = "subtitle2" xs = { 12 } > { t ( `${ type } .steps.${ id } ` ) } </ Grid >
203226 { 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}}}) => {
217240 ) }
218241 < Divider style = { { marginTop : 16 } } />
219242 </ Grid >
220- ) :
221- < Redirect to = { routes . DASHBOARD } /> // If the form is invalid, redirect to the dashboard
243+ )
222244 }
223245 </ Grid >
224246 < Grid container item justify = "center" >
@@ -227,7 +249,7 @@ export const Form = ({match: {path, params: {type, messageId}}}) => {
227249 color = "secondary"
228250 component = { Link }
229251 size = "large"
230- to = { routes . DASHBOARD }
252+ to = { routes . HOMEPAGE }
231253 >
232254 { t ( "links.back" ) }
233255 </ Button >
0 commit comments