|
| 1 | +import _ from 'lodash' |
| 2 | +import React, { Component } from 'react' |
| 3 | +import PropTypes from 'prop-types' |
| 4 | +import Select from '../../Select' |
| 5 | +import cn from 'classnames' |
| 6 | +import styles from './TimelineTemplate-Field.module.scss' |
| 7 | + |
| 8 | +class TimelineTemplateField extends Component { |
| 9 | + constructor (props) { |
| 10 | + super(props) |
| 11 | + this.state = { |
| 12 | + validOptions: [], |
| 13 | + selectedOption: {} |
| 14 | + } |
| 15 | + |
| 16 | + this.checkData = this.checkData.bind(this) |
| 17 | + this.loadSelectedOption = this.loadSelectedOption.bind(this) |
| 18 | + this.getErrorMessage = this.getErrorMessage.bind(this) |
| 19 | + } |
| 20 | + |
| 21 | + componentDidMount () { |
| 22 | + this.checkData() |
| 23 | + } |
| 24 | + |
| 25 | + shouldComponentUpdate (nextProps) { |
| 26 | + const { onUpdateSelect, challengeTimelines, timelineTemplates, challenge } = nextProps |
| 27 | + const hasSelectedTypeAndTrack = !_.isEmpty(challenge.typeId) && !_.isEmpty(challenge.trackId) |
| 28 | + if ((hasSelectedTypeAndTrack && this.state.validOptions.length === 0) || this.state.matchString !== `${challenge.typeId}-${challenge.trackId}-${this.state.selectedOption.value}`) { |
| 29 | + this.checkData(onUpdateSelect, challengeTimelines, timelineTemplates, challenge) |
| 30 | + } |
| 31 | + return true |
| 32 | + } |
| 33 | + |
| 34 | + loadSelectedOption (validOptions, value) { |
| 35 | + if (!value) return |
| 36 | + const { timelineTemplates, challenge } = this.props |
| 37 | + const selectedOption = {} |
| 38 | + const selectedTemplate = _.find(timelineTemplates, t => t.id === (value)) |
| 39 | + if (!selectedTemplate) return |
| 40 | + selectedOption.label = selectedTemplate.name |
| 41 | + selectedOption.value = selectedTemplate.id |
| 42 | + this.setState({ |
| 43 | + validOptions, |
| 44 | + matchString: `${challenge.typeId}-${challenge.trackId}-${value}`, |
| 45 | + selectedOption |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + checkData () { |
| 50 | + const { onUpdateSelect, challengeTimelines, timelineTemplates, challenge } = this.props |
| 51 | + const availableTemplates = _.filter(challengeTimelines, ct => ct.typeId === challenge.typeId && ct.trackId === challenge.trackId) |
| 52 | + const availableTemplateIds = availableTemplates.map(tt => tt.timelineTemplateId) |
| 53 | + const validOptions = _.filter(timelineTemplates, t => _.includes(availableTemplateIds, t.id)) |
| 54 | + const defaultValue = _.get(_.find(availableTemplates, t => t.isDefault), 'timelineTemplateId') |
| 55 | + if (challenge.timelineTemplateId) { |
| 56 | + if (!_.includes(validOptions.map(o => o.id), challenge.timelineTemplateId)) { |
| 57 | + onUpdateSelect(defaultValue || '', false, 'timelineTemplateId') |
| 58 | + return this.loadSelectedOption(validOptions, defaultValue) |
| 59 | + } |
| 60 | + } else if (defaultValue) { |
| 61 | + onUpdateSelect(defaultValue, false, 'timelineTemplateId') |
| 62 | + return this.loadSelectedOption(validOptions, defaultValue) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + getErrorMessage () { |
| 67 | + if (!this.props.challenge.typeId || !this.props.challenge.trackId) { |
| 68 | + return 'Please select a work type and format to enable this field' |
| 69 | + } else if (this.props.challenge.submitTriggered && !this.props.challenge.timelineTemplateId) { |
| 70 | + return 'Timeline template is required field' |
| 71 | + } else if (this.state.validOptions.length === 0) { |
| 72 | + return 'Sorry, there are no available timeline templates for the options you have selected' |
| 73 | + } |
| 74 | + return null |
| 75 | + } |
| 76 | + |
| 77 | + render () { |
| 78 | + const error = this.getErrorMessage() |
| 79 | + return ( |
| 80 | + <> |
| 81 | + <div className={styles.row}> |
| 82 | + <div className={cn(styles.field, styles.col1)}> |
| 83 | + <label htmlFor='type'>Timeline Template <span>*</span> :</label> |
| 84 | + </div> |
| 85 | + <div className={cn(styles.field, styles.col2, { [styles.disabled]: this.state.validOptions.length === 0 })}> |
| 86 | + <Select |
| 87 | + value={this.state.selectedOption} |
| 88 | + name='timelineTemplateId' |
| 89 | + options={this.state.validOptions.map(type => ({ label: type.name, value: type.id }))} |
| 90 | + placeholder='Timeline Template' |
| 91 | + isClearable={false} |
| 92 | + onChange={(e) => this.props.onUpdateSelect(e.value, false, 'timelineTemplateId')} |
| 93 | + isDisabled={this.state.validOptions.length === 0} |
| 94 | + /> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + { error && <div className={styles.row}> |
| 98 | + <div className={cn(styles.field, styles.col1)} /> |
| 99 | + <div className={cn(styles.field, styles.col2, styles.error)}> |
| 100 | + {error} |
| 101 | + </div> |
| 102 | + </div> } |
| 103 | + </> |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +TimelineTemplateField.defaultProps = { |
| 109 | + challengeTimelines: [], |
| 110 | + timelineTemplates: [] |
| 111 | +} |
| 112 | + |
| 113 | +TimelineTemplateField.propTypes = { |
| 114 | + challengeTimelines: PropTypes.arrayOf(PropTypes.shape()).isRequired, |
| 115 | + timelineTemplates: PropTypes.arrayOf(PropTypes.shape()).isRequired, |
| 116 | + challenge: PropTypes.shape().isRequired, |
| 117 | + onUpdateSelect: PropTypes.func.isRequired |
| 118 | +} |
| 119 | + |
| 120 | +export default TimelineTemplateField |
0 commit comments