diff --git a/src/projects/detail/components/FeatureSelector/CustomFeatureForm.jsx b/src/projects/detail/components/FeatureSelector/CustomFeatureForm.jsx index 533e47b34..4997a60c2 100644 --- a/src/projects/detail/components/FeatureSelector/CustomFeatureForm.jsx +++ b/src/projects/detail/components/FeatureSelector/CustomFeatureForm.jsx @@ -35,9 +35,9 @@ class CustomFeatureForm extends Component { } toggleFeature() { - const { addFeature, isEdittable } = this.props + const { toggleFeature, isEdittable } = this.props if (isEdittable) { - addFeature({ ...this.state.data, disabled: !!this.state.isActive }) + toggleFeature(this.state.data.id, !!this.state.isActive) } } @@ -65,7 +65,7 @@ class CustomFeatureForm extends Component { id: data.title.toLowerCase().replace(/\s/g, '_'), categoryId: 'custom', notes: '' - }, featureData, data)) + }, featureData, { title : data.title.trim() })) // assumes addFeature to be a synchronous call, otherwise it could lead to inconsistent UI state // e.g. if async addFeature fails, it would close the edit mode // this call is needed here because debounce call (for notes change) is closing the edit mode if @@ -73,14 +73,14 @@ class CustomFeatureForm extends Component { this.setState({ editMode : false }) } - onChange(fieldName, value) { + onChange(data){ const { featureData } = this.props // following check is needed to prevent adding the feature again after removing // because forms' onChange event gets fire with only form data when we lose focus from the form // alternative to this check is to put the change handler on textarea instead of form if (featureData) {// feature is already added - const data = {} - data[fieldName] = value + // trim the notes (as of now only notes field is auto updated) + data.notes = data.notes.trim() this.props.updateFeature(_.merge({}, featureData, data)) } } @@ -114,12 +114,12 @@ class CustomFeatureForm extends Component { }
- + { (!isAdded || editMode) && : null } diff --git a/src/projects/detail/components/FeatureSelector/DefaultFeatureForm.jsx b/src/projects/detail/components/FeatureSelector/DefaultFeatureForm.jsx index bb7fc0908..ab3467d42 100644 --- a/src/projects/detail/components/FeatureSelector/DefaultFeatureForm.jsx +++ b/src/projects/detail/components/FeatureSelector/DefaultFeatureForm.jsx @@ -32,11 +32,11 @@ class DefaultFeatureForm extends Component { } toggleFeature() { - const { removeFeature, addFeature, featureDesc, isEdittable } = this.props + const { toggleFeature, addFeature, featureDesc, isEdittable } = this.props if (isEdittable) { if (this.state.isActive) { // remove feature - removeFeature(featureDesc.id) + toggleFeature(featureDesc.id, true) } else { // add feature addFeature({ @@ -50,13 +50,15 @@ class DefaultFeatureForm extends Component { onChange(data) { const { featureData } = this.props + // trim the notes (as of now only notes field is auto updated) + data.notes = data.notes.trim() this.props.updateFeature(_.merge({}, featureData, data)) } render() { const { featureDesc, featureData, isEdittable } = this.props const { isActive } = this.state - const _debouncedOnChange = _.debounce(this.onChange, 2000, { trailing: true, maxWait: 10000 }) + // const _debouncedOnChange = _.debounce(this.onChange, 2000, { trailing: true, maxWait: 10000 }) return (
@@ -72,7 +74,7 @@ class DefaultFeatureForm extends Component {

{ featureDesc.description }

{ isActive ? - + f.id === featureId) - idx > -1 ? this.removeFeature(featureId) : this.addFeature(featureId) - } - renderCustomFeatureForm() { this.setState({ selectedFeatureId: null, @@ -262,27 +257,40 @@ class FeaturePicker extends Component { this.props.onSave(newState.activeFeatureList) } - addFeature(feature) { - let newState - const featureIndex = _.findIndex(this.state.activeFeatureList, (f) => f.id === feature.id ) - // if feature is already added and is custom feature, update feature - if (feature.categoryId === 'custom' && featureIndex >= 0) { - newState = update(this.state, { + toggleFeature(featureId, disable) { + const featureIndex = _.findIndex(this.state.activeFeatureList, (f) => f.id === featureId ) + if (featureIndex >= 0) { + const feature = this.state.activeFeatureList[featureIndex] + let featureListQuery + // separate update query for custom and standard features + // when disabling, we remove standard feature from the list while update custom feature with disabled flag + if (feature.categoryId === 'custom') { + feature.disabled = disable + featureListQuery = { $splice : [[featureIndex, 1, feature]] } + } else { + featureListQuery = { $splice: [[featureIndex, 1]] } + } + const newState = update(this.state, { activeFeatureCount: { $set: this.state.activeFeatureCount - 1 }, - activeFeatureList: { $splice : [[featureIndex, 1, feature]] }, - selectedFeatureId: { $set : feature.id } - }) - } else {// add standard feature - newState = update(this.state, { - activeFeatureCount: {$set: this.state.activeFeatureCount + 1}, - activeFeatureList: { $push : [feature] }, + activeFeatureList: featureListQuery, selectedFeatureId: { $set : feature.id } }) + this.setState(newState) + this.props.onSave(newState.activeFeatureList) } + } + + addFeature(feature) { + const newState = update(this.state, { + activeFeatureCount: {$set: this.state.activeFeatureCount + 1}, + activeFeatureList: { $push : [feature] }, + selectedFeatureId: { $set : feature.id } + }) this.setState(newState) this.props.onSave(newState.activeFeatureList) } + // removeFeature is only called for custom feature removeFeature(featureId) { // lookup index const idx = _.findIndex(this.state.activeFeatureList, f => f.id === featureId ) @@ -328,6 +336,7 @@ class FeaturePicker extends Component { featureDesc={selectedFeature} featureData={selectedFeatureData} updateFeature={this.updateSelectedFeature} + toggleFeature={ this.toggleFeature } addFeature={this.addFeature} removeFeature={this.removeFeature} />) @@ -360,6 +369,7 @@ class FeaturePicker extends Component { isEdittable={isEdittable} featureData={selectedFeatureData} updateFeature={this.updateSelectedFeature} + toggleFeature={ this. toggleFeature } addFeature={this.addFeature} removeFeature={this.removeFeature} onCancel={this.renderDefaultFeatureForm}