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

147 lines
3.4 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react';
2016-02-25 18:30:10 -08:00
import { compose } from 'redux';
import { connect } from 'react-redux';
2016-02-05 20:48:59 -08:00
import { push } from 'react-router-redux';
import PureComponent from 'react-pure-render/component';
import { createSelector } from 'reselect';
import contain from '../../../utils/professor-x';
2016-02-28 17:25:21 -08:00
import { fetchJobs } from '../redux/actions';
2015-10-19 22:51:30 -07:00
2015-09-26 22:23:56 -07:00
import ShowJob from './ShowJob.jsx';
2015-10-19 22:51:30 -07:00
import JobNotFound from './JobNotFound.jsx';
import { isJobValid } from '../utils';
2015-08-25 14:36:07 -07:00
function shouldShowApply(
2015-11-05 12:36:02 -08:00
{
isFrontEndCert: isFrontEndCertReq = false,
isBackEndCert: isBackEndCertReq = false
2015-11-05 12:36:02 -08:00
}, {
isFrontEndCert = false,
isBackEndCert = false
2015-11-05 12:36:02 -08:00
}
) {
return (!isFrontEndCertReq && !isBackEndCertReq) ||
(isBackEndCertReq && isBackEndCert) ||
2015-11-05 12:36:02 -08:00
(isFrontEndCertReq && isFrontEndCert);
}
function generateMessage(
{
isFrontEndCert: isFrontEndCertReq = false,
isBackEndCert: isBackEndCertReq = false
},
{
2015-11-05 12:36:02 -08:00
isFrontEndCert = false,
isBackEndCert = false,
2015-11-05 12:36:02 -08:00
isSignedIn = false
}
) {
if (!isSignedIn) {
return 'Must be signed in to apply';
2015-11-05 12:36:02 -08:00
}
if (isFrontEndCertReq && !isFrontEndCert) {
return 'This employer requires Free Code Camps Front ' +
'End Development Certification in order to apply';
2015-11-05 12:36:02 -08:00
}
if (isBackEndCertReq && !isBackEndCert) {
return 'This employer requires Free Code Camps Back ' +
'End Development Certification in order to apply';
}
if (isFrontEndCertReq && isFrontEndCertReq) {
return 'This employer requires the Front End Development Certification. ' +
"You've earned it, so feel free to apply.";
}
return 'This employer requires the Back End Development Certification. ' +
"You've earned it, so feel free to apply.";
2015-11-05 12:36:02 -08:00
}
2016-02-05 20:48:59 -08:00
const mapStateToProps = createSelector(
state => state.app,
state => state.jobsApp.currentJob,
2016-02-28 17:25:21 -08:00
state => state.jobsApp.jobs.entities,
({ username, isFrontEndCert, isBackEndCert }, currentJob, jobs) => ({
2016-02-05 20:48:59 -08:00
username,
isFrontEndCert,
isBackEndCert,
2016-02-28 17:25:21 -08:00
job: jobs[currentJob] || {}
2015-10-19 22:51:30 -07:00
})
2015-08-29 01:00:52 -07:00
);
2016-02-05 20:48:59 -08:00
const bindableActions = {
push,
2016-02-28 17:25:21 -08:00
fetchJobs
2016-02-05 20:48:59 -08:00
};
const fetchOptions = {
2016-02-28 17:25:21 -08:00
fetchAction: 'fetchJobs',
2016-02-05 20:48:59 -08:00
getActionArgs({ params: { id } }) {
return [ id ];
},
isPrimed({ params: { id } = {}, job = {} }) {
return job.id === id;
},
// using es6 destructuring
shouldRefetch({ job }, { params: { id } }) {
return job.id !== id;
}
};
export class Show extends PureComponent {
static displayName = 'Show';
static propTypes = {
job: PropTypes.object,
isBackEndCert: PropTypes.bool,
isFrontEndCert: PropTypes.bool,
username: PropTypes.string
};
componentDidMount() {
const { job, push } = this.props;
// redirect user in client
if (!isJobValid(job)) {
push('/jobs');
}
}
render() {
const {
isBackEndCert,
isFrontEndCert,
job,
username
} = this.props;
if (!isJobValid(job)) {
return <JobNotFound />;
}
const isSignedIn = !!username;
const showApply = shouldShowApply(
job,
{ isFrontEndCert, isBackEndCert }
);
const message = generateMessage(
job,
{ isFrontEndCert, isBackEndCert, isSignedIn }
);
return (
<ShowJob
message={ message }
preview={ false }
showApply={ showApply }
{ ...this.props }/>
);
}
}
export default compose(
connect(mapStateToProps, bindableActions),
contain(fetchOptions)
)(Show);