-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1117 from topcoder-platform/develop
Prod release - Timeline Template
- Loading branch information
Showing
7 changed files
with
224 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/components/ChallengeEditor/TimelineTemplate-Field/TimelineTemplate-Field.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
@import "../../../styles/includes"; | ||
|
||
.row { | ||
box-sizing: border-box; | ||
display: flex; | ||
flex-direction: row; | ||
margin: 30px 30px 0 30px; | ||
align-content: space-between; | ||
justify-content: flex-start; | ||
|
||
.field { | ||
@include upto-sm { | ||
display: block; | ||
padding-bottom: 10px; | ||
} | ||
|
||
label { | ||
@include roboto-bold(); | ||
|
||
font-size: 16px; | ||
line-height: 19px; | ||
font-weight: 500; | ||
color: $tc-gray-80; | ||
} | ||
|
||
&.col1 { | ||
max-width: 185px; | ||
min-width: 185px; | ||
margin-right: 14px; | ||
white-space: nowrap; | ||
display: flex; | ||
align-items: center; | ||
flex-grow: 1; | ||
|
||
span { | ||
color: $tc-red; | ||
} | ||
} | ||
|
||
&.col2.error { | ||
color: $tc-red; | ||
margin-top: -25px; | ||
} | ||
&.col2 { | ||
align-self: flex-end; | ||
width: 80%; | ||
margin-bottom: auto; | ||
margin-top: auto; | ||
display: flex; | ||
flex-direction: row; | ||
max-width: 600px; | ||
min-width: 600px; | ||
} | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
src/components/ChallengeEditor/TimelineTemplate-Field/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import _ from 'lodash' | ||
import React, { Component } from 'react' | ||
import PropTypes from 'prop-types' | ||
import Select from '../../Select' | ||
import cn from 'classnames' | ||
import styles from './TimelineTemplate-Field.module.scss' | ||
|
||
class TimelineTemplateField extends Component { | ||
constructor (props) { | ||
super(props) | ||
this.state = { | ||
validOptions: [], | ||
selectedOption: {} | ||
} | ||
|
||
this.checkData = this.checkData.bind(this) | ||
this.loadSelectedOption = this.loadSelectedOption.bind(this) | ||
this.getErrorMessage = this.getErrorMessage.bind(this) | ||
} | ||
|
||
componentDidMount () { | ||
const { challengeTimelines, timelineTemplates, challenge, currentTemplate } = this.props | ||
this.checkData(challengeTimelines, timelineTemplates, challenge, currentTemplate) | ||
} | ||
|
||
componentWillUnmount () { | ||
this.props.onUpdateSelect(this.state.selectedOption.value) | ||
} | ||
|
||
loadSelectedOption (validOptions, value) { | ||
const { timelineTemplates, challenge } = this.props | ||
const selectedOption = {} | ||
const selectedTemplate = _.find(timelineTemplates, t => t.id === (value)) | ||
this.props.onUpdateSelect(selectedTemplate) | ||
|
||
selectedOption.label = selectedTemplate.name | ||
selectedOption.value = selectedTemplate.id | ||
this.setState({ | ||
validOptions, | ||
matchString: `${challenge.typeId}-${challenge.trackId}-${value}`, | ||
selectedOption | ||
}) | ||
} | ||
|
||
checkData (challengeTimelines, timelineTemplates, challenge, currentTemplate) { | ||
const availableTemplates = _.filter(challengeTimelines, ct => ct.typeId === challenge.typeId && ct.trackId === challenge.trackId) | ||
const availableTemplateIds = availableTemplates.map(tt => tt.timelineTemplateId) | ||
const validOptions = _.filter(timelineTemplates, t => _.includes(availableTemplateIds, t.id)) | ||
const defaultValue = _.get(_.find(availableTemplates, t => t.isDefault), 'timelineTemplateId') | ||
if (currentTemplate && currentTemplate.id) { | ||
if (!_.includes(_.map(validOptions, o => o.id), currentTemplate.id)) { | ||
this.loadSelectedOption(validOptions, defaultValue) | ||
} else { | ||
this.loadSelectedOption(validOptions, currentTemplate.id) | ||
} | ||
} else if (defaultValue) { | ||
return this.loadSelectedOption(validOptions, defaultValue) | ||
} | ||
} | ||
|
||
getErrorMessage () { | ||
if (!this.props.challenge.typeId || !this.props.challenge.trackId) { | ||
return 'Please select a work type and format to enable this field' | ||
} else if (this.props.challenge.submitTriggered && !this.props.currentTemplate) { | ||
return 'Timeline template is required field' | ||
} else if (this.state.validOptions.length === 0) { | ||
return 'Sorry, there are no available timeline templates for the options you have selected' | ||
} | ||
return null | ||
} | ||
|
||
render () { | ||
const { challengeTimelines, timelineTemplates, challenge, currentTemplate } = this.props | ||
const hasSelectedTypeAndTrack = !_.isEmpty(challenge.typeId) && !_.isEmpty(challenge.trackId) | ||
if ((hasSelectedTypeAndTrack && this.state.validOptions.length === 0) || this.state.matchString !== `${challenge.typeId}-${challenge.trackId}-${this.state.selectedOption.value}`) { | ||
this.checkData(challengeTimelines, timelineTemplates, challenge, currentTemplate) | ||
} | ||
const error = this.getErrorMessage() | ||
return ( | ||
<> | ||
<div className={styles.row}> | ||
<div className={cn(styles.field, styles.col1)}> | ||
<label htmlFor='type'>Timeline Template {!this.props.readOnly && <span>*</span>} :</label> | ||
</div> | ||
<div className={cn(styles.field, styles.col2, { [styles.disabled]: this.state.validOptions.length === 0 })}> | ||
<Select | ||
value={this.state.selectedOption} | ||
name='timelineTemplateId' | ||
options={this.state.validOptions.map(type => ({ label: type.name, value: type.id }))} | ||
placeholder='Timeline Template' | ||
isClearable={false} | ||
onChange={(e) => { | ||
this.loadSelectedOption(this.state.validOptions, e.value) | ||
}} | ||
isDisabled={this.state.validOptions.length === 0 || this.props.readOnly} | ||
/> | ||
</div> | ||
</div> | ||
{ error && <div className={styles.row}> | ||
<div className={cn(styles.field, styles.col1)} /> | ||
<div className={cn(styles.field, styles.col2, styles.error)}> | ||
{error} | ||
</div> | ||
</div> } | ||
</> | ||
) | ||
} | ||
} | ||
|
||
TimelineTemplateField.defaultProps = { | ||
challengeTimelines: [], | ||
timelineTemplates: [], | ||
readOnly: false, | ||
currentTemplate: null | ||
} | ||
|
||
TimelineTemplateField.propTypes = { | ||
challengeTimelines: PropTypes.arrayOf(PropTypes.shape()).isRequired, | ||
timelineTemplates: PropTypes.arrayOf(PropTypes.shape()).isRequired, | ||
challenge: PropTypes.shape().isRequired, | ||
onUpdateSelect: PropTypes.func.isRequired, | ||
readOnly: PropTypes.bool, | ||
currentTemplate: PropTypes.shape() | ||
} | ||
|
||
export default TimelineTemplateField |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters