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
This commit is contained in:
Berkeley Martinez
2017-08-03 20:45:36 -07:00
committed by Quincy Larson
parent f92294bbda
commit c547c26bba
16 changed files with 352 additions and 364 deletions

View File

@@ -4,12 +4,7 @@ import accepts from 'accepts';
import dedent from 'dedent';
import { ifNoUserSend } from '../utils/middleware';
import { cachedMap } from '../utils/map';
import createNameIdMap from '../../common/utils/create-name-id-map';
import {
checkMapData,
getFirstChallenge
} from '../../common/utils/get-first-challenge';
import { getChallengeById, cachedMap } from '../utils/map';
const log = debug('fcc:boot:challenges');
@@ -73,8 +68,7 @@ export default function(app) {
const send200toNonUser = ifNoUserSend(true);
const api = app.loopback.Router();
const router = app.loopback.Router();
const Block = app.models.Block;
const map$ = cachedMap(Block);
const map = cachedMap(app.models);
api.post(
'/modern-challenge-completed',
@@ -344,43 +338,23 @@ export default function(app) {
function redirectToCurrentChallenge(req, res, next) {
const { user } = req;
return map$
.map(({ entities, result }) => ({
result,
entities: createNameIdMap(entities)
}))
.map(map => {
checkMapData(map);
const {
entities: { challenge: challengeMap, challengeIdToName }
} = map;
let finalChallenge;
const dashedName = challengeIdToName[user && user.currentChallengeId];
finalChallenge = challengeMap[dashedName];
// redirect to first challenge
if (!finalChallenge) {
finalChallenge = getFirstChallenge(map);
}
const { block, dashedName: finalDashedName } = finalChallenge || {};
if (!finalDashedName || !block) {
const challengeId = user && user.currentChallengeId;
return getChallengeById(map, challengeId)
.map(challenge => {
const { block, dashedName } = challenge;
if (!dashedName || !block) {
// this should normally not be hit if database is properly seeded
console.error(new Error(dedent`
throw new Error(dedent`
Attemped to find '${dashedName}'
from '${user && user.currentChallengeId || 'no challenge id found'}'
from '${ challengeId || 'no challenge id found'}'
but came up empty.
db may not be properly seeded.
`));
if (dashedName) {
// attempt to find according to dashedName
return `/challenges/${dashedName}`;
} else {
return null;
}
`);
}
return `/challenges/${block}/${finalDashedName}`;
return `/challenges/${block}/${dashedName}`;
})
.subscribe(
redirect => res.redirect(redirect || '/map'),
redirect => res.redirect(redirect || '/'),
next
);
}