Skip to content

Commit 55cf3ee

Browse files
author
Vikas Agarwal
committed
Optimized ProjectStatus component and its usage so that:
1. It does not load ProjectStatus in dropdown enhanced mode which prevent attaching unnecessary dropdown events 2. it does not fire onClickOutside method always even if ProjecStatus is editable and rendered as dropdown. Now it fires only when dropdown is open.
1 parent dcf4580 commit 55cf3ee

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

src/components/ProjectStatus/ProjectStatus.jsx

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ export const enhanceDropdown = (CompositeComponent) => class extends Component {
1111
this.onSelect = this.onSelect.bind(this)
1212
this.onClickOutside = this.onClickOutside.bind(this)
1313
this.onClickOtherDropdown = this.onClickOtherDropdown.bind(this)
14+
this.refreshEventHandlers = this.refreshEventHandlers.bind(this)
15+
}
16+
17+
refreshEventHandlers() {
18+
if (this.state.isOpen) {
19+
document.addEventListener('click', this.onClickOutside)
20+
document.addEventListener('dropdownClicked', this.onClickOtherDropdown)
21+
} else {
22+
document.removeEventListener('click', this.onClickOutside)
23+
document.removeEventListener('dropdownClicked', this.onClickOtherDropdown)
24+
}
1425
}
1526

1627
handleClick() {
@@ -19,7 +30,9 @@ export const enhanceDropdown = (CompositeComponent) => class extends Component {
1930

2031
document.dispatchEvent(dropdownClicked)
2132

22-
this.setState({ isOpen: !this.state.isOpen })
33+
this.setState({ isOpen: !this.state.isOpen }, ()=> {
34+
this.refreshEventHandlers()
35+
})
2336
}
2437

2538
onSelect(value) {
@@ -47,20 +60,26 @@ export const enhanceDropdown = (CompositeComponent) => class extends Component {
4760
} while (currNode.tagName)
4861

4962
if (!isDropdown) {
50-
this.setState({ isOpen: false })
63+
this.setState({ isOpen: false }, () => {
64+
this.refreshEventHandlers()
65+
})
5166
}
5267
}
5368

5469
onClickOtherDropdown() {
55-
this.setState({ isOpen: false })
70+
this.setState({ isOpen: false }, () => {
71+
this.refreshEventHandlers()
72+
})
5673
}
5774

5875
componentDidMount() {
5976
document.removeEventListener('click', this.onClickOutside)
6077
document.removeEventListener('dropdownClicked', this.onClickOtherDropdown)
6178

62-
document.addEventListener('click', this.onClickOutside)
63-
document.addEventListener('dropdownClicked', this.onClickOtherDropdown)
79+
if (this.state.isOpen) {
80+
document.addEventListener('click', this.onClickOutside)
81+
document.addEventListener('dropdownClicked', this.onClickOtherDropdown)
82+
}
6483
}
6584

6685
componentWillUnmount() {
@@ -126,5 +145,5 @@ ProjectStatus.propTypes = {
126145
ProjectStatus.defaultProps = {
127146
}
128147

129-
export default enhanceDropdown(ProjectStatus)
148+
export default ProjectStatus
130149

src/components/ProjectStatus/editableProjectStatus.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component} from 'react'
22
import ProjectStatusChangeConfirmation from './ProjectStatusChangeConfirmation'
33
import cn from 'classnames'
44
import _ from 'lodash'
5+
import { enhanceDropdown} from './ProjectStatus'
56
import {
67
PROJECT_STATUS_COMPLETED,
78
PROJECT_STATUS_CANCELLED
@@ -51,10 +52,11 @@ const editableProjectStatus = (CompositeComponent) => class extends Component {
5152

5253
render() {
5354
const { showStatusChangeDialog, newStatus, statusChangeReason } = this.state
55+
const EnhancedProjectStatus = enhanceDropdown(CompositeComponent)
5456
return (
5557
<div className={cn('panel', {'modal-active': showStatusChangeDialog})}>
5658
<div className="modal-overlay"></div>
57-
<CompositeComponent
59+
<EnhancedProjectStatus
5860
{ ...this.props }
5961
onSelect={ this.showStatusChangeDialog }
6062
/>

src/projects/list/components/Projects/ProjectCardBody.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import { PROJECT_STATUS_ACTIVE, PROJECT_ROLE_COPILOT } from '../../../../config/
88
import './ProjectCardBody.scss'
99
import _ from 'lodash'
1010

11-
const EnhancedProjectStatus = editableProjectStatus(ProjectStatus)
12-
1311
function ProjectCardBody({ project, duration, currentMemberRole, descLinesCount = 8,
1412
onChangeStatus, isManager }) {
1513
if (!project) return null
@@ -19,6 +17,8 @@ function ProjectCardBody({ project, duration, currentMemberRole, descLinesCount
1917

2018
const progress = _.get(process, 'percent', 0)
2119

20+
const EnhancedProjectStatus = canEdit ? editableProjectStatus(ProjectStatus) : ProjectStatus
21+
2222
return (
2323
<div className="project-card-body">
2424
<TextTruncate

src/projects/list/components/Projects/ProjectCardBody.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
display: flex;
2222
justify-content: center;
2323
margin-top: 20px;
24+
padding: 10px 0;
25+
26+
.panel { // avoid double padding for editable project status
27+
padding: 0;
28+
}
2429

2530
.status-header {
2631
.status-label {

0 commit comments

Comments
 (0)