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

151 lines
3.6 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,
2016-06-21 16:28:13 -07:00
fetchChallenges,
toggleThisPanel,
updateFilter
} from '../../redux/actions';
2016-03-21 15:39:45 -07:00
const bindableActions = {
clearFilter,
fetchChallenges,
2016-06-21 16:28:13 -07:00
toggleThisPanel,
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,
2016-06-21 16:28:13 -07:00
state => state.challengesApp.map,
({ superBlocks }, filter, mapUi) => {
return {
superBlocks,
2016-06-21 16:28:13 -07:00
filter,
mapUi
};
}
);
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,
2016-06-21 16:28:13 -07:00
updateFilter: PropTypes.func,
mapUi: PropTypes.object
2016-03-21 15:39:45 -07:00
};
2016-06-21 16:28:13 -07:00
renderSuperBlocks(
superBlocks,
updateCurrentChallenge,
mapUi,
toggleThisPanel
) {
2016-03-21 15:39:45 -07:00
if (!Array.isArray(superBlocks) || !superBlocks.length) {
return <div>No Super Blocks</div>;
}
2016-06-21 16:28:13 -07:00
return superBlocks
.map((superBlock) => {
return (
<SuperBlock
key={ superBlock.title }
mapUi={ mapUi }
toggleThisPanel={ toggleThisPanel }
updateCurrentChallenge={ updateCurrentChallenge }
{ ...superBlock }
/>
);
});
2016-03-21 15:39:45 -07:00
}
render() {
const {
2016-05-10 21:25:12 -07:00
updateCurrentChallenge,
superBlocks,
updateFilter,
clearFilter,
2016-06-21 16:28:13 -07:00
filter,
mapUi,
toggleThisPanel
} = this.props;
2016-03-21 15:39:45 -07:00
return (
<div>
<MapHeader
clearFilter={ clearFilter }
filter={ filter }
updateFilter={ updateFilter }
/>
2016-06-21 16:28:13 -07:00
<div className='map-accordion'>
{
this.renderSuperBlocks(
superBlocks,
updateCurrentChallenge,
mapUi,
toggleThisPanel
)
}
<FullStack
mapUi={ mapUi }
toggleThisPanel={ toggleThisPanel }
/>
<CodingPrep
mapUi={ mapUi }
toggleThisPanel={ toggleThisPanel }
/>
<div className='spacer' />
2016-03-21 15:39:45 -07:00
</div>
</div>
);
}
}
export default compose(
connect(mapStateToProps, bindableActions),
contain(fetchOptions)
)(ShowMap);