Skip to content

Commit c84b030

Browse files
author
vikasrohit
authored
Merge pull request #863 from appirio-tech/feature/number_of_screens_valiation_802
Github issue#802, can add more screen
2 parents 5efbe9e + 380ba68 commit c84b030

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, {
@@ -143,20 +151,21 @@ class EditProjectForm extends Component {
143151
*/
144152
handleChange(change) {
145153
// removed check for isChanged argument to fire the PROJECT_DIRTY event for every change in the form
146-
this.props.fireProjectDirty(change)
154+
this.props.fireProjectDirty(unflatten(change))
147155
}
148156

149157

150158
render() {
151159
const { isEdittable, sections } = this.props
152-
const { project } = this.state
160+
const { project, dirtyProject } = this.state
153161
const renderSection = (section, idx) => {
154162
const anySectionInvalid = _.some(this.props.sections, (s) => s.isInvalid)
155163
return (
156164
<div key={idx}>
157165
<SpecSection
158166
{...section}
159167
project={project}
168+
dirtyProject={dirtyProject}
160169
sectionNumber={idx + 1}
161170
resetFeatures={this.onFeaturesSaveAttachedClick}
162171
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,
@@ -193,9 +192,16 @@ export const projectState = function (state=initialState, action) {
193192
})
194193
}
195194

196-
case PROJECT_DIRTY: {
195+
case PROJECT_DIRTY: {// payload contains only changed values from the project form
197196
return Object.assign({}, state, {
198-
project: _.merge({}, state.project, unflatten(action.payload), { isDirty : true})
197+
project: _.mergeWith({}, state.project, action.payload, { isDirty : true},
198+
// customizer to override screens array with changed values
199+
(objValue, srcValue, key) => {
200+
if (key === 'screens') {
201+
return srcValue// srcValue contains the changed values from action payload
202+
}
203+
}
204+
)
199205
})
200206
}
201207

0 commit comments

Comments
 (0)