Move back to previous challenge loads new challenge

pull/7430/head
Berkeley Martinez 2016-06-13 21:08:22 -07:00
parent 6628ce51b4
commit c0bd971f91
4 changed files with 45 additions and 5 deletions

View File

@ -10,7 +10,11 @@ import Step from './step/Step.jsx';
import Project from './project/Project.jsx'; import Project from './project/Project.jsx';
import Video from './video/Video.jsx'; import Video from './video/Video.jsx';
import { fetchChallenge, fetchChallenges } from '../redux/actions'; import {
fetchChallenge,
fetchChallenges,
replaceChallenge
} from '../redux/actions';
import { challengeSelector } from '../redux/selectors'; import { challengeSelector } from '../redux/selectors';
const views = { const views = {
@ -23,7 +27,8 @@ const views = {
const bindableActions = { const bindableActions = {
fetchChallenge, fetchChallenge,
fetchChallenges fetchChallenges,
replaceChallenge
}; };
const mapStateToProps = createSelector( const mapStateToProps = createSelector(
@ -47,15 +52,27 @@ const fetchOptions = {
export class Challenges extends PureComponent { export class Challenges extends PureComponent {
static displayName = 'Challenges'; static displayName = 'Challenges';
static propTypes = { static propTypes = {
isStep: PropTypes.bool, isStep: PropTypes.bool,
fetchChallenges: PropTypes.func fetchChallenges: PropTypes.func,
replaceChallenge: PropTypes.func,
params: PropTypes.object
}; };
componentDidMount() { componentDidMount() {
this.props.fetchChallenges(); this.props.fetchChallenges();
} }
componentWillReceiveProps(nextProps) {
if (this.props.params.dashedName !== nextProps.params.dashedName) {
this.props.replaceChallenge({
dashedName: nextProps.params.dashedName,
block: nextProps.params.block
});
}
}
renderView(viewType) { renderView(viewType) {
const View = views[viewType] || Classic; const View = views[viewType] || Classic;
return <View />; return <View />;

View File

@ -30,6 +30,8 @@ export const fetchChallengesCompleted = createAction(
export const updateCurrentChallenge = createAction( export const updateCurrentChallenge = createAction(
types.updateCurrentChallenge types.updateCurrentChallenge
); );
// replaceChallenge(dashedname) => Action
export const replaceChallenge = createAction(types.replaceChallenge);
// map // map
export const updateFilter = createAction( export const updateFilter = createAction(

View File

@ -1,5 +1,10 @@
import { Observable } from 'rx'; import { Observable } from 'rx';
import { fetchChallenge, fetchChallenges } from './types'; import { challengeSelector } from './selectors';
import {
fetchChallenge,
fetchChallenges,
replaceChallenge
} from './types';
import { import {
delayedRedirect, delayedRedirect,
createErrorObserable createErrorObserable
@ -14,9 +19,24 @@ export default function fetchChallengesSaga(action$, getState, { services }) {
return action$ return action$
.filter(({ type }) => ( .filter(({ type }) => (
type === fetchChallenges || type === fetchChallenges ||
type === fetchChallenge type === fetchChallenge ||
type === replaceChallenge
)) ))
.flatMap(({ type, payload: { dashedName, block } = {} }) => { .flatMap(({ type, payload: { dashedName, block } = {} }) => {
const state = getState();
if (type === replaceChallenge) {
const { challenge: newChallenge } = challengeSelector({
...state,
challengesApp: {
...state.challengesApp,
challenge: dashedName
}
});
if (state.challengesApp.challenge !== newChallenge.dashedName) {
return Observable.just(updateCurrentChallenge(newChallenge));
}
return Observable.just(null);
}
const options = { service: 'map' }; const options = { service: 'map' };
if (type === fetchChallenge) { if (type === fetchChallenge) {
options.params = { dashedName, block }; options.params = { dashedName, block };

View File

@ -11,6 +11,7 @@ export default createTypes([
'fetchChallengeCompleted', 'fetchChallengeCompleted',
'fetchChallengesCompleted', 'fetchChallengesCompleted',
'updateCurrentChallenge', 'updateCurrentChallenge',
'replaceChallenge',
'resetUi', 'resetUi',
// map // map