feat(map-service): Refactor out the map service

This commit is contained in:
Stuart Taylor
2018-02-23 17:08:42 +00:00
parent a7587ed6f0
commit 2143063084
5 changed files with 36 additions and 114 deletions

View File

@ -25,7 +25,7 @@ function fetchChallengeEpic(actions, { getState }, { services }) {
.filter(({ payload }) => !isChallengeLoaded(getState(), payload)) .filter(({ payload }) => !isChallengeLoaded(getState(), payload))
.flatMapLatest(({ payload: params }) => { .flatMapLatest(({ payload: params }) => {
const options = { const options = {
service: 'map', service: 'challenge',
params params
}; };
return services.readService$(options) return services.readService$(options)
@ -76,7 +76,7 @@ export function fetchChallengesForBlockEpic(
const options = { const options = {
params: { lang, blockName }, params: { lang, blockName },
service: 'challenges-for-block' service: 'challenge'
}; };
return services.readService$(options) return services.readService$(options)
.retry(3) .retry(3)

View File

@ -1,20 +1,17 @@
import Fetchr from 'fetchr'; import Fetchr from 'fetchr';
import getUserServices from '../services/user'; import getUserServices from '../services/user';
import getMapServices from '../services/map';
import getMapUiServices from '../services/mapUi'; import getMapUiServices from '../services/mapUi';
import getChallengesForBlockService from '../services/challenge'; import getChallengesForBlockService from '../services/challenge';
export default function bootServices(app) { export default function bootServices(app) {
const user = getUserServices(app);
const map = getMapServices(app);
const mapUi = getMapUiServices(app);
const challenge = getChallengesForBlockService(app); const challenge = getChallengesForBlockService(app);
const mapUi = getMapUiServices(app);
const user = getUserServices(app);
Fetchr.registerFetcher(user);
Fetchr.registerFetcher(map);
Fetchr.registerFetcher(mapUi);
Fetchr.registerFetcher(challenge); Fetchr.registerFetcher(challenge);
Fetchr.registerFetcher(mapUi);
Fetchr.registerFetcher(user);
app.use('/services', Fetchr.middleware()); app.use('/services', Fetchr.middleware());
} }

View File

@ -2,7 +2,7 @@ import debug from 'debug';
import { pickBy } from 'lodash'; import { pickBy } from 'lodash';
import { Observable } from 'rx'; import { Observable } from 'rx';
import { cachedMap, getMapForLang } from '../utils/map'; import { cachedMap, getMapForLang, getChallenge } from '../utils/map';
import { shapeChallenges } from '../../common/app/redux/utils'; import { shapeChallenges } from '../../common/app/redux/utils';
const log = debug('fcc:services:challenge'); const log = debug('fcc:services:challenge');
@ -11,16 +11,15 @@ const isDev = debug.enabled('fcc:*');
export default function getChallengesForBlock(app) { export default function getChallengesForBlock(app) {
const challengeMap = cachedMap(app.models); const challengeMap = cachedMap(app.models);
return { return {
name: 'challenges-for-block', name: 'challenge',
read: function readChallengesForBlock( read: function readChallengesForBlock(
req, req,
resource, resource,
{ blockName, lang = 'en' } = {}, { dashedName, blockName, lang = 'en' } = {},
config, config,
cb cb
) { ) {
log(`sourcing challenges for the ${blockName} block`); const getChallengeBlock$ = challengeMap.map(getMapForLang(lang))
return challengeMap.map(getMapForLang(lang))
.flatMap(({ .flatMap(({
result: { superBlocks }, result: { superBlocks },
entities: { entities: {
@ -28,22 +27,28 @@ export default function getChallengesForBlock(app) {
challenge: challengeMap challenge: challengeMap
} }
}) => { }) => {
const requestedChallenges = pickBy( log(`sourcing challenges for the ${blockName} block`);
challengeMap, const requestedChallenges = pickBy(
ch => ch.block === blockName challengeMap,
); ch => ch.block === blockName
const entities = { );
block: { const entities = {
[blockName]: fullBlockMap[blockName] block: {
}, [blockName]: fullBlockMap[blockName]
challenge: requestedChallenges },
}; challenge: requestedChallenges
const { challenge, block } = shapeChallenges(entities, isDev); };
return Observable.of({ const { challenge, block } = shapeChallenges(entities, isDev);
result: { superBlocks }, return Observable.of({
entities: { challenge, block } result: { superBlocks },
entities: { challenge, block }
});
}); });
}) return Observable.if(
() => !!dashedName,
getChallenge(dashedName, blockName, challengeMap, lang),
getChallengeBlock$
)
.subscribe( .subscribe(
result => cb(null, result), result => cb(null, result),
cb cb

View File

@ -1,28 +0,0 @@
import { Observable } from 'rx';
import debug from 'debug';
import {
cachedMap,
getChallenge,
getMapForLang
} from '../utils/map';
const log = debug('fcc:services:map');
export default function mapService(app) {
const challengeMap = cachedMap(app.models);
return {
name: 'map',
read: (req, resource, { lang, block, dashedName } = {}, config, cb) => {
log(`${lang} language requested`);
return Observable.if(
() => !!dashedName,
getChallenge(dashedName, block, challengeMap, lang),
challengeMap.map(getMapForLang(lang))
)
.subscribe(
results => cb(null, results),
err => { log(err); cb(err); }
);
}
};
}

View File

@ -154,58 +154,6 @@ export function getMapForLang(lang) {
}; };
} }
export function generateMapForLang(
superBlocks,
fullSuperBlockMap,
fullBlockMap,
fullChallengeMap
) {
const superBlockMap = superBlocks
.map(superBlock => fullSuperBlockMap[superBlock])
.reduce((map, { dashedName, blocks, title }) => {
map[dashedName] = { blocks, title, dashedName};
return map;
}, {});
const blockMap = Object.keys(fullBlockMap)
.map(block => fullBlockMap[block])
.reduce((map, { dashedName, title, time, challenges }) => {
map[dashedName] = { dashedName, title, time, challenges };
return map;
}, {});
const challengeMap = Object.keys(fullChallengeMap)
.map(challenge => fullChallengeMap[challenge])
.reduce((map, challenge) => {
const {
dashedName,
id,
title,
block,
isLocked,
isComingSoon,
isBeta
} = challenge;
map[dashedName] = {
dashedName,
id,
title,
block,
isLocked,
isComingSoon,
isBeta
};
return map;
}, {});
return {
result: { superBlocks },
entities: {
superBlock: superBlockMap,
block: blockMap,
challenge: challengeMap
}
};
}
// type ObjectId: String; // type ObjectId: String;
// getChallengeById( // getChallengeById(
// map: Observable[map], // map: Observable[map],