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

76 lines
1.6 KiB
JavaScript
Raw Normal View History

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-05 21:06:04 -08:00
import PureComponent from 'react-pure-render/component';
2016-05-13 22:34:40 -07:00
import Classic from './classic/Classic.jsx';
import Step from './step/Step.jsx';
2016-06-03 18:37:53 -07:00
import Project from './project/Project.jsx';
2016-06-01 15:52:08 -07:00
import { fetchChallenge, fetchChallenges } from '../redux/actions';
2016-05-10 13:17:57 -07:00
import { challengeSelector } from '../redux/selectors';
2016-06-03 14:12:56 -07:00
const views = {
step: Step,
2016-06-03 18:37:53 -07:00
classic: Classic,
2016-06-07 20:41:42 -07:00
project: Project,
simple: Project
2016-06-03 14:12:56 -07:00
};
const bindableActions = {
2016-06-01 15:52:08 -07:00
fetchChallenge,
fetchChallenges
};
const mapStateToProps = createSelector(
challengeSelector,
2016-05-10 13:17:57 -07:00
state => state.challengesApp.challenge,
2016-06-03 14:12:56 -07:00
({ viewType }, challenge) => ({
challenge,
viewType
})
);
const fetchOptions = {
fetchAction: 'fetchChallenge',
2016-06-09 16:02:51 -07:00
getActionArgs({ params: { block, dashedName } }) {
return [ dashedName, block ];
},
isPrimed({ challenge }) {
2016-05-10 13:17:57 -07:00
return !!challenge;
}
};
export class Challenges extends PureComponent {
2016-03-05 21:06:04 -08:00
static displayName = 'Challenges';
2016-06-01 15:52:08 -07:00
static propTypes = {
isStep: PropTypes.bool,
fetchChallenges: PropTypes.func
};
2016-03-05 21:06:04 -08:00
2016-06-01 15:52:08 -07:00
componentDidMount() {
this.props.fetchChallenges();
}
2016-06-03 14:12:56 -07:00
renderView(viewType) {
const View = views[viewType] || Classic;
return <View />;
2016-03-05 21:06:04 -08:00
}
2016-06-01 15:52:08 -07:00
render() {
2016-06-03 14:12:56 -07:00
const { viewType } = this.props;
2016-06-01 15:52:08 -07:00
return (
<div>
2016-06-03 14:12:56 -07:00
{ this.renderView(viewType) }
2016-06-01 15:52:08 -07:00
</div>
);
}
2016-03-05 21:06:04 -08:00
}
export default compose(
connect(mapStateToProps, bindableActions),
contain(fetchOptions)
)(Challenges);