@@ -2,11 +2,42 @@ import React, { Component, PropTypes } from 'react'
22import { withRouter } from 'react-router'
33import Modal from 'react-modal'
44import _ from 'lodash'
5+ import { unflatten } from 'flat'
56import update from 'react-addons-update'
67import FeaturePicker from './FeatureSelector/FeaturePicker'
78import { Formsy , Icons } from 'appirio-tech-react-components'
89
910import SpecSection from './SpecSection'
11+ import { HOC as hoc } from 'formsy-react'
12+
13+ const FeaturePickerModal = ( { project, isEdittable, showFeaturesDialog, hideFeaturesDialog, saveFeatures, setValue } ) => {
14+ const setFormValue = ( features , featureSeeAttached = false ) => {
15+ const featureObj = {
16+ value : features ,
17+ seeAttached : featureSeeAttached
18+ }
19+ setValue ( featureObj )
20+ saveFeatures ( features , featureSeeAttached )
21+ }
22+ return (
23+ < Modal
24+ isOpen = { showFeaturesDialog }
25+ className = "feature-selection-dialog"
26+ overlayClassName = "feature-selection-dialog-overlay"
27+ onRequestClose = { hideFeaturesDialog }
28+ >
29+ < FeaturePicker
30+ features = { _ . get ( project , 'details.appDefinition.features.value' , [ ] ) }
31+ isEdittable = { isEdittable } onSave = { setFormValue }
32+ />
33+ < div onClick = { hideFeaturesDialog } className = "feature-selection-dialog-close" >
34+ Save and close < Icons . XMarkIcon />
35+ </ div >
36+ </ Modal >
37+ )
38+ }
39+
40+ const FeaturePickerFormField = hoc ( FeaturePickerModal )
1041
1142class EditProjectForm extends Component {
1243
@@ -34,8 +65,15 @@ class EditProjectForm extends Component {
3465 }
3566
3667 componentWillReceiveProps ( nextProps ) {
37- // we receipt property updates from PROJECT_DIRTY REDUX state
38- if ( nextProps . project . isDirty ) return
68+ // we received property updates from PROJECT_DIRTY REDUX state
69+ if ( nextProps . project . isDirty ) {
70+ this . setState ( {
71+ // sets a new state variable with dirty project
72+ // any component who wants to listen for unsaved changes in project form can listen to this state variable
73+ dirtyProject : Object . assign ( { } , nextProps . project )
74+ } )
75+ return
76+ }
3977 let updatedProject = Object . assign ( { } , nextProps . project )
4078 if ( this . state . isFeaturesDirty && ! this . state . isSaving ) {
4179 updatedProject = update ( updatedProject , {
@@ -127,10 +165,9 @@ class EditProjectForm extends Component {
127165 }
128166
129167 submit ( model ) {
130- console . log ( 'submit' , this . isChanged ( ) )
131- if ( this . state . isFeaturesDirty ) {
132- model . details . appDefinition . features = this . state . project . details . appDefinition . features
133- }
168+ // if (this.state.isFeaturesDirty) {
169+ // model.details.appDefinition.features = this.state.project.details.appDefinition.features
170+ // }
134171 this . setState ( { isSaving : true } )
135172 this . props . submitHandler ( model )
136173 }
@@ -143,20 +180,21 @@ class EditProjectForm extends Component {
143180 */
144181 handleChange ( change ) {
145182 // removed check for isChanged argument to fire the PROJECT_DIRTY event for every change in the form
146- this . props . fireProjectDirty ( change )
183+ this . props . fireProjectDirty ( unflatten ( change ) )
147184 }
148185
149186
150187 render ( ) {
151188 const { isEdittable, sections } = this . props
152- const { project } = this . state
189+ const { project, dirtyProject } = this . state
153190 const renderSection = ( section , idx ) => {
154191 const anySectionInvalid = _ . some ( this . props . sections , ( s ) => s . isInvalid )
155192 return (
156193 < div key = { idx } >
157194 < SpecSection
158195 { ...section }
159196 project = { project }
197+ dirtyProject = { dirtyProject }
160198 sectionNumber = { idx + 1 }
161199 resetFeatures = { this . onFeaturesSaveAttachedClick }
162200 showFeaturesDialog = { this . showFeaturesDialog }
@@ -183,21 +221,17 @@ class EditProjectForm extends Component {
183221 onChange = { this . handleChange }
184222 >
185223 { sections . map ( renderSection ) }
186- </ Formsy . Form >
187- < Modal
188- isOpen = { this . state . showFeaturesDialog }
189- className = "feature-selection-dialog"
190- overlayClassName = "feature-selection-dialog-overlay"
191- onRequestClose = { this . hideFeaturesDialog }
192- >
193- < FeaturePicker
194- features = { _ . get ( project , 'details.appDefinition.features.value' , [ ] ) }
195- isEdittable = { isEdittable } onSave = { this . saveFeatures }
224+ < FeaturePickerFormField
225+ name = 'details.appDefinition.features'
226+ project = { project }
227+ isEdittable = { isEdittable }
228+ showFeaturesDialog = { this . state . showFeaturesDialog }
229+ hideFeaturesDialog = { this . hideFeaturesDialog }
230+ saveFeatures = { this . saveFeatures }
231+ value = { _ . get ( project , 'details.appDefinition.features' , { } ) }
196232 />
197- < div onClick = { this . hideFeaturesDialog } className = "feature-selection-dialog-close" >
198- Save and close < Icons . XMarkIcon />
199- </ div >
200- </ Modal >
233+ </ Formsy . Form >
234+
201235 </ div >
202236 )
203237 }
0 commit comments