Skip to content

#175 Fix up the Gantt charts #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/components/ChallengeEditor/ChallengeSchedule-Field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Select from '../../Select'
import { parseSVG } from '../../../util/svg'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faAngleDown, faTrash } from '@fortawesome/free-solid-svg-icons'
import { getPhaseEndDate } from '../../../util/date'

const GANTT_ROW_HEIGHT = 45
const GANTT_FOOTER_HEIGHT = 40
Expand All @@ -27,6 +26,7 @@ class ChallengeScheduleField extends Component {
this.toggleEditMode = this.toggleEditMode.bind(this)
this.renderTimeLine = this.renderTimeLine.bind(this)
this.getChallengePhase = this.getChallengePhase.bind(this)
this.getAllPhases = this.getAllPhases.bind(this)
}

componentDidMount () {
Expand Down Expand Up @@ -71,13 +71,22 @@ class ChallengeScheduleField extends Component {
return challengePhase || phase
}

getAllPhases () {
const { allPhases, challenge } = this.props
if (allPhases && allPhases.length) {
return allPhases
}
return challenge.phases
}

renderTimeLine () {
const { challenge } = this.props
if (_.isEmpty(challenge.phases) || typeof challenge.phases[0] === 'undefined') {
const allPhases = this.getAllPhases()
if (_.isEmpty(allPhases) || typeof allPhases[0] === 'undefined') {
return null
}

const timelines = []
let timelines = []
timelines.push(
[
{ type: 'string', label: 'Task ID' },
Expand All @@ -91,20 +100,22 @@ class ChallengeScheduleField extends Component {
)

var secondToMilisecond = 1000 // = 1 second
_.map(challenge.phases, (p, index) => {
_.map(allPhases, (p, index) => {
const phase = this.getChallengePhase(p)
if (phase) {
if (phase && timelines) {
var startDate
if (p.scheduledStartDate) {
startDate = moment(p.scheduledStartDate).toDate()
} else {
startDate = (index === 0 || index === 1) || !phase.predecessor ? moment(challenge.startDate).toDate() : getPhaseEndDate(index - 1, challenge, this.getChallengePhase).toDate()
timelines = null
return
}
var endDate
if (p.scheduledEndDate) {
endDate = moment(p.scheduledEndDate).toDate()
} else {
endDate = getPhaseEndDate(index, challenge, this.getChallengePhase).toDate()
timelines = null
return
}

var currentTime = moment().valueOf()
Expand Down Expand Up @@ -266,7 +277,7 @@ class ChallengeScheduleField extends Component {
<div id='gantt-chart' className={styles.chart}>
<Chart
width={'100%'}
height={`${(challenge.phases.length * GANTT_ROW_HEIGHT) + GANTT_FOOTER_HEIGHT}px`}
height={`${(this.getAllPhases().length * GANTT_ROW_HEIGHT) + GANTT_FOOTER_HEIGHT}px`}
chartType='Gantt'
loader={<div>Loading Timelines</div>}
data={timelines}
Expand Down Expand Up @@ -337,11 +348,13 @@ class ChallengeScheduleField extends Component {
}

ChallengeScheduleField.defaultProps = {
templates: []
templates: [],
allPhases: []
}

ChallengeScheduleField.propTypes = {
templates: PropTypes.arrayOf(PropTypes.shape()).isRequired,
allPhases: PropTypes.arrayOf(PropTypes.shape()),
challengePhases: PropTypes.arrayOf(PropTypes.shape()).isRequired,
challenge: PropTypes.shape().isRequired,
removePhase: PropTypes.func.isRequired,
Expand Down
40 changes: 34 additions & 6 deletions src/components/ChallengeEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { convertDollarToInteger, validateValue } from '../../util/input-check'
import dropdowns from './mock-data/dropdowns'
import styles from './ChallengeEditor.module.scss'
import { withRouter } from 'react-router-dom'
import { createChallenge, updateChallenge, createResource, deleteResource } from '../../services/challenges'
import { createChallenge, updateChallenge, createResource, deleteResource, fetchChallenge } from '../../services/challenges'

const theme = {
container: styles.modalContainer
Expand Down Expand Up @@ -434,7 +434,21 @@ class ChallengeEditor extends Component {
};

async checkToCreateDraftChallenge () {
if (this.state.isSaving || !this.props.isNew || this.getCurrentChallengeId()) return
if (this.state.isSaving || !this.props.isNew) return

if (this.getCurrentChallengeId()) {
if (!this.state.draftChallenge.data.id) {
// retreive draft challenge
this.setState({ isSaving: true })
try {
const draftChallenge = await fetchChallenge(this.getCurrentChallengeId())
this.setState({ isSaving: false, draftChallenge: { data: draftChallenge } })
} catch (e) {
this.setState({ isSaving: false })
}
}
return
}
const challenge = this.collectChallengeData('Draft')
// can't create challenge without empty project
// I add fake data to make sure create challenge success
Expand Down Expand Up @@ -499,7 +513,12 @@ class ChallengeEditor extends Component {
async autoUpdateChallenge () {
if (this.state.isSaving || !this.getCurrentChallengeId() || !this.isValidChallenge()) return
const challenge = this.collectChallengeData(this.getCurrentChallengeStatus())
await this.updateAllChallengeInfo(challenge)
try {
const draftChallenge = await this.updateAllChallengeInfo(challenge)
this.setState({ draftChallenge })
} catch (e) {
this.setState({ isSaving: false })
}
}

getCurrentChallengeStatus () {
Expand Down Expand Up @@ -587,7 +606,7 @@ class ChallengeEditor extends Component {
}

render () {
const { isLaunch, isConfirm, challenge, isOpenAdvanceSettings } = this.state
const { isLaunch, isConfirm, challenge, isOpenAdvanceSettings, draftChallenge } = this.state
const {
isNew,
isDraft,
Expand Down Expand Up @@ -635,7 +654,6 @@ class ChallengeEditor extends Component {
}
}
const currentChallengeId = this.getCurrentChallengeId()

return (
<div className={styles.wrapper}>
<Helmet title={getTitle(isNew)} />
Expand Down Expand Up @@ -698,7 +716,17 @@ class ChallengeEditor extends Component {
<GroupsField groups={metadata.groups} onUpdateMultiSelect={this.onUpdateMultiSelect} challenge={challenge} />
</React.Fragment>
) }
<ChallengeScheduleField templates={metadata.timelineTemplates} challengePhases={metadata.challengePhases} removePhase={this.removePhase} resetPhase={this.resetPhase} challenge={challenge} onUpdateSelect={this.onUpdateSelect} onUpdatePhase={this.onUpdatePhase} onUpdateOthers={this.onUpdateOthers} />
<ChallengeScheduleField
templates={metadata.timelineTemplates}
challengePhases={metadata.challengePhases}
removePhase={this.removePhase}
resetPhase={this.resetPhase}
challenge={challenge}
onUpdateSelect={this.onUpdateSelect}
onUpdatePhase={this.onUpdatePhase}
onUpdateOthers={this.onUpdateOthers}
allPhases={draftChallenge.data.phases}
/>
</div>
<div className={styles.group}>
<div className={styles.title}>Public specification</div>
Expand Down
40 changes: 0 additions & 40 deletions src/util/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import moment from 'moment'
import 'moment-duration-format'
import _ from 'lodash'

const HOUR_MS = 60 * 60 * 1000
const DAY_MS = 24 * HOUR_MS
Expand Down Expand Up @@ -42,42 +41,3 @@ export const getRoundFormattedDuration = (duration) => {
else format = 'H[h]'
return moment.duration(duration).format(format)
}

/**
* Get phase end date
* @param index the phase index
* @param challenge the challenge data
* @param getChallengePhase a function to get the challenge phase from the metadata for the specified phase
* @returns {moment.Moment}
*/
export const getPhaseEndDate = (index, challenge, getChallengePhase) => {
const map = {}
const alreadyCalculated = {}
_.each(challenge.phases, p => {
if (p) {
const phase = getChallengePhase(p)
if (phase) map[phase.id] = phase.duration
}
})
const finalDate = moment(challenge.startDate)
const phase = challenge.phases[index] && getChallengePhase(challenge.phases[index])
if (phase) {
finalDate.add(phase.duration, 'hours')
if (!phase.predecessor) {
return finalDate
}
}

for (let i = index; i >= 0 && challenge.phases[i]; i -= 1) {
const challengePhase = getChallengePhase(challenge.phases[i])
const predecessor = challengePhase && challengePhase.predecessor
if (predecessor) {
if (!alreadyCalculated[predecessor]) {
alreadyCalculated[predecessor] = true
finalDate.add(map[predecessor], 'hours')
}
}
}

return finalDate
}