11import moment from 'moment'
2- import React , { useEffect , useMemo } from 'react'
2+ import React , { useEffect , useMemo , useRef } from 'react'
33import PropTypes from 'prop-types'
44import styles from './PhaseInput.module.scss'
55import 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'
96import isAfter from 'date-fns/isAfter'
107import subDays from 'date-fns/subDays'
11- import '@nateradebaugh/react-datetime/scss/styles.scss'
128import DurationInput from '../DurationInput'
13- import { getPhaseHoursMinutes , getPhaseEndDate } from '../../util/date'
9+ import { getPhaseHoursMinutes , getPhaseEndDate , getPhaseDuration } from '../../util/date'
10+ import DateInput from '../DateInput'
1411
1512const dateFormat = 'MM/DD/YYYY HH:mm'
1613const inputDateFormat = 'MM/dd/yyyy'
@@ -21,6 +18,7 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
2118 const { scheduledStartDate : startDate , scheduledEndDate : endDate , duration, isStartTimeActive, isDurationActive } = phase
2219
2320 const durationHoursMinutes = useMemo ( ( ) => getPhaseHoursMinutes ( duration ) , [ duration ] )
21+ const endDateInputRef = useRef ( )
2422
2523 const onStartDateChange = ( e ) => {
2624 let startDate = moment ( e ) . format ( dateFormat )
@@ -32,6 +30,20 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
3230 } )
3331 }
3432
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+
3547 useEffect ( ( ) => {
3648 if ( ! startDate && onUpdatePhase ) {
3749 let startDate = moment ( ) . format ( dateFormat )
@@ -71,9 +83,9 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
7183 < span className = { styles . readOnlyValue } > { moment ( startDate ) . format ( dateFormat ) } </ span >
7284 )
7385 : (
74- < DateTime
86+ < DateInput
7587 className = { styles . dateTimeInput }
76- value = { moment ( startDate ) . format ( dateFormat ) }
88+ value = { moment ( startDate ) . toDate ( ) }
7789 onChange = { onStartDateChange }
7890 isValidDate = { ( current ) => {
7991 const yesterday = subDays ( new Date ( ) , 1 )
@@ -87,7 +99,21 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
8799 < div className = { cn ( styles . field , styles . col2 ) } >
88100 < span className = { styles . title } > End Date:</ span >
89101 < 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+ ) }
91117 </ div >
92118 </ div >
93119 < div className = { cn ( styles . field , styles . col2 ) } >
0 commit comments