1
1
import moment from 'moment'
2
- import React , { useEffect , useState } from 'react'
2
+ import React from 'react'
3
3
import PropTypes from 'prop-types'
4
4
import styles from './PhaseInput.module.scss'
5
5
import cn from 'classnames'
@@ -17,47 +17,31 @@ const inputTimeFormat = 'HH:mm'
17
17
const MAX_LENGTH = 5
18
18
19
19
const 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
23
21
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' )
48
23
49
24
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
+ } )
52
32
}
53
33
54
- const onDurationChange = ( e ) => {
34
+ const onDurationChange = ( e , isBlur = false ) => {
55
35
if ( e . length > MAX_LENGTH ) return null
56
36
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
+ } )
61
45
}
62
46
63
47
return (
@@ -96,17 +80,17 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex, isActive }) =>
96
80
< div className = { cn ( styles . field , styles . col2 ) } >
97
81
< span className = { styles . title } > Duration:</ span >
98
82
< 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
+ ) }
110
94
</ div >
111
95
</ div >
112
96
</ div >
@@ -122,9 +106,9 @@ PhaseInput.defaultProps = {
122
106
123
107
PhaseInput . propTypes = {
124
108
phase : PropTypes . shape ( ) . isRequired ,
125
- onUpdatePhase : PropTypes . func . isRequired ,
109
+ onUpdatePhase : PropTypes . func ,
126
110
readOnly : PropTypes . bool ,
127
- phaseIndex : PropTypes . string . isRequired ,
111
+ phaseIndex : PropTypes . number . isRequired ,
128
112
isActive : PropTypes . bool
129
113
}
130
114
export default PhaseInput
0 commit comments