Files
freeCodeCamp/common/app/routes/map/components/Show.jsx

84 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-03-21 15:39:45 -07:00
import React, { PropTypes } from 'react';
import { compose } from 'redux';
import { connect } from 'react-redux';
import PureComponent from 'react-pure-render/component';
import { createSelector } from 'reselect';
import Map from './Map.jsx';
import contain from '../../../utils/professor-x';
2016-03-23 16:19:16 -07:00
import {
clearFilter,
fetchChallenges,
updateFilter
} from '../redux/actions';
2016-03-21 15:39:45 -07:00
const bindableActions = {
2016-03-23 16:19:16 -07:00
clearFilter,
fetchChallenges,
updateFilter
};
const superBlocksSelector = createSelector(
2016-03-21 15:39:45 -07:00
state => state.map.superBlocks,
state => state.entities.superBlock,
state => state.entities.block,
state => state.entities.challenge,
(superBlocks, superBlockMap, blockMap, challengeMap) => {
if (!superBlockMap || !blockMap || !challengeMap) {
return {
superBlocks: []
};
}
return superBlocks
2016-03-21 15:39:45 -07:00
.map(superBlockName => superBlockMap[superBlockName])
.map(superBlock => ({
...superBlock,
blocks: superBlock.blocks
.map(blockName => blockMap[blockName])
.map(block => ({
...block,
challenges: block.challenges
.map(dashedName => challengeMap[dashedName])
}))
}));
}
);
const mapStateToProps = createSelector(
superBlocksSelector,
state => state.map.filter,
(superBlocks, filter) => {
2016-03-21 15:39:45 -07:00
return {
superBlocks,
filter
2016-03-21 15:39:45 -07:00
};
}
);
2016-03-21 15:39:45 -07:00
const fetchOptions = {
fetchAction: 'fetchChallenges',
isPrimed({ superBlocks }) {
return Array.isArray(superBlocks) && superBlocks.length > 0;
}
};
export class ShowMap extends PureComponent {
static displayName = 'ShowMap';
static propTypes = {
2016-03-23 16:19:16 -07:00
clearFilter: PropTypes.func,
filter: PropTypes.string,
superBlocks: PropTypes.array,
updateFilter: PropTypes.func
2016-03-21 15:39:45 -07:00
};
render() {
return (
<Map { ...this.props } />
2016-03-21 15:39:45 -07:00
);
}
}
export default compose(
connect(mapStateToProps, bindableActions),
contain(fetchOptions)
)(ShowMap);