1
1
import moment from 'moment'
2
- import React , { useEffect , useMemo } from 'react'
2
+ import React , { useEffect , useMemo , useRef } from 'react'
3
3
import PropTypes from 'prop-types'
4
4
import styles from './PhaseInput.module.scss'
5
5
import cn from 'classnames'
6
- import 'react-day-picker/lib/style.css'
7
- import 'rc-time-picker/assets/index.css'
8
- import DateTime from '@nateradebaugh/react-datetime'
9
6
import isAfter from 'date-fns/isAfter'
10
7
import subDays from 'date-fns/subDays'
11
- import '@nateradebaugh/react-datetime/scss/styles.scss'
12
8
import DurationInput from '../DurationInput'
13
- import { getPhaseHoursMinutes , getPhaseEndDate } from '../../util/date'
9
+ import { getPhaseHoursMinutes , getPhaseEndDate , getPhaseDuration } from '../../util/date'
10
+ import DateInput from '../DateInput'
14
11
15
12
const dateFormat = 'MM/DD/YYYY HH:mm'
16
13
const inputDateFormat = 'MM/dd/yyyy'
@@ -21,6 +18,7 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
21
18
const { scheduledStartDate : startDate , scheduledEndDate : endDate , duration, isStartTimeActive, isDurationActive } = phase
22
19
23
20
const durationHoursMinutes = useMemo ( ( ) => getPhaseHoursMinutes ( duration ) , [ duration ] )
21
+ const endDateInputRef = useRef ( )
24
22
25
23
const onStartDateChange = ( e ) => {
26
24
let startDate = moment ( e ) . format ( dateFormat )
@@ -32,6 +30,20 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
32
30
} )
33
31
}
34
32
33
+ const onEndDateChange = ( e ) => {
34
+ let endDate = moment ( e ) . format ( dateFormat )
35
+ let duration = getPhaseDuration ( startDate , endDate )
36
+ if ( duration > 0 ) {
37
+ onUpdatePhase ( {
38
+ startDate,
39
+ endDate,
40
+ duration
41
+ } )
42
+ } else {
43
+ endDateInputRef . current . forceReset ( )
44
+ }
45
+ }
46
+
35
47
useEffect ( ( ) => {
36
48
if ( ! startDate && onUpdatePhase ) {
37
49
let startDate = moment ( ) . format ( dateFormat )
@@ -71,9 +83,9 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
71
83
< span className = { styles . readOnlyValue } > { moment ( startDate ) . format ( dateFormat ) } </ span >
72
84
)
73
85
: (
74
- < DateTime
86
+ < DateInput
75
87
className = { styles . dateTimeInput }
76
- value = { moment ( startDate ) . format ( dateFormat ) }
88
+ value = { moment ( startDate ) . toDate ( ) }
77
89
onChange = { onStartDateChange }
78
90
isValidDate = { ( current ) => {
79
91
const yesterday = subDays ( new Date ( ) , 1 )
@@ -87,7 +99,21 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
87
99
< div className = { cn ( styles . field , styles . col2 ) } >
88
100
< span className = { styles . title } > End Date:</ span >
89
101
< div className = { styles . dayPicker } >
90
- < span className = { styles . readOnlyValue } > { moment ( endDate ) . format ( dateFormat ) } </ span >
102
+ { ( readOnly || ! isDurationActive ) ? (
103
+ < span className = { styles . readOnlyValue } > { moment ( endDate ) . format ( dateFormat ) } </ span >
104
+ ) : (
105
+ < DateInput
106
+ ref = { endDateInputRef }
107
+ className = { styles . dateTimeInput }
108
+ value = { moment ( endDate ) . toDate ( ) }
109
+ onChange = { onEndDateChange }
110
+ isValidDate = { ( current ) => {
111
+ return isAfter ( current , moment ( startDate ) . toDate ( ) )
112
+ } }
113
+ dateFormat = { inputDateFormat }
114
+ timeFormat = { inputTimeFormat }
115
+ />
116
+ ) }
91
117
</ div >
92
118
</ div >
93
119
< div className = { cn ( styles . field , styles . col2 ) } >
0 commit comments