1
1
import moment from 'moment'
2
- import React , { useEffect } from 'react'
2
+ import React , { useEffect , useMemo } from 'react'
3
3
import PropTypes from 'prop-types'
4
4
import styles from './PhaseInput.module.scss'
5
5
import cn from 'classnames'
@@ -10,6 +10,7 @@ import isAfter from 'date-fns/isAfter'
10
10
import subDays from 'date-fns/subDays'
11
11
import '@nateradebaugh/react-datetime/scss/styles.scss'
12
12
import DurationInput from '../DurationInput'
13
+ import { getPhaseHoursMinutes , getPhaseEndDate } from '../../util/date'
13
14
14
15
const dateFormat = 'MM/DD/YYYY HH:mm'
15
16
const inputDateFormat = 'MM/dd/yyyy'
@@ -19,11 +20,11 @@ const MAX_LENGTH = 5
19
20
const PhaseInput = ( { onUpdatePhase, phase, readOnly, phaseIndex } ) => {
20
21
const { scheduledStartDate : startDate , scheduledEndDate : endDate , duration, isStartTimeActive, isDurationActive } = phase
21
22
22
- const getEndDate = ( startDate , duration ) => moment ( startDate ) . add ( duration , 'hours' ) . format ( dateFormat )
23
+ const durationHoursMinutes = useMemo ( ( ) => getPhaseHoursMinutes ( duration ) , [ duration ] )
23
24
24
25
const onStartDateChange = ( e ) => {
25
26
let startDate = moment ( e ) . format ( dateFormat )
26
- let endDate = getEndDate ( startDate , duration )
27
+ let endDate = getPhaseEndDate ( startDate , duration )
27
28
onUpdatePhase ( {
28
29
startDate,
29
30
endDate,
@@ -34,7 +35,7 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
34
35
useEffect ( ( ) => {
35
36
if ( ! startDate && onUpdatePhase ) {
36
37
let startDate = moment ( ) . format ( dateFormat )
37
- let endDate = getEndDate ( startDate , duration )
38
+ let endDate = getPhaseEndDate ( startDate , duration )
38
39
onUpdatePhase ( {
39
40
startDate,
40
41
endDate,
@@ -44,10 +45,10 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
44
45
} , [ startDate ] )
45
46
46
47
const onDurationChange = ( e , isBlur = false ) => {
47
- if ( e . length > MAX_LENGTH ) return null
48
+ if ( ` ${ e } ` . length > MAX_LENGTH ) return null
48
49
49
- let duration = parseInt ( e || 0 )
50
- let endDate = getEndDate ( startDate , duration )
50
+ let duration = e
51
+ let endDate = getPhaseEndDate ( startDate , duration )
51
52
onUpdatePhase ( {
52
53
startDate,
53
54
endDate,
@@ -90,10 +91,15 @@ const PhaseInput = ({ onUpdatePhase, phase, readOnly, phaseIndex }) => {
90
91
</ div >
91
92
</ div >
92
93
< div className = { cn ( styles . field , styles . col2 ) } >
93
- < span className = { styles . title } > Duration: </ span >
94
+ < span className = { styles . title } > Duration</ span >
94
95
< div className = { styles . inputField } >
95
96
{ readOnly ? (
96
- < span className = { styles . readOnlyValue } > { duration } </ span >
97
+ < div className = { styles . readOnlyDurationContainer } >
98
+ < span > Hours: </ span >
99
+ < span className = { styles . readOnlyValue } > { durationHoursMinutes . hours } </ span >
100
+ < span > Minutes: </ span >
101
+ < span className = { styles . readOnlyValue } > { durationHoursMinutes . minutes } </ span >
102
+ </ div >
97
103
) : (
98
104
< DurationInput
99
105
duration = { duration }
0 commit comments