Skip to content

Commit 2d821c8

Browse files
author
Vikas Agarwal
committed
Github issue#802, can add more screen
-- Utilized the on the fly update of other form sections on every form change event. Updated SpecQuestion component to use dirty project value for numberOfScreens question
1 parent 3607d94 commit 2d821c8

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

src/projects/detail/components/EditProjectForm.jsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component, PropTypes } from 'react'
22
import { withRouter } from 'react-router'
33
import Modal from 'react-modal'
44
import _ from 'lodash'
5+
import { unflatten } from 'flat'
56
import update from 'react-addons-update'
67
import FeaturePicker from './FeatureSelector/FeaturePicker'
78
import { Formsy, Icons } from 'appirio-tech-react-components'
@@ -34,8 +35,15 @@ class EditProjectForm extends Component {
3435
}
3536

3637
componentWillReceiveProps(nextProps) {
37-
// we receipt property updates from PROJECT_DIRTY REDUX state
38-
if (nextProps.project.isDirty) return
38+
// we received property updates from PROJECT_DIRTY REDUX state
39+
if (nextProps.project.isDirty) {
40+
this.setState({
41+
// sets a new state variable with dirty project
42+
// any component who wants to listen for unsaved changes in project form can listen to this state variable
43+
dirtyProject : Object.assign({}, nextProps.project)
44+
})
45+
return
46+
}
3947
let updatedProject = Object.assign({}, nextProps.project)
4048
if (this.state.isFeaturesDirty && !this.state.isSaving) {
4149
updatedProject = update(updatedProject, {
@@ -137,21 +145,22 @@ class EditProjectForm extends Component {
137145

138146
handleChange(change, isChanged) {
139147
if (isChanged) {
140-
this.props.fireProjectDirty(change)
148+
this.props.fireProjectDirty(unflatten(change))
141149
}
142150
}
143151

144152

145153
render() {
146154
const { isEdittable, sections } = this.props
147-
const { project } = this.state
155+
const { project, dirtyProject } = this.state
148156
const renderSection = (section, idx) => {
149157
const anySectionInvalid = _.some(this.props.sections, (s) => s.isInvalid)
150158
return (
151159
<div key={idx}>
152160
<SpecSection
153161
{...section}
154162
project={project}
163+
dirtyProject={dirtyProject}
155164
sectionNumber={idx + 1}
156165
resetFeatures={this.onFeaturesSaveAttachedClick}
157166
showFeaturesDialog={this.showFeaturesDialog}

src/projects/detail/components/SpecQuestions.jsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const getIcon = icon => {
2626
}
2727
}
2828

29-
const SpecQuestions = ({questions, project, resetFeatures, showFeaturesDialog, isRequired}) => {
29+
const SpecQuestions = ({questions, project, dirtyProject, resetFeatures, showFeaturesDialog, isRequired}) => {
3030

3131
const renderQ = (q, index) => {
3232
// let child = null
@@ -36,9 +36,9 @@ const SpecQuestions = ({questions, project, resetFeatures, showFeaturesDialog, i
3636
label: q.label,
3737
value: _.get(project, q.fieldName, '')
3838
}
39-
4039
if (q.fieldName === 'details.appDefinition.numberScreens') {
41-
const screens = _.get(project, 'details.appScreens.screens', [])
40+
const p = dirtyProject ? dirtyProject : project
41+
const screens = _.get(p, 'details.appScreens.screens', [])
4242
const definedScreens = screens.length
4343
_.each(q.options, (option) => {
4444
let maxValue = 0
@@ -145,9 +145,26 @@ const SpecQuestions = ({questions, project, resetFeatures, showFeaturesDialog, i
145145
}
146146

147147
SpecQuestions.propTypes = {
148+
/**
149+
* Original project object for which questions are to be rendered
150+
*/
148151
project: PropTypes.object.isRequired,
152+
/**
153+
* Dirty project with all unsaved changes
154+
*/
155+
dirtyProject: PropTypes.object,
156+
/**
157+
* Callback to be called when user clicks on Add/Edit Features button in feature picker component
158+
*/
149159
showFeaturesDialog: PropTypes.func.isRequired,
160+
/**
161+
* Call back to be called when user resets features from feature picker.
162+
* NOTE: It seems it is not used as of now by feature picker component
163+
*/
150164
resetFeatures: PropTypes.func.isRequired,
165+
/**
166+
* Array of questions to be rendered. This comes from the spec template for the product
167+
*/
151168
questions: PropTypes.arrayOf(PropTypes.object).isRequired
152169
}
153170

src/projects/detail/components/SpecSection.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { PROJECT_STATUS_DRAFT, PROJECT_NAME_MAX_LENGTH, PROJECT_REF_CODE_MAX_LEN
1010
const SpecSection = props => {
1111
const {
1212
project,
13+
dirtyProject,
1314
resetFeatures,
1415
showFeaturesDialog,
1516
id,
@@ -61,6 +62,7 @@ const SpecSection = props => {
6162
resetFeatures={resetFeatures}
6263
questions={props.questions}
6364
project={project}
65+
dirtyProject={dirtyProject}
6466
isRequired={props.required}
6567
/>
6668
)

src/projects/reducers/project.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { unflatten } from 'flat'
21
import {
32
LOAD_PROJECT_PENDING, LOAD_PROJECT_SUCCESS, LOAD_PROJECT_FAILURE, LOAD_DIRECT_PROJECT_SUCCESS,
43
CREATE_PROJECT_PENDING, CREATE_PROJECT_SUCCESS, CREATE_PROJECT_FAILURE, CLEAR_LOADED_PROJECT,
@@ -169,9 +168,16 @@ export const projectState = function (state=initialState, action) {
169168
})
170169
}
171170

172-
case PROJECT_DIRTY: {
171+
case PROJECT_DIRTY: {// payload contains only changed values from the project form
173172
return Object.assign({}, state, {
174-
project: _.merge({}, state.project, unflatten(action.payload), { isDirty : true})
173+
project: _.mergeWith({}, state.project, action.payload, { isDirty : true},
174+
// customizer to override screens array with changed values
175+
(objValue, srcValue, key) => {
176+
if (key === 'screens') {
177+
return srcValue// srcValue contains the changed values from action payload
178+
}
179+
}
180+
)
175181
})
176182
}
177183

0 commit comments

Comments
 (0)