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

136 lines
3.1 KiB
JavaScript
Raw Normal View History

import React, { PropTypes } from 'react';
2015-10-19 22:51:30 -07:00
import { History } from 'react-router';
2015-08-29 01:00:52 -07:00
import { contain } from 'thundercats-react';
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,
isFullStackCert: isFullStackCertReq = false
}, {
isFrontEndCert = false,
isFullStackCert = false
}
) {
return (!isFrontEndCertReq && !isFullStackCertReq) ||
(isFullStackCertReq && isFullStackCert) ||
(isFrontEndCertReq && isFrontEndCert);
}
function generateMessage(
{
isFrontEndCert: isFrontEndCertReq = false,
isFullStackCert: isFullStackCertReq = false
},
{
2015-11-05 12:36:02 -08:00
isFrontEndCert = false,
isFullStackCert = 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 (isFullStackCertReq && !isFullStackCert) {
return 'This employer requires Free Code Camps Full ' +
'Stack 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 Full Stack Development Certification. ' +
"You've earned it, so feel free to apply.";
2015-11-05 12:36:02 -08:00
}
2015-08-29 01:00:52 -07:00
export default contain(
{
2015-11-05 12:36:02 -08:00
stores: ['appStore', 'jobsStore'],
fetchWaitFor: 'jobsStore',
2015-08-29 01:00:52 -07:00
fetchAction: 'jobActions.getJob',
combineLatest(
{ username, isFrontEndCert, isFullStackCert },
{ currentJob }
) {
2015-11-05 12:36:02 -08:00
return {
username,
2015-11-05 12:36:02 -08:00
job: currentJob,
isFrontEndCert,
isFullStackCert
};
},
getPayload({ params: { id }, job = {} }) {
2015-08-31 14:32:31 -07:00
return {
id,
isPrimed: job.id === id
2015-08-31 14:32:31 -07:00
};
},
// using es6 destructuring
shouldContainerFetch({ job = {} }, { params: { id } }
) {
return job.id !== id;
2015-08-29 01:00:52 -07:00
}
2015-08-25 14:36:07 -07:00
},
React.createClass({
2015-10-19 22:51:30 -07:00
displayName: 'Show',
propTypes: {
job: PropTypes.object,
isFullStackCert: PropTypes.bool,
isFrontEndCert: PropTypes.bool,
username: PropTypes.string
},
2015-10-19 22:51:30 -07:00
mixins: [History],
componentDidMount() {
const { job } = this.props;
// redirect user in client
if (!isJobValid(job)) {
this.history.pushState(null, '/jobs');
}
},
render() {
2015-11-05 12:36:02 -08:00
const {
isFullStackCert,
isFrontEndCert,
job,
username
} = this.props;
2015-10-19 22:51:30 -07:00
if (!isJobValid(job)) {
return <JobNotFound />;
}
const isSignedIn = !!username;
const showApply = shouldShowApply(
job,
{ isFrontEndCert, isFullStackCert }
);
const message = generateMessage(
job,
{ isFrontEndCert, isFullStackCert, isSignedIn }
);
2015-11-05 12:36:02 -08:00
return (
<ShowJob
message={ message }
preview={ false }
showApply={ showApply }
2015-11-05 12:36:02 -08:00
{ ...this.props }/>
);
2015-10-19 22:51:30 -07:00
}
})
2015-08-29 01:00:52 -07:00
);