Skip to content

Commit 6aba0cc

Browse files
authored
Merge pull request #1377 from 52cs/fix-1375
Fix 1375
2 parents d5937d8 + cba9c35 commit 6aba0cc

File tree

4 files changed

+59
-64
lines changed

4 files changed

+59
-64
lines changed

src/components/ChallengeEditor/ChallengeView/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { isBetaMode } from '../../../util/cookie'
2222
import { loadGroupDetails } from '../../../actions/challenges'
2323
import { REVIEW_TYPES, CONNECT_APP_URL, PHASE_PRODUCT_CHALLENGE_ID_FIELD } from '../../../config/constants'
2424
import PhaseInput from '../../PhaseInput'
25-
import { v4 as uuidv4 } from 'uuid'
2625

2726
const ChallengeView = ({
2827
projectDetail,
@@ -190,10 +189,11 @@ const ChallengeView = ({
190189
</>
191190
)}
192191
{
193-
phases.map((phase) => (
192+
phases.map((phase, index) => (
194193
<PhaseInput
195194
phase={phase}
196-
phaseIndex={uuidv4()}
195+
phaseIndex={index}
196+
key={index}
197197
readOnly
198198
/>
199199
))

src/components/ChallengeEditor/index.js

+17-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { pick } from 'lodash/fp'
99
import { withRouter } from 'react-router-dom'
1010
import { toastr } from 'react-redux-toastr'
1111
import xss from 'xss'
12-
import { v4 as uuidv4 } from 'uuid'
1312

1413
import {
1514
VALIDATION_VALUE_TYPE,
@@ -815,9 +814,20 @@ class ChallengeEditor extends Component {
815814
onUpdatePhaseDate (phase, index) {
816815
const { phases } = this.state.challenge
817816
let newChallenge = _.cloneDeep(this.state.challenge)
818-
newChallenge.phases[index]['duration'] = phase.duration
819-
newChallenge.phases[index]['scheduledStartDate'] = phase.startDate
820-
newChallenge.phases[index]['scheduledEndDate'] = phase.endDate
817+
if (phase.isBlur && newChallenge.phases[index]['name'] === 'Submission') {
818+
newChallenge.phases[index]['duration'] = _.max([
819+
newChallenge.phases[index - 1]['duration'],
820+
phase.duration
821+
])
822+
newChallenge.phases[index]['scheduledEndDate'] =
823+
moment(newChallenge.phases[index]['scheduledStartDate'])
824+
.add(newChallenge.phases[index]['duration'], 'hours')
825+
.format('MM/DD/YYYY HH:mm')
826+
} else {
827+
newChallenge.phases[index]['duration'] = phase.duration
828+
newChallenge.phases[index]['scheduledStartDate'] = phase.startDate
829+
newChallenge.phases[index]['scheduledEndDate'] = phase.endDate
830+
}
821831

822832
for (let phaseIndex = index + 1; phaseIndex < phases.length; ++phaseIndex) {
823833
if (newChallenge.phases[phaseIndex]['name'] === 'Submission') {
@@ -1610,15 +1620,12 @@ class ChallengeEditor extends Component {
16101620
phases.map((phase, index) => (
16111621
<PhaseInput
16121622
phase={phase}
1613-
phaseIndex={uuidv4()}
1623+
phaseIndex={index}
1624+
key={index}
16141625
readOnly={false}
16151626
isActive={this.isPhaseEditable(index)}
16161627
onUpdatePhase={(item) => {
1617-
if ((item.startDate && !moment(item.startDate).isSame(phase.scheduledStartDate)) ||
1618-
(item.endDate && !moment(item.endDate).isSame(phase.scheduledEndDate))
1619-
) {
1620-
this.onUpdatePhaseDate(item, index)
1621-
}
1628+
this.onUpdatePhaseDate(item, index)
16221629
}}
16231630
/>
16241631
)

src/components/DurationInput/index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ const DurationInput = ({ duration, onDurationChange, index, isActive }) => {
1414
ref={inputRef}
1515
min={1}
1616
type='number'
17-
value={Number(duration).toString()}
17+
value={duration}
1818
onChange={e => {
1919
e.preventDefault()
2020
onDurationChange(e.target.value)
2121
}}
22+
onBlur={e => {
23+
e.preventDefault()
24+
onDurationChange(e.target.value, true)
25+
}}
2226
autoFocus={inputRef.current === document.activeElement}
2327
disabled={!isActive}
2428
/>
@@ -27,9 +31,9 @@ const DurationInput = ({ duration, onDurationChange, index, isActive }) => {
2731
}
2832

2933
DurationInput.propTypes = {
30-
duration: PropTypes.string,
34+
duration: PropTypes.number,
3135
onDurationChange: PropTypes.func.isRequired,
32-
index: PropTypes.string.isRequired,
36+
index: PropTypes.number.isRequired,
3337
isActive: PropTypes.bool.isRequired
3438
}
3539

src/components/PhaseInput/index.js

+32-48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import moment from 'moment'
2-
import React, { useEffect, useState } from 'react'
2+
import React from 'react'
33
import PropTypes from 'prop-types'
44
import styles from './PhaseInput.module.scss'
55
import cn from 'classnames'
@@ -17,47 +17,31 @@ const inputTimeFormat = 'HH:mm'
1717
const MAX_LENGTH = 5
1818

1919
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
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

123107
PhaseInput.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
}
130114
export default PhaseInput

0 commit comments

Comments
 (0)