fetch/set current hike shouldn't need to pass around state

This commit is contained in:
Berkeley Martinez
2015-07-16 16:55:37 -07:00
parent 639f85c580
commit 2d5013d9ae
4 changed files with 33 additions and 41 deletions

View File

@ -11,18 +11,16 @@ const debug = debugFactory('freecc:hikes');
export default contain( export default contain(
{ {
store: 'hikesStore', store: 'hikesStore',
fetchAction: 'hikesActions.getHike', fetchAction: 'hikesActions.fetchCurrentHike',
getPayload({ currentHike, hikes, params: { dashedName } }) { getPayload({ currentHike, params: { dashedName } }) {
const filterRegex = new RegExp(dashedName, 'i'); const filterRegex = new RegExp(dashedName, 'i');
if (currentHike && filterRegex.test(currentHike.dashedName)) { if (currentHike && filterRegex.test(currentHike.dashedName)) {
return { return {
hikes: [],
isPrimed: true, isPrimed: true,
dashedName dashedName
}; };
} }
return { return {
hikes,
isPrimed: false, isPrimed: false,
dashedName: dashedName dashedName: dashedName
}; };
@ -32,8 +30,9 @@ export default contain(
displayName: 'Lecture', displayName: 'Lecture',
propTypes: { propTypes: {
params: PropTypes.object, currentHike: PropTypes.object,
currentHike: PropTypes.object dashedName: PropTypes.string,
params: PropTypes.object
}, },
handleError: debug, handleError: debug,

View File

@ -16,26 +16,21 @@ const debug = debugFactory('freecc:hikes');
export default contain( export default contain(
{ {
store: 'hikesStore', store: 'hikesStore',
map({ hikes, currentHike }) { map({ currentHike }) {
const { tests = [] } = currentHike; const { tests = [] } = currentHike;
return {
hikes, return { currentHike, tests };
currentHike,
tests
};
}, },
fetchAction: 'hikesActions.getHike', fetchAction: 'hikesActions.fetchCurrentHike',
getPayload({ currentHike, hikes, params: { dashedName } }) { getPayload({ currentHike, params: { dashedName } }) {
const filterRegex = new RegExp(dashedName, 'i'); const filterRegex = new RegExp(dashedName, 'i');
if (currentHike && filterRegex.test(currentHike.dashedName)) { if (currentHike && filterRegex.test(currentHike.dashedName)) {
return { return {
hikes: [],
isPrimed: true, isPrimed: true,
dashedName dashedName
}; };
} }
return { return {
hikes,
isPrimed: false, isPrimed: false,
dashedName: dashedName dashedName: dashedName
}; };
@ -46,9 +41,9 @@ export default contain(
displayName: 'Question', displayName: 'Question',
propTypes: { propTypes: {
params: PropTypes.object,
currentHike: PropTypes.object, currentHike: PropTypes.object,
dashedName: PropTypes.string, dashedName: PropTypes.string,
params: PropTypes.object,
tests: PropTypes.array tests: PropTypes.array
}, },

View File

@ -1,5 +1,6 @@
import { helpers } from 'rx'; import { helpers } from 'rx';
import { Actions } from 'thundercats'; import { Actions } from 'thundercats';
import assign from 'object.assign';
import debugFactory from 'debug'; import debugFactory from 'debug';
import Fetchr from 'fetchr'; import Fetchr from 'fetchr';
@ -20,15 +21,13 @@ export default Actions({
}; };
}, },
getHike: null,
reEmit() { reEmit() {
return helpers.identity; return helpers.identity;
}, },
setCurrentHike(currentHike) { fetchCurrentHike: null,
return { currentHike }; setCurrentHike: null
}
}) })
.refs({ displayName: 'HikesActions' }) .refs({ displayName: 'HikesActions' })
.init(({ instance }) => { .init(({ instance }) => {
@ -36,10 +35,8 @@ export default Actions({
instance.fetchHikes.subscribe( instance.fetchHikes.subscribe(
({ isPrimed }) => { ({ isPrimed }) => {
if (isPrimed) { if (isPrimed) {
debug('already primed');
return instance.reEmit(); return instance.reEmit();
} }
debug('fetching');
service.read('hikes', null, null, (err, hikes) => { service.read('hikes', null, null, (err, hikes) => {
if (err) { if (err) {
debug('an error occurred fetching hikes', err); debug('an error occurred fetching hikes', err);
@ -49,30 +46,31 @@ export default Actions({
} }
); );
instance.getHike.subscribe(({ isPrimed, hikes, dashedName }) => { instance.fetchCurrentHike.subscribe(({ isPrimed, dashedName }) => {
if (isPrimed) { if (isPrimed) {
return instance.reEmit(); return instance.setCurrentHike({
} transformer: (oldState) => {
if (hikes && hikes.length) { const { hikes } = oldState;
const filterRegex = new RegExp(dashedName, 'i'); const filterRegex = new RegExp(dashedName, 'i');
const potentialHike = hikes const potentialHike = hikes
.filter((hike) => { .filter(({ dashedName }) => {
return filterRegex.test(hike.dashedName); return filterRegex.test(dashedName);
}) })
.reduce((sum, hike) => { .reduce((throwAway, hike) => {
return hike; return hike;
}); });
if (potentialHike) { // TODO(berks): do something when potential hike does not exist
return instance.setCurrentHike(potentialHike); return assign({}, oldState, { currentHike: potentialHike });
} }
});
} }
service.read('hikes', { dashedName }, null, (err, hikes) => { service.read('hikes', { dashedName }, null, (err, hikes) => {
if (err) { if (err) {
debug('error occurred fetching hike', err); debug('error occurred fetching hike', err);
} }
const [hike] = hikes; const [currentHike] = hikes;
return instance.setCurrentHike(hike); return instance.setCurrentHike({ set: { currentHike } });
}); });
}); });
}); });

View File

@ -19,7 +19,7 @@ export default Store(initialValue)
instance.register( instance.register(
fromMany( fromMany(
setter(setHikes), setter(setHikes),
setter(setCurrentHike), setCurrentHike,
transformer(reEmit) transformer(reEmit)
) )
); );