Skip to content

Commit bbceb4d

Browse files
authored
Replace many lodash _.get() calls with native ES expressions (#2515)
1 parent 341ad47 commit bbceb4d

File tree

220 files changed

+1254
-1551
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

220 files changed

+1254
-1551
lines changed

src/components/ActivityListing/ActivityDescription.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import classNames from 'classnames'
44
import { FormattedMessage } from 'react-intl'
55
import { Link } from 'react-router-dom'
66
import _isFinite from 'lodash/isFinite'
7-
import _get from 'lodash/get'
87
import { ActivityItemType, messagesByType }
98
from '../../services/Activity/ActivityItemTypes/ActivityItemTypes'
109
import { ActivityActionType, messagesByAction }
@@ -49,7 +48,7 @@ export const ActivityDescription = props => {
4948
{challengeName}
5049
</Link>
5150
</div>
52-
{_get(props.entry, 'challenge.general.parent.id') &&
51+
{(props.entry?.challenge?.general?.parent?.id) &&
5352
<div className="mr-break-words mr-links-grey-light mr-mb-4">
5453
<Link
5554
to={`/browse/projects/${props.entry.challenge.general.parent.id}`}

src/components/ActivityMap/ActivityMap.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { getCoord } from '@turf/invariant'
55
import centroid from '@turf/centroid'
66
import { differenceInHours, parseISO } from 'date-fns'
77
import _isString from 'lodash/isString'
8-
import _get from 'lodash/get'
98
import _map from 'lodash/map'
109
import { latLng } from 'leaflet'
1110
import { toLatLngBounds, GLOBAL_MAPBOUNDS } from '../../services/MapBounds/MapBounds'
@@ -26,7 +25,7 @@ const VisibleTileLayer = WithVisibleLayer(SourcedTileLayer)
2625
* @author [Neil Rotstan](https://github.com/nrotstan)
2726
*/
2827
export const ActivityMap = props => {
29-
const hasTaskMarkers = _get(props, 'activity.length', 0) > 0
28+
const hasTaskMarkers = (props.activity?.length ?? 0) > 0
3029
let coloredMarkers = null
3130
if (hasTaskMarkers) {
3231
coloredMarkers = _map(props.activity, entry => {
@@ -63,7 +62,7 @@ export const ActivityMap = props => {
6362
}
6463

6564
const overlayLayers = buildLayerSources(
66-
props.visibleOverlays, _get(props, 'user.settings.customBasemaps'),
65+
props.visibleOverlays, props.user?.settings?.customBasemaps,
6766
(layerId, index, layerSource) =>
6867
<SourcedTileLayer key={layerId} source={layerSource} zIndex={index + 2} />
6968
)

src/components/AdminPane/HOCs/WithChallengeManagement/WithChallengeManagement.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { connect } from "react-redux";
22
import _map from "lodash/map";
3-
import _get from "lodash/get";
43
import _isObject from "lodash/isObject";
54
import _compact from "lodash/compact";
65
import bundleByTaskBundleId from "../../../../utils/bundleByTaskBundleId";
@@ -192,7 +191,7 @@ async function uploadLineByLine(
192191
* component
193192
*/
194193
async function deleteIncompleteTasks(dispatch, ownProps, challenge) {
195-
const estimatedToDelete = _get(challenge, "actions.available");
194+
const estimatedToDelete = challenge?.actions?.available;
196195
let latestAvailable = estimatedToDelete;
197196

198197
ownProps.updateDeletingTasksProgress(true, 0);
@@ -217,16 +216,9 @@ async function deleteIncompleteTasks(dispatch, ownProps, challenge) {
217216
challenge.id,
218217
true
219218
)(dispatch).then((challengeResult) => {
220-
const available = _get(
221-
actionsResult,
222-
`entities.challenges.${challenge.id}.actions.available`,
223-
latestAvailable
224-
);
219+
const available = actionsResult?.entities?.challenges?.[challenge.id]?.actions?.available ?? latestAvailable;
225220

226-
const status = _get(
227-
challengeResult,
228-
`entities.challenges.${challenge.id}.status`
229-
);
221+
const status = challengeResult?.entities?.challenges?.[challenge.id]?.status;
230222

231223
if (
232224
available >= latestAvailable &&

src/components/AdminPane/HOCs/WithChallengeMetrics/WithChallengeMetrics.jsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component } from 'react'
22
import { injectIntl } from 'react-intl'
3-
import _get from 'lodash/get'
43
import _map from 'lodash/map'
54
import _toPairs from 'lodash/toPairs'
65
import _groupBy from 'lodash/groupBy'
@@ -36,12 +35,12 @@ const WithChallengeMetrics = function(WrappedComponent, applyFilters = false) {
3635
}
3736

3837
updateMetrics(props) {
39-
const challengeId =_get(props.challenge, 'id')
38+
const challengeId =props.challenge?.id
4039

4140
if (challengeId && props.fetchChallengeActions) {
4241
this.setState({loading: true})
43-
const criteria = {filters: _get(props.searchFilters, 'filters')}
44-
criteria.invertFields = _get(props.searchCriteria, 'filters.invertFields')
42+
const criteria = {filters: props.searchFilters?.filters}
43+
criteria.invertFields = props.searchCriteria?.filters?.invertFields
4544

4645
if (props.includeTaskStatuses && this.isFiltering(props.includeTaskStatuses)) {
4746
criteria.status = _keys(_pickBy(props.includeTaskStatuses, v => v)).join(',')
@@ -59,9 +58,9 @@ const WithChallengeMetrics = function(WrappedComponent, applyFilters = false) {
5958
props.fetchChallengeActions(challengeId, true, criteria).then((normalizedResults) => {
6059
let fetchedMetrics = null
6160
let fetchedPriorityMetrics = null
62-
if (_get(normalizedResults, `entities.challenges.${challengeId}`)) {
63-
fetchedMetrics = _get(normalizedResults, 'entities.challenges')[challengeId].actions
64-
fetchedPriorityMetrics = _get(normalizedResults, 'entities.challenges')[challengeId].priorityActions
61+
if (normalizedResults?.entities?.challenges?.[challengeId]) {
62+
fetchedMetrics = (normalizedResults?.entities?.challenges)[challengeId].actions
63+
fetchedPriorityMetrics = (normalizedResults?.entities?.challenges)[challengeId].priorityActions
6564
}
6665

6766
this.setState({loading: false,
@@ -80,10 +79,10 @@ const WithChallengeMetrics = function(WrappedComponent, applyFilters = false) {
8079
return false
8180
}
8281

83-
const challengeId =_get(this.props.challenge, 'id')
82+
const challengeId =this.props.challenge?.id
8483

8584
if (challengeId) {
86-
if (challengeId !== _get(this.props.challenge, 'id')) {
85+
if (challengeId !== (this.props.challenge?.id)) {
8786
return this.updateMetrics(this.props)
8887
}
8988

@@ -99,13 +98,13 @@ const WithChallengeMetrics = function(WrappedComponent, applyFilters = false) {
9998
return this.updateMetrics(this.props)
10099
}
101100

102-
if (!_isEqual(_get(this.props.searchFilters, 'filters'),
103-
_get(prevProps.searchFilters, 'filters'))) {
101+
if (!_isEqual(this.props.searchFilters?.filters,
102+
prevProps.searchFilters?.filters)) {
104103
return this.updateMetrics(this.props)
105104
}
106105

107-
if (!_isEqual(_get(this.props.searchCriteria, 'filters.invertFields'),
108-
_get(prevProps.searchCriteria, 'filters.invertFields'))) {
106+
if (!_isEqual(this.props.searchCriteria?.filters?.invertFields,
107+
prevProps.searchCriteria?.filters?.invertFields)) {
109108
return this.updateMetrics(this.props)
110109
}
111110
}
@@ -119,7 +118,7 @@ const WithChallengeMetrics = function(WrappedComponent, applyFilters = false) {
119118
const taskMetricsByPriority = this.state.fetchedPriorityMetrics || this.props.taskMetricsByPriority
120119

121120
if (!_isEmpty(taskMetrics) && !_isEmpty(this.props.challenges) &&
122-
_get(taskMetrics, 'total', 0) > 0) {
121+
(taskMetrics?.total ?? 0) > 0) {
123122
tasksAvailable = _sumBy(this.props.challenges, 'actions.available')
124123
if (_isFinite(tasksAvailable)) {
125124
let allActivity = _isArray(this.props.activity) ? this.props.activity :
@@ -165,11 +164,11 @@ const WithChallengeMetrics = function(WrappedComponent, applyFilters = false) {
165164
dailyMetrics={dailyMetrics}
166165
taskMetricsByPriority={taskMetricsByPriority} />
167166
}
168-
}
167+
};
169168
}
170169

171170
export const includeChallengeInMetrics = function(challenge, manager, tallied, challengeFilters, props) {
172-
const tallyMarks = tallied(_get(props.project, 'id'))
171+
const tallyMarks = tallied(props.project?.id)
173172
if (tallyMarks && tallyMarks.length > 0 && tallyMarks.indexOf(challenge.id) === -1) {
174173
return false
175174
}

src/components/AdminPane/HOCs/WithChallengeResultParents/WithChallengeResultParents.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Component } from 'react'
2-
import _get from 'lodash/get'
32
import _filter from 'lodash/filter'
43
import _each from 'lodash/each'
54
import _uniqBy from 'lodash/uniqBy'
@@ -15,7 +14,7 @@ import _isArray from 'lodash/isArray'
1514
const WithChallengeResultParents = function(WrappedComponent) {
1615
return class extends Component {
1716
projectsAndChallengeParents = () => {
18-
if (_get(this.props, 'filteredChallenges.length', 0) === 0) {
17+
if ((this.props.filteredChallenges?.length ?? 0) === 0) {
1918
return this.props.resultProjects
2019
}
2120

@@ -42,7 +41,7 @@ const WithChallengeResultParents = function(WrappedComponent) {
4241
return <WrappedComponent {...this.props}
4342
resultProjects={this.projectsAndChallengeParents()} />
4443
}
45-
}
44+
};
4645
}
4746

4847
export default WrappedComponent => WithChallengeResultParents(WrappedComponent)

src/components/AdminPane/HOCs/WithChallengeReviewMetrics/WithChallengeReviewMetrics.jsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Component } from 'react'
22
import { connect } from 'react-redux'
33
import _omit from 'lodash/omit'
4-
import _get from 'lodash/get'
54
import _keys from 'lodash/keys'
65
import _pickBy from 'lodash/pickBy'
76
import _merge from 'lodash/merge'
@@ -24,11 +23,11 @@ export const WithChallengeReviewMetrics = function(WrappedComponent) {
2423
updateMetrics(props) {
2524
this.setState({updateAvailable: false, loading: true})
2625

27-
const filters = {challengeId: _get(props.challenge, 'id')}
28-
_merge(filters, _get(props.searchFilters, 'filters'))
26+
const filters = {challengeId: props.challenge?.id}
27+
_merge(filters, props.searchFilters?.filters)
2928

3029
const criteria = {filters}
31-
criteria.invertFields = _get(props.searchCriteria, 'filters.invertFields')
30+
criteria.invertFields = props.searchCriteria?.filters?.invertFields
3231

3332
if (props.includeTaskStatuses) {
3433
criteria.filters.status = _keys(_pickBy(props.includeTaskStatuses, v => v)).join(',')
@@ -43,7 +42,7 @@ export const WithChallengeReviewMetrics = function(WrappedComponent) {
4342
criteria.filters.priorities =_keys(_pickBy(props.includeTaskPriorities, v => v)).join(',')
4443
}
4544

46-
props.updateReviewMetrics(_get(props.user, 'id'), criteria).then((entity) => {
45+
props.updateReviewMetrics(props.user?.id, criteria).then((entity) => {
4746
const reviewMetrics = entity
4847
this.setState({loading: false, reviewMetrics: reviewMetrics})
4948
})
@@ -54,7 +53,7 @@ export const WithChallengeReviewMetrics = function(WrappedComponent) {
5453
return // nothing to do
5554
}
5655

57-
if (_get(prevProps.challenge, 'id') !== _get(this.props.challenge, 'id')) {
56+
if ((prevProps.challenge?.id) !== (this.props.challenge?.id)) {
5857
this.setState({updateAvailable: true})
5958
return
6059
}
@@ -74,7 +73,7 @@ export const WithChallengeReviewMetrics = function(WrappedComponent) {
7473
return
7574
}
7675

77-
if (_get(this.props.searchFilters, 'filters') !== _get(prevProps.searchFilters, 'filters')) {
76+
if ((this.props.searchFilters?.filters) !== (prevProps.searchFilters?.filters)) {
7877
this.setState({updateAvailable: true})
7978
return
8079
}
@@ -91,13 +90,13 @@ export const WithChallengeReviewMetrics = function(WrappedComponent) {
9190
/>
9291
)
9392
}
94-
}
93+
};
9594
}
9695

9796
const mapStateToProps = state => (
98-
{reviewMetrics: _get(state, 'currentReviewTasks.metrics.reviewActions'),
99-
reviewMetricsByPriority: _get(state, 'currentReviewTasks.metrics.priorityReviewActions'),
100-
reviewMetricsByTaskStatus: _get(state, 'currentReviewTasks.metrics.statusReviewActions') }
97+
{reviewMetrics: state.currentReviewTasks?.metrics?.reviewActions,
98+
reviewMetricsByPriority: state.currentReviewTasks?.metrics?.priorityReviewActions,
99+
reviewMetricsByTaskStatus: state.currentReviewTasks?.metrics?.statusReviewActions }
101100
)
102101

103102
const mapDispatchToProps = (dispatch) => ({

src/components/AdminPane/HOCs/WithChallengeSnapshots/WithChallengeSnapshots.jsx

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component } from 'react'
22
import { injectIntl } from 'react-intl'
3-
import _get from 'lodash/get'
43
import _find from 'lodash/find'
54
import _values from 'lodash/values'
65
import { fetchChallengeSnapshotList,
@@ -17,7 +16,7 @@ const WithChallengeSnapshots = function(WrappedComponent) {
1716
}
1817

1918
updateSnapshots(props) {
20-
const challengeId =_get(props.challenge, 'id')
19+
const challengeId =props.challenge?.id
2120
if (challengeId) {
2221
this.setState({loading: true})
2322

@@ -31,7 +30,7 @@ const WithChallengeSnapshots = function(WrappedComponent) {
3130
}
3231

3332
recordSnapshot(props) {
34-
const challengeId =_get(props.challenge, 'id')
33+
const challengeId =props.challenge?.id
3534

3635
if (challengeId) {
3736
this.setState({loading: true})
@@ -66,16 +65,16 @@ const WithChallengeSnapshots = function(WrappedComponent) {
6665
}
6766

6867
componentDidUpdate(prevProps) {
69-
const challengeId =_get(this.props.challenge, 'id')
70-
if (challengeId && challengeId !== _get(prevProps.challenge, 'id')) {
68+
const challengeId =this.props.challenge?.id
69+
if (challengeId && challengeId !== (prevProps.challenge?.id)) {
7170
this.updateSnapshots(this.props)
7271
}
7372
}
7473

7574
render() {
7675
// If we don't have a selectedSnapshot in our state then we want to show
7776
// the "current" metrics which are passed to us in our props.
78-
const reviewActions = _get(this.state.selectedSnapshot, 'reviewActions')
77+
const reviewActions = this.state.selectedSnapshot?.reviewActions
7978
const reviewMetrics = reviewActions ? {
8079
total: reviewActions.total,
8180
reviewRequested: reviewActions.requested,
@@ -86,22 +85,24 @@ const WithChallengeSnapshots = function(WrappedComponent) {
8685
avgReviewTime: reviewActions.avgReviewTime,
8786
} : this.props.reviewMetrics
8887

89-
return <WrappedComponent
90-
{...this.props}
91-
recordSnapshot={() => this.recordSnapshot(this.props)}
92-
deleteSnapshot={(snapshotId) => this.deleteSnapshot(this.props, snapshotId)}
93-
snapshotList={this.state.snapshotList}
94-
setSelectedSnapshot={this.setSelectedSnapshot}
95-
currentMetrics={this.props.taskMetrics}
96-
taskMetrics =
97-
{_get(this.state.selectedSnapshot, 'actions') || this.props.taskMetrics}
98-
taskMetricsByPriority =
99-
{_get(this.state.selectedSnapshot, 'priorityActions') || this.props.taskMetricsByPriority}
100-
reviewMetrics={reviewMetrics}
101-
/>
88+
return (
89+
<WrappedComponent
90+
{...this.props}
91+
recordSnapshot={() => this.recordSnapshot(this.props)}
92+
deleteSnapshot={(snapshotId) => this.deleteSnapshot(this.props, snapshotId)}
93+
snapshotList={this.state.snapshotList}
94+
setSelectedSnapshot={this.setSelectedSnapshot}
95+
currentMetrics={this.props.taskMetrics}
96+
taskMetrics =
97+
{(this.state.selectedSnapshot?.actions) || this.props.taskMetrics}
98+
taskMetricsByPriority =
99+
{(this.state.selectedSnapshot?.priorityActions) || this.props.taskMetricsByPriority}
100+
reviewMetrics={reviewMetrics}
101+
/>
102+
);
102103

103104
}
104-
}
105+
};
105106
}
106107

107108

0 commit comments

Comments
 (0)