11import moment from 'moment'
2- import React , { useEffect , useState } from 'react'
2+ import React from 'react'
33import PropTypes from 'prop-types'
44import styles from './PhaseInput.module.scss'
55import cn from 'classnames'
@@ -17,47 +17,31 @@ const inputTimeFormat = 'HH:mm'
1717const MAX_LENGTH = 5
1818
1919const PhaseInput = ( { onUpdatePhase, phase, readOnly, phaseIndex, isActive } ) => {
20- const [ startDate , setStartDate ] = useState ( )
21- const [ endDate , setEndDate ] = useState ( )
22- const [ duration , setDuration ] = useState ( )
20+ const { scheduledStartDate : startDate , scheduledEndDate : endDate , duration } = phase
2321
24- useEffect ( ( ) => {
25- if ( phase ) {
26- setStartDate ( phase . scheduledStartDate )
27- setEndDate ( phase . scheduledEndDate )
28- setDuration ( moment ( phase . scheduledEndDate ) . diff ( phase . scheduledStartDate , 'hours' ) )
29- }
30- } , [ ] )
31-
32- useEffect ( ( ) => {
33- if ( phase ) {
34- setStartDate ( phase . scheduledStartDate )
35- setEndDate ( phase . scheduledEndDate )
36- }
37- } , [ phase ] )
38-
39- useEffect ( ( ) => {
40- if ( ! readOnly ) {
41- onUpdatePhase ( {
42- startDate,
43- endDate,
44- duration
45- } )
46- }
47- } , [ startDate , endDate , duration ] )
22+ const getEndDate = ( startDate , duration ) => moment ( startDate ) . add ( duration , 'hours' )
4823
4924 const onStartDateChange = ( e ) => {
50- setStartDate ( moment ( e ) . format ( dateFormat ) )
51- setEndDate ( moment ( e ) . add ( duration , 'hours' ) . format ( dateFormat ) )
25+ let startDate = moment ( e ) . format ( dateFormat )
26+ let endDate = getEndDate ( startDate , duration )
27+ onUpdatePhase ( {
28+ startDate,
29+ endDate,
30+ duration
31+ } )
5232 }
5333
54- const onDurationChange = ( e ) => {
34+ const onDurationChange = ( e , isBlur = false ) => {
5535 if ( e . length > MAX_LENGTH ) return null
5636
57- const dur = parseInt ( e || 0 )
58- setDuration ( dur )
59- const end = moment ( startDate ) . add ( dur , 'hours' )
60- setEndDate ( moment ( end ) . format ( dateFormat ) )
37+ let duration = parseInt ( e || 0 )
38+ let endDate = getEndDate ( startDate , duration )
39+ onUpdatePhase ( {
40+ startDate,
41+ endDate,
42+ duration,
43+ isBlur
44+ } )
6145 }
6246
6347 return (
@@ -96,17 +80,17 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex, isActive }) =>
9680 < div className = { cn ( styles . field , styles . col2 ) } >
9781 < span className = { styles . title } > Duration:</ span >
9882 < div className = { styles . inputField } >
99- {
100- readOnly ? (
101- < span className = { styles . readOnlyValue } > { duration } </ span >
102- )
103- : < DurationInput
104- duration = { duration }
105- name = { phase . name }
106- onDurationChange = { onDurationChange }
107- index = { phaseIndex }
108- isActive
109- /> }
83+ { readOnly ? (
84+ < span className = { styles . readOnlyValue } > { duration } </ span >
85+ ) : (
86+ < DurationInput
87+ duration = { duration }
88+ name = { phase . name }
89+ onDurationChange = { onDurationChange }
90+ index = { phaseIndex }
91+ isActive
92+ />
93+ ) }
11094 </ div >
11195 </ div >
11296 </ div >
@@ -122,9 +106,9 @@ PhaseInput.defaultProps = {
122106
123107PhaseInput . propTypes = {
124108 phase : PropTypes . shape ( ) . isRequired ,
125- onUpdatePhase : PropTypes . func . isRequired ,
109+ onUpdatePhase : PropTypes . func ,
126110 readOnly : PropTypes . bool ,
127- phaseIndex : PropTypes . string . isRequired ,
111+ phaseIndex : PropTypes . number . isRequired ,
128112 isActive : PropTypes . bool
129113}
130114export default PhaseInput
0 commit comments