Skip to content

Commit 59deda4

Browse files
author
vikasrohit
authored
Merge pull request #449 from appirio-tech/feature/new-custom-feature-picker
Feature/new custom feature picker
2 parents 0dad2fb + 7c50f20 commit 59deda4

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

src/projects/detail/components/FeatureSelector/CustomFeatureForm.jsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class CustomFeatureForm extends Component {
3535
}
3636

3737
toggleFeature() {
38-
const { addFeature, isEdittable } = this.props
38+
const { toggleFeature, isEdittable } = this.props
3939
if (isEdittable) {
40-
addFeature({ ...this.state.data, disabled: !!this.state.isActive })
40+
toggleFeature(this.state.data.id, !!this.state.isActive)
4141
}
4242
}
4343

@@ -65,22 +65,22 @@ class CustomFeatureForm extends Component {
6565
id: data.title.toLowerCase().replace(/\s/g, '_'),
6666
categoryId: 'custom',
6767
notes: ''
68-
}, featureData, data))
68+
}, featureData, { title : data.title.trim() }))
6969
// assumes addFeature to be a synchronous call, otherwise it could lead to inconsistent UI state
7070
// e.g. if async addFeature fails, it would close the edit mode
7171
// this call is needed here because debounce call (for notes change) is closing the edit mode if
7272
// we do set the editMode in componentWillReceiveProps method
7373
this.setState({ editMode : false })
7474
}
7575

