Skip to content

Commit 27d0a8d

Browse files
authored
Merge pull request #227 from nrotstan/bug-224-ensure-browsed-challenge-loaded
Ensure browsed challenge data is loaded.
2 parents 640c60d + d0b658a commit 27d0a8d

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

src/components/ChallengePane/ChallengeResultItem/ChallengeResultItem.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ export class ChallengeResultItem extends Component {
7676
return true
7777
}
7878

79-
// if the browsedChallenge changed and it either references our challenge now
80-
// or used to before.
81-
if (_get(nextProps, 'browsedChallenge.id') !== _get(this.props, 'browsedChallenge.id') &&
82-
(_get(nextProps, 'browsedChallenge.id') === this.props.challenge.id ||
83-
_get(this.props, 'browsedChallenge.id') === this.props.challenge.id)) {
84-
return true
79+
// if the browsedChallenge or its loading status changed and it either
80+
// references our challenge now or used to before.
81+
if (_get(nextProps, 'browsedChallenge.id') === this.props.challenge.id ||
82+
_get(this.props, 'browsedChallenge.id') === this.props.challenge.id) {
83+
if (_get(nextProps, 'browsedChallenge.id') !==
84+
_get(this.props, 'browsedChallenge.id')) {
85+
return true
86+
}
87+
88+
if (nextProps.loadingBrowsedChallenge !== this.props.loadingBrowsedChallenge) {
89+
return true
90+
}
8591
}
8692

8793
return false

src/components/ChallengePane/ChallengeResultList/ChallengeResultList.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,19 @@ export class ChallengeResultList extends Component {
3434
render() {
3535
const challengeResults = this.props.challenges
3636

37+
if (this.props.loadingBrowsedChallenge) {
38+
return (
39+
<div className="pane-loading">
40+
<BusySpinner />
41+
</div>
42+
)
43+
}
44+
3745
// If the user is actively browsing a challenge, include that challenge even if
3846
// it didn't pass the filters.
39-
if (_isObject(this.props.browsedChallenge)) {
40-
if (_findIndex(challengeResults, {id: this.props.browsedChallenge.id}) === -1) {
47+
if (_isObject(this.props.browsedChallenge) && !this.props.loadingBrowsedChallenge) {
48+
if (this.props.browsedChallenge.isVirtual ||
49+
_findIndex(challengeResults, {id: this.props.browsedChallenge.id}) === -1) {
4150
challengeResults.push(this.props.browsedChallenge)
4251
}
4352
}

src/components/HOCs/WithBrowsedChallenge/WithBrowsedChallenge.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ import { connect } from 'react-redux'
44
import _get from 'lodash/get'
55
import _isObject from 'lodash/isObject'
66
import _isFinite from 'lodash/isFinite'
7+
import _debounce from 'lodash/debounce'
78
import _find from 'lodash/find'
89
import _omit from 'lodash/omit'
10+
import { fetchChallenge }
11+
from '../../../services/Challenge/Challenge'
12+
13+
const FRESHNESS_THRESHOLD = 5000 // 5 seconds
914

1015
/**
1116
* WithBrowsedChallenge provides functions for starting and stopping browsing
@@ -19,6 +24,7 @@ export const WithBrowsedChallenge = function(WrappedComponent) {
1924
class _WithBrowsedChallenge extends Component {
2025
state = {
2126
browsedChallenge: null,
27+
loadingBrowsedChallenge: false,
2228
isVirtual: false,
2329
}
2430

@@ -67,21 +73,37 @@ export const WithBrowsedChallenge = function(WrappedComponent) {
6773

6874
if (_isFinite(challengeId)) {
6975
if (_get(this.state, 'browsedChallenge.id') !== challengeId ||
70-
this.state.isVirtual !== isVirtual) {
76+
this.state.isVirtual !== isVirtual ||
77+
this.state.loadingBrowsedChallenge) {
7178
const challenge = isVirtual ? this.props.virtualChallenge :
7279
_find(props.challenges, {id: challengeId})
7380

7481
if (_isObject(challenge)) {
75-
this.setState({browsedChallenge: challenge, isVirtual})
82+
this.setState({
83+
browsedChallenge: challenge,
84+
loadingBrowsedChallenge: false,
85+
isVirtual
86+
})
7687

7788
if (challenge.id !== _get(this.props, 'clusteredTasks.challengeId') ||
7889
isVirtual !== _get(this.props, 'clusteredTasks.isVirtualChallenge')) {
7990
this.props.fetchClusteredTasks(challenge.id, isVirtual)
8091
}
8192
}
93+
else if (!isVirtual) {
94+
// We don't have the challenge available, so fetch it.
95+
this.setState({
96+
browsedChallenge: {id: challengeId},
97+
loadingBrowsedChallenge: true,
98+
isVirtual,
99+
})
100+
101+
props.loadChallenge(challengeId)
102+
}
82103
}
83104
}
84-
else if (_isObject(this.state.browsedChallenge)) {
105+
else if (_isObject(this.state.browsedChallenge &&
106+
!this.state.loadingBrowsedChallenge)) {
85107
this.setState({browsedChallenge: null, isVirtual: false})
86108
}
87109
}
@@ -128,10 +150,13 @@ export const WithBrowsedChallenge = function(WrappedComponent) {
128150

129151
return (
130152
<WrappedComponent browsedChallenge = {this.state.browsedChallenge}
153+
loadingBrowsedChallenge = {this.state.loadingBrowsedChallenge}
131154
startBrowsingChallenge={this.startBrowsingChallenge}
132155
stopBrowsingChallenge={this.stopBrowsingChallenge}
133156
clusteredTasks={clusteredTasks}
134-
{..._omit(this.props, ['entities', 'clusteredTasks'])} />
157+
{..._omit(this.props, ['entities',
158+
'clusteredTasks',
159+
'loadChallenge'])} />
135160
)
136161
}
137162
}
@@ -148,5 +173,17 @@ const mapStateToProps = state => ({
148173
entities: state.entities,
149174
})
150175

176+
export const mapDispatchToProps = (dispatch, ownProps) => {
177+
return {
178+
loadChallenge: _debounce(
179+
challengeId => {
180+
return dispatch(fetchChallenge(challengeId))
181+
},
182+
FRESHNESS_THRESHOLD,
183+
{leading: true},
184+
),
185+
}
186+
}
187+
151188
export default WrappedComponent =>
152-
connect(mapStateToProps)(WithBrowsedChallenge(WrappedComponent))
189+
connect(mapStateToProps, mapDispatchToProps)(WithBrowsedChallenge(WrappedComponent))

src/components/HOCs/WithStartChallenge/WithStartChallenge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const WithStartChallenge = WrappedComponent =>
2929
* @private
3030
*/
3131
export const chooseVisibleTask = (challenge, challengeBounds, clusteredTasks) => {
32-
if (challenge.id !== clusteredTasks.challengeId ||
32+
if (challenge.id !== _get(clusteredTasks, 'challengeId') ||
3333
_get(clusteredTasks, 'tasks.length', 0) === 0) {
3434
return null
3535
}

0 commit comments

Comments
 (0)