Files
freeCodeCamp/common/app/routes/challenges/components/video/Video.jsx

87 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-12-22 19:33:25 -08:00
import React, { PropTypes } from 'react';
2016-01-27 11:34:44 -08:00
import { connect } from 'react-redux';
import { Col } from 'react-bootstrap';
2016-01-27 11:34:44 -08:00
import { createSelector } from 'reselect';
2015-12-22 19:33:25 -08:00
import Lecture from './Lecture.jsx';
import Questions from './Questions.jsx';
import { resetUi } from '../../redux/actions';
import { updateTitle } from '../../../../redux/actions';
import { challengeSelector } from '../../redux/selectors';
2015-12-22 19:33:25 -08:00
const bindableActions = { resetUi, updateTitle };
2016-01-27 11:34:44 -08:00
const mapStateToProps = createSelector(
challengeSelector,
state => state.challengesApp.shouldShowQuestions,
({ challenge: { title } }, shouldShowQuestions) => ({
title,
2016-02-04 15:03:05 -08:00
shouldShowQuestions
})
);
2016-02-04 12:40:49 -08:00
2016-01-27 11:34:44 -08:00
// export plain component for testing
export class Video extends React.Component {
static displayName = 'Video';
2016-01-27 11:34:44 -08:00
static propTypes = {
2016-02-04 14:13:13 -08:00
// actions
resetUi: PropTypes.func,
2016-02-04 14:13:13 -08:00
// ui
title: PropTypes.string,
params: PropTypes.object,
2016-05-28 09:01:18 +04:00
shouldShowQuestions: PropTypes.bool,
updateTitle: PropTypes.func
2016-01-27 11:34:44 -08:00
};
2016-05-28 09:01:18 +04:00
componentWillMount() {
const { updateTitle } = this.props;
updateTitle(this.props.title);
}
2016-01-27 11:34:44 -08:00
componentWillUnmount() {
this.props.resetUi();
2016-01-27 11:34:44 -08:00
}
componentWillReceiveProps({ title }) {
if (this.props.title !== title) {
this.props.resetUi();
2016-01-27 11:34:44 -08:00
}
}
renderBody(showQuestions) {
if (showQuestions) {
return <Questions />;
}
return <Lecture />;
}
render() {
const {
title,
2016-02-04 14:13:13 -08:00
shouldShowQuestions
2016-01-27 11:34:44 -08:00
} = this.props;
return (
<Col xs={ 12 }>
<header className='text-center'>
<h4>{ title }</h4>
</header>
<hr />
<div className='spacer' />
<section
className={ 'text-center' }
title={ title }
>
{ this.renderBody(shouldShowQuestions) }
</section>
2016-01-27 11:34:44 -08:00
</Col>
);
}
}
// export redux aware component
export default connect(
mapStateToProps,
bindableActions
)(Video);