Skip to content

Commit c71006c

Browse files
author
vikasrohit
authored
Merge pull request #1260 from appirio-tech/feature/optimize-incomplete-project-screen-behaviour
optimize incomplete project screen behaviour
2 parents f104bf8 + 16f6985 commit c71006c

File tree

2 files changed

+64
-39
lines changed

2 files changed

+64
-39
lines changed

src/projects/create/components/ProjectWizard.jsx

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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,10 +49,27 @@ class ProjectWizard extends Component {
4849
const incompleteProjectStr = window.localStorage.getItem(LS_INCOMPLETE_PROJECT)
4950
if(incompleteProjectStr) {
5051
const incompleteProject = JSON.parse(incompleteProjectStr)
52+
const incompleteProduct = _.get(incompleteProject, 'details.products[0]')
53+
let wizardStep = WZ_STEP_INCOMP_PROJ_CONF
54+
if (incompleteProduct && params && params.product) {
55+
// assumes the params.product to be id of a product because incomplete project is set only
56+
// after user selects a particular product
57+
const product = findProduct(params.product, true)
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+
}
67+
}
68+
}
5169
this.setState({
5270
project: update(this.state.project, {$merge : incompleteProject}),
5371
dirtyProject: update(this.state.dirtyProject, {$merge : incompleteProject}),
54-
wizardStep: WZ_STEP_INCOMP_PROJ_CONF,
72+
wizardStep: wizardStep,
5573
isProjectDirty: false
5674
}, () => {
5775
typeof onStepChange === 'function' && onStepChange(this.state.wizardStep)
@@ -61,20 +79,7 @@ class ProjectWizard extends Component {
6179
const updateQuery = {}
6280
let wizardStep = WZ_STEP_SELECT_PROJ_TYPE
6381
if (params && params.product) {
64-
// first try the path param to be a project category
65-
let projectType = findCategory(params.product, true)
66-
if (projectType) {// if its a category
67-
updateQuery['type'] = { $set : projectType.id }
68-
wizardStep = WZ_STEP_SELECT_PROD_TYPE
69-
} else {
70-
// if it is not a category, it should be a product and we should be able to find a category for it
71-
projectType = findProductCategory(params.product, true)
72-
// finds product object from product alias
73-
const product = findProduct(params.product, true)
74-
updateQuery['type'] = { $set : projectType.id }
75-
updateQuery['details'] = { products : { $set: [product.id] } }
76-
wizardStep = WZ_STEP_FILL_PROJ_DETAILS
77-
}
82+
wizardStep = this.loadProjectAndProductFromURL(params.product, updateQuery)
7883
}
7984
// retrieve refCode from query param
8085
// TODO give warning after truncating
@@ -106,22 +111,7 @@ class ProjectWizard extends Component {
106111
let wizardStep = type && product ? WZ_STEP_FILL_PROJ_DETAILS : null
107112
const updateQuery = {}
108113
if (params && params.product) { // if there exists product path param
109-
// first try the path param to be a project category
110-
let projectType = findCategory(params.product, true)
111-
if (projectType) {// if its a category
112-
updateQuery['type'] = { $set : projectType.id }
113-
wizardStep = WZ_STEP_SELECT_PROD_TYPE
114-
} else {
115-
// if it is not a category, it should be a product and we should be able to find a category for it
116-
projectType = findProductCategory(params.product, true)
117-
// finds product object from product alias
118-
const product = findProduct(params.product, true)
119-
if (projectType) {// we can have `incomplete` as params.product
120-
updateQuery['type'] = { $set : projectType.id }
121-
updateQuery['details'] = { products : { $set: [product.id] } }
122-
wizardStep = WZ_STEP_FILL_PROJ_DETAILS
123-
}
124-
}
114+
wizardStep = this.loadProjectAndProductFromURL(params.product, updateQuery)
125115
} else { // if there is not product path param, it should be first step of the wizard
126116
updateQuery['type'] = { $set : null }
127117
updateQuery['details'] = { products : { $set: [] } }
@@ -139,6 +129,33 @@ class ProjectWizard extends Component {
139129
}
140130
}
141131

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+
142159
/**
143160
* Loads incomplete project from the local storage and populates the state from that project.
144161
* It also moves the wizard to the project details step if there exists an incomplete project.
@@ -163,14 +180,22 @@ class ProjectWizard extends Component {
163180
* Removed incomplete project from the local storage and resets the state. Also, moves wizard to the first step.
164181
*/
165182
removeIncompleteProject() {
166-
const { onStepChange } = this.props
183+
const { onStepChange, onProjectUpdate } = this.props
184+
// remove incomplete project from local storage
167185
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+
}
168194
this.setState({
169-
project: { details: {} },
170-
dirtyProject: { details: {} },
171-
wizardStep: WZ_STEP_SELECT_PROJ_TYPE
195+
wizardStep: wizardStep
172196
}, () => {
173-
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)
174199
})
175200
}
176201

src/projects/create/containers/CreateContainer.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ class CreateConainer extends React.Component {
108108

109109
// stores the incomplete project in local storage
110110
onLeave(e) {// eslint-disable-line no-unused-vars
111-
const { wizardStep } = this.state
112-
if (wizardStep === ProjectWizard.Steps.WZ_STEP_FILL_PROJ_DETAILS) {// Project Details step
113-
console.log('saving incomplete project')
111+
const { wizardStep, isProjectDirty } = this.state
112+
if (wizardStep === ProjectWizard.Steps.WZ_STEP_FILL_PROJ_DETAILS && isProjectDirty) {// Project Details step
113+
console.log('saving incomplete project', this.state.updatedProject)
114114
window.localStorage.setItem(LS_INCOMPLETE_PROJECT, JSON.stringify(this.state.updatedProject))
115115
}
116116
// commenting alerts for the page unload and route change hooks as discussed

0 commit comments

Comments
 (0)