@@ -4,8 +4,7 @@ import qs from 'query-string'
44import React , { Component } from 'react'
55import PropTypes from 'prop-types'
66import { withRouter } from 'react-router-dom'
7- import { getProjectCreationTemplateField , getProjectTemplateByAlias ,
8- getProjectTemplateByKey , getProjectTemplatesByCategory , getProjectTypeByAlias } from '../../../helpers/templates'
7+ import { getProjectCreationTemplateField , getProjectTemplateByAlias , getProjectTemplatesByCategory , getProjectTypeByAlias } from '../../../helpers/templates'
98import Wizard from '../../../components/Wizard'
109import SelectProjectTemplate from './SelectProjectTemplate'
1110import SelectProjectType from './SelectProjectType'
@@ -53,14 +52,15 @@ class ProjectWizard extends Component {
5352 const incompleteProjectStr = window . localStorage . getItem ( LS_INCOMPLETE_PROJECT )
5453 if ( incompleteProjectStr ) {
5554 const incompleteProject = JSON . parse ( incompleteProjectStr )
56- const incompleteProjectTemplateKey = _ . get ( incompleteProject , 'details.products[0]' )
55+ const incompleteProjectTemplateId = _ . get ( incompleteProject , 'templateId' )
56+ const incompleteProjectTemplate = _ . find ( projectTemplates , pt => pt . id === incompleteProjectTemplateId )
5757 let wizardStep = WZ_STEP_INCOMP_PROJ_CONF
5858 let updateQuery = { }
59- if ( incompleteProjectTemplateKey && params && params . project ) {
60- const project = getProjectTemplateByAlias ( projectTemplates , params . project )
61- if ( project ) {
59+ if ( incompleteProjectTemplate && params && params . project ) {
60+ const projectTemplate = getProjectTemplateByAlias ( projectTemplates , params . project )
61+ if ( projectTemplate ) {
6262 // load project details page directly
63- if ( project . key === incompleteProjectTemplateKey ) {
63+ if ( projectTemplate . key === incompleteProjectTemplate . key ) {
6464 wizardStep = WZ_STEP_FILL_PROJ_DETAILS
6565 updateQuery = { $merge : incompleteProject }
6666 } else {
@@ -111,15 +111,15 @@ class ProjectWizard extends Component {
111111 const { onStepChange } = nextProps
112112 const params = nextProps . match . params
113113 const type = _ . get ( nextProps . project , 'type' , null )
114- const projectTemplateKey = _ . get ( nextProps . project , 'details.products[0] ' , null )
114+ const projectTemplateId = _ . get ( nextProps . project , 'templateId ' , null )
115115 // redirect user to project details form, if we already have type and project available
116- let wizardStep = type && projectTemplateKey ? WZ_STEP_FILL_PROJ_DETAILS : null
116+ let wizardStep = type && projectTemplateId ? WZ_STEP_FILL_PROJ_DETAILS : null
117117 const updateQuery = { }
118118 if ( params && params . project ) { // if there exists project path param
119119 wizardStep = this . loadProjectFromURL ( params , updateQuery )
120120 } else { // if there is not project path param, it should be first step of the wizard
121121 updateQuery [ 'type' ] = { $set : null }
122- updateQuery [ 'details' ] = { products : { $set : [ ] } }
122+ updateQuery [ 'details' ] = { $set : { } }
123123 wizardStep = WZ_STEP_SELECT_PROJ_TYPE
124124 }
125125 // if wizard step deduced above and stored in state are not the same, update the state
@@ -166,7 +166,8 @@ class ProjectWizard extends Component {
166166 // show details step
167167 if ( projectTemplate ) {
168168 updateQuery [ 'type' ] = { $set : projectTemplate . category }
169- updateQuery [ 'details' ] = { products : { $set : [ projectTemplate . key ] } }
169+ updateQuery [ 'templateId' ] = { $set : projectTemplate . id }
170+ updateQuery [ 'details' ] = { }
170171
171172 const refCode = _ . get ( qs . parse ( window . location . search ) , 'refCode' , '' ) . trim ( ) . substr ( 0 , PROJECT_REF_CODE_MAX_LENGTH )
172173 if ( refCode ) {
@@ -211,11 +212,11 @@ class ProjectWizard extends Component {
211212 window . localStorage . removeItem ( LS_INCOMPLETE_PROJECT )
212213 // following code assumes that componentDidMount has already updated state with correct project
213214 const projectType = _ . get ( this . state . project , 'type' )
214- const projectTemplateKey = _ . get ( this . state . project , 'details.products[0] ' )
215+ const projectTemplateId = _ . get ( this . state . project , 'templateId ' )
215216 let wizardStep = WZ_STEP_SELECT_PROJ_TYPE
216217 let project = null
217- if ( projectTemplateKey ) {
218- project = { type : projectType , details : { products : [ projectTemplateKey ] } }
218+ if ( projectTemplateId ) {
219+ project = { type : projectType , templateId : projectTemplateId }
219220 wizardStep = WZ_STEP_FILL_PROJ_DETAILS
220221 }
221222 const refCode = this . getRefCodeFromURL ( )
@@ -243,18 +244,13 @@ class ProjectWizard extends Component {
243244 } )
244245 }
245246
246- updateProjectTemplate ( projectTemplateKey ) {
247+ updateProjectTemplate ( projectTemplate ) {
247248 window . scrollTo ( 0 , 0 )
248- const { onStepChange, onProjectUpdate, projectTemplates } = this . props
249+ const { onStepChange, onProjectUpdate } = this . props
249250 const updateQuery = { }
250- if ( projectTemplateKey ) {
251- const projectTemplate = getProjectTemplateByKey ( projectTemplates , projectTemplateKey )
252-
253- const detailsQuery = { products : [ projectTemplateKey ] }
254- this . restoreCommonDetails ( projectTemplate , updateQuery , detailsQuery )
255-
251+ if ( projectTemplate ) {
256252 updateQuery . type = { $set : projectTemplate . category }
257- updateQuery . details = { $set : detailsQuery }
253+ updateQuery . templateId = { $set : projectTemplate . id }
258254 }
259255 this . setState ( {
260256 project : update ( this . state . project , updateQuery ) ,
@@ -275,8 +271,9 @@ class ProjectWizard extends Component {
275271 if ( projectType ) {
276272 updateQuery . type = { $set : projectType }
277273
274+ // sets the appropriate project template if project category has only one project template
278275 if ( visibleProjectTemplates . length === 1 ) {
279- updateQuery . details = { $set : { products : [ visibleProjectTemplates [ 0 ] . key ] } }
276+ updateQuery . templateId = { $set : visibleProjectTemplates [ 0 ] . id }
280277 }
281278 }
282279
@@ -392,8 +389,8 @@ class ProjectWizard extends Component {
392389 const type = wizardStep === WZ_STEP_SELECT_PROJ_TEMPLATE ? this . state . project . type : null
393390 this . setState ( {
394391 // resets project sub type or product
395- project : update ( this . state . project , { type : { $set : type } , details : { products : { $set : [ ] } } } ) ,
396- dirtyProject : update ( this . state . project , { type : { $set : type } , details : { products : { $set : [ ] } } } ) ,
392+ project : update ( this . state . project , { type : { $set : type } , details : { } } ) ,
393+ dirtyProject : update ( this . state . project , { type : { $set : type } , details : { } } ) ,
397394 wizardStep
398395 } , ( ) => {
399396 typeof onStepChange === 'function' && onStepChange ( wizardStep , this . state . dirtyProject )
0 commit comments