@@ -3,28 +3,35 @@ import PropTypes from 'prop-types'
33import _get from 'lodash/get'
44import _findIndex from 'lodash/findIndex'
55import _sortBy from 'lodash/sortBy'
6+ import _isEmpty from 'lodash/isEmpty'
67import _omit from 'lodash/omit'
78
89const FEATURED_POINTS = - 1
910const 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