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))
.flatMapLatest(({ payload: params }) => {
const options = {
service: 'map',
service: 'challenge',
params
};
return services.readService$(options)
@ -76,7 +76,7 @@ export function fetchChallengesForBlockEpic(
const options = {
params: { lang, blockName },
service: 'challenges-for-block'
service: 'challenge'
};
return services.readService$(options)
.retry(3)

View File

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

View File

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