76-
onChange(fieldName, value) {
76+
onChange(data){
7777
const { featureData } = this.props
7878
// following check is needed to prevent adding the feature again after removing
7979
// because forms' onChange event gets fire with only form data when we lose focus from the form
8080
// alternative to this check is to put the change handler on textarea instead of form
8181
if (featureData) {// feature is already added
82-
const data = {}
83-
data[fieldName] = value
82+
// trim the notes (as of now only notes field is auto updated)
83+
data.notes = data.notes.trim()
8484
this.props.updateFeature(_.merge({}, featureData, data))
8585
}
8686
}
@@ -114,12 +114,12 @@ class CustomFeatureForm extends Component {
114114
</div>
115115
}
116116
<div className="feature-form-content">
117-
<Formsy.Form className="custom-feature-form" disabled={!isEdittable} onValidSubmit={ this.onSave }>
117+
<Formsy.Form className="custom-feature-form" disabled={!isEdittable} onChange={ this.onChange } onValidSubmit={ this.onSave }>
118118
{ (!isAdded || editMode) &&
119119
<TCFormFields.TextInput
120120
name="title"
121121
label="Feature name"
122-
validations="minLength:1" required
122+
validations="isRequired" required
123123
validationError="Feature name is required"
124124
wrapperClass="row"
125125
value={ _.get(data, 'title', '') }
@@ -131,7 +131,6 @@ class CustomFeatureForm extends Component {
131131
label="Feature Notes"
132132
wrapperClass="feature-notes"
133133
value={ _.get(data, 'notes', '') }
134-
onChange={ this.onChange }
135134
/>
136135
: null
137136
}

src/projects/detail/components/FeatureSelector/DefaultFeatureForm.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class DefaultFeatureForm extends Component {
3232
}
3333

3434
toggleFeature() {
35-
const { removeFeature, addFeature, featureDesc, isEdittable } = this.props
35+
const { toggleFeature, addFeature, featureDesc, isEdittable } = this.props
3636
if (isEdittable) {
3737
if (this.state.isActive) {
3838
// remove feature
39-
removeFeature(featureDesc.id)
39+
toggleFeature(featureDesc.id, true)
4040
} else {
4141
// add feature
4242
addFeature({
@@ -50,13 +50,15 @@ class DefaultFeatureForm extends Component {
5050

5151
onChange(data) {
5252
const { featureData } = this.props
53+
// trim the notes (as of now only notes field is auto updated)
54+
data.notes = data.notes.trim()
5355
this.props.updateFeature(_.merge({}, featureData, data))
5456
}
5557

5658
render() {
5759
const { featureDesc, featureData, isEdittable } = this.props
5860
const { isActive } = this.state
59-
const _debouncedOnChange = _.debounce(this.onChange, 2000, { trailing: true, maxWait: 10000 })
61+
// const _debouncedOnChange = _.debounce(this.onChange, 2000, { trailing: true, maxWait: 10000 })
6062
return (
6163
<div className="feature-form">
6264
<div className="feature-title-row flex space-between">
@@ -72,7 +74,7 @@ class DefaultFeatureForm extends Component {
7274
<p className="feature-description">{ featureDesc.description }</p>
7375
{
7476
isActive ?
75-
<Formsy.Form className="predefined-feature-form" disabled={!isEdittable} onChange={ _debouncedOnChange }>
77+
<Formsy.Form className="predefined-feature-form" disabled={!isEdittable} onChange={ this.onChange }>
7678
<TCFormFields.Textarea
7779
name="notes"
7880
wrapperClass="feature-notes"

src/projects/detail/components/FeatureSelector/FeaturePicker.jsx

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ class FeaturePicker extends Component {
212212
}
213213
this.addFeature = this.addFeature.bind(this)
214214
this.removeFeature = this.removeFeature.bind(this)
215-
this.selectFeature = this.selectFeature.bind(this)
216215
this.toggleFeature = this.toggleFeature.bind(this)
216+
this.selectFeature = this.selectFeature.bind(this)
217217
this.updateSelectedFeature = this.updateSelectedFeature.bind(this)
218218
this.renderCustomFeatureForm = this.renderCustomFeatureForm.bind(this)
219219
this.renderDefaultFeatureForm = this.renderDefaultFeatureForm.bind(this)
@@ -232,11 +232,6 @@ class FeaturePicker extends Component {
232232
})
233233
}
234234

235-
toggleFeature(featureId) {
236-
const idx = _.findIndex(this.state.activeFeatureList, f => f.id === featureId)
237-
idx > -1 ? this.removeFeature(featureId) : this.addFeature(featureId)
238-
}
239-
240235
renderCustomFeatureForm() {
241236
this.setState({
242237
selectedFeatureId: null,
@@ -262,27 +257,40 @@ class FeaturePicker extends Component {
262257
this.props.onSave(newState.activeFeatureList)
263258
}
264259

265-
addFeature(feature) {
266-
let newState
267-
const featureIndex = _.findIndex(this.state.activeFeatureList, (f) => f.id === feature.id )
268-
// if feature is already added and is custom feature, update feature
269-
if (feature.categoryId === 'custom' && featureIndex >= 0) {
270-
newState = update(this.state, {
260+
toggleFeature(featureId, disable) {
261+
const featureIndex = _.findIndex(this.state.activeFeatureList, (f) => f.id === featureId )
262+
if (featureIndex >= 0) {
263+
const feature = this.state.activeFeatureList[featureIndex]
264+
let featureListQuery
265+
// separate update query for custom and standard features
266+
// when disabling, we remove standard feature from the list while update custom feature with disabled flag
267+
if (feature.categoryId === 'custom') {
268+
feature.disabled = disable
269+
featureListQuery = { $splice : [[featureIndex, 1, feature]] }
270+
} else {
271+
featureListQuery = { $splice: [[featureIndex, 1]] }
272+
}
273+
const newState = update(this.state, {
271274
activeFeatureCount: { $set: this.state.activeFeatureCount - 1 },
272-
activeFeatureList: { $splice : [[featureIndex, 1, feature]] },
273-
selectedFeatureId: { $set : feature.id }
274-
})
275-
} else {// add standard feature
276-
newState = update(this.state, {
277-
activeFeatureCount: {$set: this.state.activeFeatureCount + 1},
278-
activeFeatureList: { $push : [feature] },
275+
activeFeatureList: featureListQuery,
279276
selectedFeatureId: { $set : feature.id }
280277
})
278+
this.setState(newState)
279+
this.props.onSave(newState.activeFeatureList)
281280
}
281+
}
282+
283+
addFeature(feature) {
284+
const newState = update(this.state, {
285+
activeFeatureCount: {$set: this.state.activeFeatureCount + 1},
286+
activeFeatureList: { $push : [feature] },
287+
selectedFeatureId: { $set : feature.id }
288+
})
282289
this.setState(newState)
283290
this.props.onSave(newState.activeFeatureList)
284291
}
285292

293+
// removeFeature is only called for custom feature
286294
removeFeature(featureId) {
287295
// lookup index
288296
const idx = _.findIndex(this.state.activeFeatureList, f => f.id === featureId )
@@ -328,6 +336,7 @@ class FeaturePicker extends Component {
328336
featureDesc={selectedFeature}
329337
featureData={selectedFeatureData}
330338
updateFeature={this.updateSelectedFeature}
339+
toggleFeature={ this.toggleFeature }
331340
addFeature={this.addFeature}
332341
removeFeature={this.removeFeature}
333342
/>)
@@ -360,6 +369,7 @@ class FeaturePicker extends Component {
360369
isEdittable={isEdittable}
361370
featureData={selectedFeatureData}
362371
updateFeature={this.updateSelectedFeature}
372+
toggleFeature={ this. toggleFeature }
363373
addFeature={this.addFeature}
364374
removeFeature={this.removeFeature}
365375
onCancel={this.renderDefaultFeatureForm}

0 commit comments

Comments
 (0)