11import React from 'react'
22import { connect } from 'react-redux'
3+ import _ from 'lodash'
34import FormsyForm from 'appirio-tech-react-components/components/Formsy'
45const Formsy = FormsyForm . Formsy
5- import RadioGroup from 'appirio-tech-react-components/components/Formsy/RadioGroup'
6- import SpecQuestionList from '../SpecQuestionList/SpecQuestionList'
7- import Accordion from '../Accordion/Accordion'
86import { updateProject } from '../../../actions/project'
9- import { DEFAULT_NDA_UUID } from '../../../../config/constants'
7+ import NDAField from '../NDAField'
8+ import GroupsField from '../GroupsField'
109
1110import './EditProjectDefaultsForm.scss'
1211
@@ -15,7 +14,6 @@ class EditProjectDefaultsForm extends React.Component {
1514 super ( props )
1615
1716 this . state = {
18- hasNda : false ,
1917 enableButton : false ,
2018 isLoading : true
2119 }
@@ -25,64 +23,46 @@ class EditProjectDefaultsForm extends React.Component {
2523 }
2624
2725 componentDidMount ( ) {
28- const { terms} = this . props . project
29- if ( terms . indexOf ( DEFAULT_NDA_UUID ) >= 0 ) {
30- this . setState ( { hasNda : true } )
26+ this . setState ( { isLoading : false , project : this . props . project } )
27+ }
28+
29+ componentDidUpdate ( prevProps ) {
30+ if ( ! _ . isEqual ( prevProps . project , this . props . project ) ) {
31+ this . setState ( { project : this . props . project } )
3132 }
32- this . setState ( { isLoading : false } )
3333 }
3434
35- handleChange ( { nda} ) {
36- if ( ( nda === 'yes' ) !== this . state . hasNda ) {
37- if ( this . state . enableButton !== true ) {
38- this . setState ( { enableButton : true } )
39- }
40- } else {
41- if ( this . state . enableButton !== false ) {
42- this . setState ( { enableButton : false } )
43- }
35+ handleChange ( changed ) {
36+ const keys = _ . intersection ( Object . keys ( changed ) , Object . keys ( this . state . project ) )
37+ const reqProjectState = keys . reduce ( ( acc , curr ) => {
38+ acc [ curr ] = changed [ curr ]
39+ return acc
40+ } , { } )
41+ const project = _ . assign ( { } , this . state . project , reqProjectState )
42+ this . setState ( { project} )
43+ const isProjectEqual = _ . isEqual ( this . state . project , this . props . project )
44+ if ( ! isProjectEqual && ! this . state . enableButton ) {
45+ this . setState ( { enableButton : true } )
46+ } else if ( isProjectEqual && this . state . enableButton !== false ) {
47+ this . setState ( { enableButton : false } )
4448 }
4549 }
4650
4751 handleSubmit ( ) {
4852 const { updateProject} = this . props
49- const { id, terms} = this . props . project
50- const newHasNda = ! this . state . hasNda
51- if ( newHasNda ) {
52- updateProject ( id , {
53- terms : [ ...new Set ( [ ...terms , DEFAULT_NDA_UUID ] ) ]
54- } , true ) . then ( ( ) => {
55- this . setState ( {
56- hasNda : this . props . project . terms . indexOf ( DEFAULT_NDA_UUID ) >= 0
57- } )
58- } )
59- } else {
60- const newTerms = [ ...terms ]
61- if ( newTerms . indexOf ( DEFAULT_NDA_UUID ) >= 0 ) {
62- newTerms . splice ( newTerms . indexOf ( DEFAULT_NDA_UUID ) , 1 )
63- updateProject ( id , {
64- terms : newTerms
65- } , true ) . then ( ( ) => {
66- this . setState ( {
67- hasNda : this . props . project . terms . indexOf ( DEFAULT_NDA_UUID ) >= 0
68- } )
69- } )
70- }
71- }
53+ const { id} = this . props . project
54+ const updateProjectObj = Object . keys ( this . state . project )
55+ . filter ( key => ! _ . isEqual ( this . props . project [ key ] , this . state . project [ key ] ) )
56+ . reduce ( ( acc , curr ) => {
57+ acc [ curr ] = this . state . project [ curr ]
58+ return acc
59+ } , { } )
60+ updateProject ( id , updateProjectObj )
61+ . then ( ( ) => this . setState ( { enableButton : false } ) )
62+ . catch ( console . error )
7263 }
7364
7465 render ( ) {
75- const opts = [
76- {
77- value : 'yes' ,
78- label : 'Yes'
79- } ,
80- {
81- value : 'no' ,
82- label : 'No'
83- }
84- ]
85-
8666 if ( this . state . isLoading ) return null
8767
8868 return (
@@ -92,21 +72,14 @@ class EditProjectDefaultsForm extends React.Component {
9272 onChange = { this . handleChange }
9373 >
9474 < div className = "container" >
95- < SpecQuestionList >
96- < Accordion
97- title = "Enforce Topcoder NDA"
98- type = "radio-group"
99- options = { opts }
100- >
101- < SpecQuestionList . Item >
102- < RadioGroup
103- name = "nda"
104- options = { opts }
105- value = { this . state . hasNda ? 'yes' : 'no' }
106- />
107- </ SpecQuestionList . Item >
108- </ Accordion >
109- </ SpecQuestionList >
75+ < NDAField
76+ name = "terms"
77+ value = { this . state . project . terms }
78+ />
79+ < GroupsField
80+ name = "groups"
81+ value = { this . state . project . groups }
82+ />
11083 </ div >
11184 < div className = "section-footer section-footer-spec" >
11285 < button
0 commit comments