Files
freeCodeCamp/common/app/routes/challenges/components/map/Map.jsx

124 lines
3.0 KiB
JavaScript
Raw Normal View History

2016-03-21 15:39:45 -07:00
import React, { PropTypes } from 'react';
import { compose } from 'redux';
import { contain } from 'redux-epic';
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
2016-03-21 15:39:45 -07:00
import PureComponent from 'react-pure-render/component';
import MapHeader from './Header.jsx';
import SuperBlock from './Super-Block.jsx';
import FullStack from './Full-Stack.jsx';
import CodingPrep from './Coding-Prep.jsx';
import {
clearFilter,
updateFilter,
fetchChallenges
} from '../../redux/actions';
2016-03-21 15:39:45 -07:00
const bindableActions = {
clearFilter,
fetchChallenges,
updateFilter
};
const superBlocksSelector = createSelector(
state => state.challengesApp.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: superBlocks
.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.challengesApp.filter,
({ superBlocks }, filter) => {
return {
superBlocks,
filter
};
}
);
const fetchOptions = {
fetchAction: 'fetchChallenges',
isPrimed({ superBlocks }) {
return Array.isArray(superBlocks) && superBlocks.length > 1;
}
};
export class ShowMap extends PureComponent {
2016-03-21 15:39:45 -07:00
static displayName = 'Map';
static propTypes = {
2016-03-23 16:19:16 -07:00
clearFilter: PropTypes.func,
filter: PropTypes.string,
2016-03-23 16:19:16 -07:00
superBlocks: PropTypes.array,
updateFilter: PropTypes.func
2016-03-21 15:39:45 -07:00
};
2016-05-10 21:25:12 -07:00
renderSuperBlocks(superBlocks, updateCurrentChallenge) {
2016-03-21 15:39:45 -07:00
if (!Array.isArray(superBlocks) || !superBlocks.length) {
return <div>No Super Blocks</div>;
}
return superBlocks.map((superBlock) => {
return (
<SuperBlock
key={ superBlock.title }
2016-05-10 21:25:12 -07:00
updateCurrentChallenge={ updateCurrentChallenge }
2016-06-21 11:50:58 -07:00
{ ...superBlock }
/>
2016-03-21 15:39:45 -07:00
);
});
}
render() {
const {
2016-05-10 21:25:12 -07:00
updateCurrentChallenge,
superBlocks,
updateFilter,
clearFilter,
filter
} = this.props;
2016-03-21 15:39:45 -07:00
return (
<div>
<MapHeader
clearFilter={ clearFilter }
filter={ filter }
updateFilter={ updateFilter }
/>
2016-03-21 15:39:45 -07:00
<div
2016-06-21 11:50:58 -07:00
className='map-accordion'
>
2016-05-10 21:25:12 -07:00
{ this.renderSuperBlocks(superBlocks, updateCurrentChallenge) }
<FullStack />
<CodingPrep />
2016-03-21 15:39:45 -07:00
</div>
</div>
);
}
}
export default compose(
connect(mapStateToProps, bindableActions),
contain(fetchOptions)
)(ShowMap);