@@ -40,6 +40,7 @@ class ProjectWizard extends Component {
4040 this . handleStepChange = this . handleStepChange . bind ( this )
4141 this . restoreCommonDetails = this . restoreCommonDetails . bind ( this )
4242 this . handleWizardCancel = this . handleWizardCancel . bind ( this )
43+ this . loadProjectAndProductFromURL = this . loadProjectAndProductFromURL . bind ( this )
4344 }
4445
4546 componentDidMount ( ) {
@@ -48,16 +49,21 @@ class ProjectWizard extends Component {
4849 const incompleteProjectStr = window . localStorage . getItem ( LS_INCOMPLETE_PROJECT )
4950 if ( incompleteProjectStr ) {
5051 const incompleteProject = JSON . parse ( incompleteProjectStr )
51- // const incompleteProjectType = _.get(incompleteProject, 'type')
5252 const incompleteProduct = _ . get ( incompleteProject , 'details.products[0]' )
5353 let wizardStep = WZ_STEP_INCOMP_PROJ_CONF
5454 if ( incompleteProduct && params && params . product ) {
5555 // assumes the params.product to be id of a product because incomplete project is set only
5656 // after user selects a particular product
5757 const product = findProduct ( params . product , true )
58- // load project detais page directly if product of save project and deep link are the same
59- if ( product && product . id === incompleteProduct ) {
60- wizardStep = WZ_STEP_FILL_PROJ_DETAILS
58+ if ( product ) {
59+ // load project detais page directly if product of save project and deep link are the same
60+ if ( product . id === incompleteProduct ) {
61+ wizardStep = WZ_STEP_FILL_PROJ_DETAILS
62+ } else { // for different product type, update local state with URL params
63+ const projectType = findProductCategory ( params . product )
64+ _ . set ( incompleteProject , 'type' , projectType . id )
65+ _ . set ( incompleteProject , 'details.products[0]' , product . id )
66+ }
6167 }
6268 }
6369 this . setState ( {
@@ -73,20 +79,7 @@ class ProjectWizard extends Component {
7379 const updateQuery = { }
7480 let wizardStep = WZ_STEP_SELECT_PROJ_TYPE
7581 if ( params && params . product ) {
76- // first try the path param to be a project category
77- let projectType = findCategory ( params . product , true )
78- if ( projectType ) { // if its a category
79- updateQuery [ 'type' ] = { $set : projectType . id }
80- wizardStep = WZ_STEP_SELECT_PROD_TYPE
81- } else {
82- // if it is not a category, it should be a product and we should be able to find a category for it
83- projectType = findProductCategory ( params . product , true )
84- // finds product object from product alias
85- const product = findProduct ( params . product , true )
86- updateQuery [ 'type' ] = { $set : projectType . id }
87- updateQuery [ 'details' ] = { products : { $set : [ product . id ] } }
88- wizardStep = WZ_STEP_FILL_PROJ_DETAILS
89- }
82+ wizardStep = this . loadProjectAndProductFromURL ( params . product , updateQuery )
9083 }
9184 // retrieve refCode from query param
9285 // TODO give warning after truncating
@@ -118,22 +111,7 @@ class ProjectWizard extends Component {
118111 let wizardStep = type && product ? WZ_STEP_FILL_PROJ_DETAILS : null
119112 const updateQuery = { }
120113 if ( params && params . product ) { // if there exists product path param
121- // first try the path param to be a project category
122- let projectType = findCategory ( params . product , true )
123- if ( projectType ) { // if its a category
124- updateQuery [ 'type' ] = { $set : projectType . id }
125- wizardStep = WZ_STEP_SELECT_PROD_TYPE
126- } else {
127- // if it is not a category, it should be a product and we should be able to find a category for it
128- projectType = findProductCategory ( params . product , true )
129- // finds product object from product alias
130- const product = findProduct ( params . product , true )
131- if ( projectType ) { // we can have `incomplete` as params.product
132- updateQuery [ 'type' ] = { $set : projectType . id }
133- updateQuery [ 'details' ] = { products : { $set : [ product . id ] } }
134- wizardStep = WZ_STEP_FILL_PROJ_DETAILS
135- }
136- }
114+ wizardStep = this . loadProjectAndProductFromURL ( params . product , updateQuery )
137115 } else { // if there is not product path param, it should be first step of the wizard
138116 updateQuery [ 'type' ] = { $set : null }
139117 updateQuery [ 'details' ] = { products : { $set : [ ] } }
@@ -151,6 +129,33 @@ class ProjectWizard extends Component {
151129 }
152130 }
153131
132+ /**
133+ * Loads project type and product from the given URL parameter.
134+ *
135+ * @param {string } urlParam URL parameter value which represents either project type or product
136+ * @param {object } updateQuery query object which would be updated according to parsed project type and product
137+ *
138+ * @return {number } step where wizard should move after parsing the URL param
139+ */
140+ loadProjectAndProductFromURL ( urlParam , updateQuery ) {
141+ // first try the path param to be a project category
142+ let projectType = findCategory ( urlParam , true )
143+ if ( projectType ) { // if its a category
144+ updateQuery [ 'type' ] = { $set : projectType . id }
145+ return WZ_STEP_SELECT_PROD_TYPE
146+ } else {
147+ // if it is not a category, it should be a product and we should be able to find a category for it
148+ projectType = findProductCategory ( urlParam , true )
149+ // finds product object from product alias
150+ const product = findProduct ( urlParam , true )
151+ if ( projectType ) { // we can have `incomplete` as params.product
152+ updateQuery [ 'type' ] = { $set : projectType . id }
153+ updateQuery [ 'details' ] = { products : { $set : [ product . id ] } }
154+ return WZ_STEP_FILL_PROJ_DETAILS
155+ }
156+ }
157+ }
158+
154159 /**
155160 * Loads incomplete project from the local storage and populates the state from that project.
156161 * It also moves the wizard to the project details step if there exists an incomplete project.
@@ -175,14 +180,22 @@ class ProjectWizard extends Component {
175180 * Removed incomplete project from the local storage and resets the state. Also, moves wizard to the first step.
176181 */
177182 removeIncompleteProject ( ) {
178- const { onStepChange } = this . props
183+ const { onStepChange, params, onProjectUpdate } = this . props
184+ // remove incomplete project from local storage
179185 window . localStorage . removeItem ( LS_INCOMPLETE_PROJECT )
186+ const projectType = _ . get ( this . state . project , 'type' )
187+ const product = _ . get ( this . state . project , 'details.products[0]' )
188+ let wizardStep = WZ_STEP_SELECT_PROJ_TYPE
189+ if ( product ) {
190+ wizardStep = WZ_STEP_FILL_PROJ_DETAILS
191+ } else if ( projectType ) {
192+ wizardStep = WZ_STEP_SELECT_PROD_TYPE
193+ }
180194 this . setState ( {
181- project : { details : { } } ,
182- dirtyProject : { details : { } } ,
183- wizardStep : WZ_STEP_SELECT_PROJ_TYPE
195+ wizardStep : wizardStep
184196 } , ( ) => {
185- typeof onStepChange === 'function' && onStepChange ( this . state . wizardStep )
197+ typeof onProjectUpdate === 'function' && onProjectUpdate ( this . state . dirtyProject , true )
198+ typeof onStepChange === 'function' && onStepChange ( this . state . wizardStep , this . state . project )
186199 } )
187200 }
188201
0 commit comments