Files
freeCodeCamp/common/app/Nav/redux/load-current-challenge-epic.js
Berkeley Martinez c547c26bba Fix: map should redirect to current challenge (#15723)
* fix(routes): /map redirects to current challenge

* fix(map): Normalize server map building

Localize all server code dealing with the map

* refactor(server): Remove unused services

* feat(Nav): Show Map button when no panes

This gives user the ability to quickly return to their challenge using a
known feature

* fix(server.map): Add caching to nameIdMap

Add caching to nameIdMap on the server

* fix(services.map): Fix map service

Move map building utils to map util. Fix bad import. Normalize challenge
lookup
2017-08-03 22:45:36 -05:00

62 lines
1.8 KiB
JavaScript

import { ofType } from 'redux-epic';
import { types } from './';
import {
updateCurrentChallenge,
userSelector,
firstChallengeSelector,
challengeSelector
} from '../../redux';
import { entitiesSelector } from '../../entities';
export default function loadCurrentChallengeEpic(actions, { getState }) {
return actions::ofType(types.clickOnLogo, types.clickOnMap)
.debounce(500)
.map(() => {
let finalChallenge;
const state = getState();
const { id: currentlyLoadedChallengeId } = challengeSelector(state);
const {
challenge: challengeMap,
challengeIdToName
} = entitiesSelector(state);
const {
routing: {
locationBeforeTransitions: { pathname } = {}
}
} = state;
const firstChallenge = firstChallengeSelector(state);
const { currentChallengeId } = userSelector(state);
const isOnAChallenge = (/^\/[^\/]{2,6}\/challenges/).test(pathname);
if (!currentChallengeId) {
finalChallenge = firstChallenge;
} else {
finalChallenge = challengeMap[
challengeIdToName[ currentChallengeId ]
];
}
return {
finalChallenge,
isOnAChallenge,
currentlyLoadedChallengeId
};
})
.filter(({
finalChallenge,
isOnAChallenge,
currentlyLoadedChallengeId
}) => (
// data might not be there yet, filter out for now
!!finalChallenge &&
// are we already on that challenge? if not load challenge
(!isOnAChallenge || finalChallenge.id !== currentlyLoadedChallengeId)
// don't reload if the challenge is already loaded.
// This may change to toast to avoid user confusion
))
.map(({ finalChallenge }) => {
return updateCurrentChallenge(finalChallenge.dashedName);
});
}