Skip to content

Commit 0eb72a2

Browse files
authored
Merge pull request #100 from nrotstan/add-output-prop-to-with-sorted-challenges
Support output prop on WithSortedChallenges.
2 parents 30c0f89 + a9c8c87 commit 0eb72a2

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/components/ChallengePane/ChallengeResultList/ChallengeResultList.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export default WithCurrentUser(
113113
WithFilteredChallenges(
114114
WithSearchResults(
115115
WithSortedChallenges(
116-
ChallengeResultList
116+
ChallengeResultList,
117+
'filteredChallenges'
117118
),
118119
'challenges',
119120
'filteredChallenges'

src/components/HOCs/WithSortedChallenges/WithSortedChallenges.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,35 @@ import PropTypes from 'prop-types'
33
import _get from 'lodash/get'
44
import _findIndex from 'lodash/findIndex'
55
import _sortBy from 'lodash/sortBy'
6+
import _isEmpty from 'lodash/isEmpty'
67
import _omit from 'lodash/omit'
78

89
const FEATURED_POINTS = -1
910
const SAVED_POINTS = -2
1011

11-
export default function(WrappedComponent) {
12+
export default function(WrappedComponent,
13+
challengesProp='challenges',
14+
outputProp) {
1215
class WithSortedChallenges extends Component {
1316
render() {
1417
// Give extra points to featured challenges and -- if we're given a
1518
// user -- saved challenges
1619
const savedChallenges = _get(this.props, 'user.savedChallenges', [])
1720

18-
const sortedChallenges = _sortBy(this.props.challenges, challenge => {
21+
const sortedChallenges = _sortBy(this.props[challengesProp], challenge => {
1922
let score = 0
2023
score += challenge.featured ? FEATURED_POINTS : 0
2124
score += _findIndex(savedChallenges, {id: challenge.id}) !== -1 ?
2225
SAVED_POINTS : 0
2326
return score
2427
})
2528

26-
return <WrappedComponent challenges={sortedChallenges}
27-
{..._omit(this.props, 'challenges')} />
29+
if (_isEmpty(outputProp)) {
30+
outputProp = challengesProp
31+
}
32+
33+
return <WrappedComponent {...{[outputProp]: sortedChallenges}}
34+
{..._omit(this.props, outputProp)} />
2835
}
2936
}
3037

0 commit comments

Comments
 (0)