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

389 lines
11 KiB
JavaScript
Raw Normal View History

2015-09-14 17:31:48 -07:00
import React, { PropTypes } from 'react';
2015-09-26 22:23:56 -07:00
import { History } from 'react-router';
2015-09-14 17:31:48 -07:00
import { contain } from 'thundercats-react';
2015-09-24 20:28:04 -07:00
import debugFactory from 'debug';
import dedent from 'dedent';
2015-09-24 20:28:04 -07:00
import { getDefaults } from '../utils';
import {
inHTMLData,
uriInSingleQuotedAttr
} from 'xss-filters';
2015-09-14 17:31:48 -07:00
import {
2015-09-23 13:31:27 -07:00
Button,
2015-09-14 17:31:48 -07:00
Col,
Input,
Row,
Panel
2015-09-14 17:31:48 -07:00
} from 'react-bootstrap';
2015-09-24 20:28:04 -07:00
2015-09-22 16:10:12 -07:00
import {
isAscii,
isEmail,
2015-09-22 17:26:53 -07:00
isURL
2015-09-22 16:10:12 -07:00
} from 'validator';
2015-09-14 17:31:48 -07:00
2015-09-24 20:28:04 -07:00
const debug = debugFactory('freecc:jobs:newForm');
const checkValidity = [
'position',
'locale',
'description',
'email',
'url',
'logo',
2015-10-15 22:30:07 -07:00
'company',
'isHighlighted'
2015-09-24 20:28:04 -07:00
];
2015-10-16 20:05:22 -07:00
const hightlightCopy = `
Highlight my post to make it stand out. (+$50)
`;
2015-10-20 15:01:05 -07:00
const foo = `
This will narrow the field substantially with higher quality applicants
`;
const isFullStackCopy = `
2015-10-20 15:01:05 -07:00
Applicants must have Free Code Camps Full Stack Certification to apply.*
`;
const isFrontEndCopy = `
2015-10-20 15:01:05 -07:00
Applicants must have Free Code Camps Front End Certification to apply.*
`;
const isRemoteCopy = `
2015-10-20 15:01:05 -07:00
This job can be performed remotely.
`;
const checkboxClass = dedent`
jobs-checkbox
jobs-checkbox-spacer
col-sm-offset-2
col-sm-6 col-md-offset-3
`;
function formatValue(value, validator, type = 'string') {
const formated = getDefaults(type);
if (validator && type === 'string') {
formated.valid = validator(value);
}
if (value) {
formated.value = value;
formated.bsStyle = formated.valid ? 'success' : 'error';
}
return formated;
}
function isValidURL(data) {
return isURL(data, { 'require_protocol': true });
}
2015-10-11 21:11:27 -07:00
function makeRequired(validator) {
return (val) => !!val && validator(val);
}
2015-09-14 17:31:48 -07:00
export default contain({
actions: 'jobActions',
store: 'jobsStore',
map({ form = {} }) {
const {
position,
locale,
2015-09-22 16:10:12 -07:00
description,
email,
2015-09-22 18:25:09 -07:00
url,
logo,
2015-10-15 22:30:07 -07:00
company,
isHighlighted,
isFullStackCert,
isFrontEndCert,
isRemoteOk
} = form;
return {
2015-10-11 21:11:27 -07:00
position: formatValue(position, makeRequired(isAscii)),
locale: formatValue(locale, makeRequired(isAscii)),
description: formatValue(description, makeRequired(isAscii)),
email: formatValue(email, makeRequired(isEmail)),
url: formatValue(url, isValidURL),
logo: formatValue(logo, isValidURL),
2015-10-15 22:30:07 -07:00
company: formatValue(company, makeRequired(isAscii)),
isHighlighted: formatValue(isHighlighted, null, 'bool'),
isFullStackCert: formatValue(isFullStackCert, null, 'bool'),
isFrontEndCert: formatValue(isFrontEndCert, null, 'bool'),
isRemoteOk: formatValue(isRemoteOk, null, 'bool')
};
2015-09-24 20:28:04 -07:00
},
subscribeOnWillMount() {
return typeof window !== 'undefined';
2015-09-14 17:31:48 -07:00
}
},
React.createClass({
displayName: 'NewJob',
2015-09-14 17:31:48 -07:00
propTypes: {
jobActions: PropTypes.object,
position: PropTypes.object,
locale: PropTypes.object,
2015-09-22 16:10:12 -07:00
description: PropTypes.object,
email: PropTypes.object,
2015-09-22 18:25:09 -07:00
url: PropTypes.object,
logo: PropTypes.object,
2015-10-15 22:30:07 -07:00
company: PropTypes.object,
isHighlighted: PropTypes.object,
isFullStackCert: PropTypes.object,
isFrontEndCert: PropTypes.object,
isRemoteOk: PropTypes.object
},
2015-09-26 22:23:56 -07:00
mixins: [History],
2015-09-24 20:28:04 -07:00
handleSubmit(e) {
e.preventDefault();
const props = this.props;
2015-09-24 20:28:04 -07:00
let valid = true;
checkValidity.forEach((prop) => {
// if value exist, check if it is valid
if (props[prop].value && props[prop].type !== 'boolean') {
valid = valid && !!props[prop].valid;
2015-09-24 20:28:04 -07:00
}
});
if (!valid) {
debug('form not valid');
return;
}
const {
jobActions,
// form values
2015-09-24 20:28:04 -07:00
position,
locale,
description,
email,
url,
logo,
2015-10-15 22:30:07 -07:00
company,
isHighlighted,
isFullStackCert,
isFrontEndCert,
isRemoteOk
2015-09-24 20:28:04 -07:00
} = this.props;
// sanitize user output
const jobValues = {
position: inHTMLData(position.value),
locale: inHTMLData(locale.value),
2015-09-24 20:28:04 -07:00
description: inHTMLData(description.value),
email: inHTMLData(email.value),
url: uriInSingleQuotedAttr(url.value),
logo: uriInSingleQuotedAttr(logo.value),
2015-10-15 22:30:07 -07:00
company: inHTMLData(company.value),
isHighlighted: !!isHighlighted.value,
isFrontEndCert: !!isFrontEndCert.value,
isFullStackCert: !!isFullStackCert.value,
isRemoteOk: !!isRemoteOk.value
2015-09-24 20:28:04 -07:00
};
const job = Object.keys(jobValues).reduce((accu, prop) => {
if (jobValues[prop]) {
accu[prop] = jobValues[prop];
}
return accu;
}, {});
2015-09-26 22:23:56 -07:00
job.postedOn = new Date();
2015-09-24 20:28:04 -07:00
debug('job sanitized', job);
jobActions.saveForm(job);
2015-09-26 22:23:56 -07:00
this.history.pushState(null, '/jobs/new/preview');
2015-09-24 20:28:04 -07:00
},
componentDidMount() {
const { jobActions } = this.props;
jobActions.getSavedForm();
},
handleChange(name, { target: { value } }) {
const { jobActions: { handleForm } } = this.props;
handleForm({ [name]: value });
2015-09-14 17:31:48 -07:00
},
2015-09-14 17:31:48 -07:00
render() {
const {
position,
locale,
2015-09-22 16:10:12 -07:00
description,
email,
2015-09-22 18:25:09 -07:00
url,
logo,
2015-10-15 22:30:07 -07:00
company,
isHighlighted,
isFrontEndCert,
isFullStackCert,
isRemoteOk,
jobActions: { handleForm }
} = this.props;
const { handleChange } = this;
const labelClass = 'col-sm-offset-1 col-sm-2';
const inputClass = 'col-sm-6';
2015-09-14 17:31:48 -07:00
return (
<div>
<Row>
<Col
md={ 10 }
mdOffset={ 1 }>
<Panel className='text-center'>
2015-09-22 16:10:12 -07:00
<h1>Create Your Job Post</h1>
2015-09-24 20:28:04 -07:00
<form
className='form-horizontal'
onSubmit={ this.handleSubmit }>
2015-09-22 18:25:09 -07:00
2015-09-23 13:31:27 -07:00
<div className='spacer'>
2015-10-16 16:12:01 -07:00
<h2>First, tell us about the position</h2>
2015-09-23 13:31:27 -07:00
</div>
<Input
bsStyle={ position.bsStyle }
2015-10-16 16:12:01 -07:00
label='Job Title'
2015-09-23 13:31:27 -07:00
labelClassName={ labelClass }
onChange={ (e) => handleChange('position', e) }
2015-10-16 20:05:22 -07:00
placeholder={
'e.g. Full Stack Developer, Front End Developer, etc.'
}
2015-10-11 21:11:27 -07:00
required={ true }
2015-09-23 13:31:27 -07:00
type='text'
value={ position.value }
wrapperClassName={ inputClass } />
<Input
bsStyle={ locale.bsStyle }
label='Location'
labelClassName={ labelClass }
onChange={ (e) => handleChange('locale', e) }
2015-10-16 16:12:01 -07:00
placeholder='e.g. San Francisco, Remote, etc.'
2015-10-11 21:11:27 -07:00
required={ true }
2015-09-23 13:31:27 -07:00
type='text'
value={ locale.value }
wrapperClassName={ inputClass } />
<Input
bsStyle={ description.bsStyle }
label='Description'
labelClassName={ labelClass }
onChange={ (e) => handleChange('description', e) }
2015-10-11 21:11:27 -07:00
required={ true }
2015-09-23 13:31:27 -07:00
rows='10'
type='textarea'
value={ description.value }
wrapperClassName={ inputClass } />
2015-10-20 15:01:05 -07:00
<Input
checked={ isFrontEndCert.value }
label={ isFrontEndCopy }
onChange={
({ target: { checked } }) => handleForm({
isFrontEndCert: !!checked
})
}
type='checkbox'
wrapperClassName={ checkboxClass } />
<Input
checked={ isFullStackCert.value }
label={ isFullStackCopy }
onChange={
({ target: { checked } }) => handleForm({
isFullStackCert: !!checked
})
}
type='checkbox'
wrapperClassName={ checkboxClass } />
<Input
checked={ isRemoteOk.value }
label={ isRemoteCopy }
onChange={
({ target: { checked } }) => handleForm({
isRemoteOk: !!checked
})
}
type='checkbox'
wrapperClassName={ checkboxClass } />
<Row>
<small>* { foo }</small>
</Row>
<div className='spacer' />
2015-09-23 13:31:27 -07:00
<div className='divider'>
2015-10-16 16:12:01 -07:00
<h2>Tell us about your organization</h2>
2015-09-23 13:31:27 -07:00
</div>
<Input
2015-10-15 22:30:07 -07:00
bsStyle={ company.bsStyle }
2015-09-23 13:31:27 -07:00
label='Company Name'
labelClassName={ labelClass }
2015-10-15 22:30:07 -07:00
onChange={ (e) => handleChange('company', e) }
2015-09-23 13:31:27 -07:00
type='text'
2015-10-15 22:30:07 -07:00
value={ company.value }
2015-09-23 13:31:27 -07:00
wrapperClassName={ inputClass } />
<Input
bsStyle={ email.bsStyle }
label='Email'
labelClassName={ labelClass }
onChange={ (e) => handleChange('email', e) }
2015-10-20 15:01:05 -07:00
placeholder='This is how we will contact you'
2015-10-11 21:11:27 -07:00
required={ true }
2015-09-23 13:31:27 -07:00
type='email'
value={ email.value }
wrapperClassName={ inputClass } />
<Input
bsStyle={ url.bsStyle }
label='URL'
labelClassName={ labelClass }
onChange={ (e) => handleChange('url', e) }
2015-10-20 15:01:05 -07:00
placeholder='http://yourcompany.com'
2015-09-23 13:31:27 -07:00
type='url'
value={ url.value }
wrapperClassName={ inputClass } />
<Input
bsStyle={ logo.bsStyle }
label='Logo'
labelClassName={ labelClass }
onChange={ (e) => handleChange('logo', e) }
2015-10-20 15:01:05 -07:00
placeholder='http://yourcompany.com/logo.png'
2015-09-23 13:31:27 -07:00
type='url'
value={ logo.value }
wrapperClassName={ inputClass } />
2015-09-22 18:25:09 -07:00
2015-10-20 15:01:05 -07:00
<div className='spacer' />
2015-09-23 13:31:27 -07:00
<Input
checked={ isHighlighted.value }
2015-10-16 20:05:22 -07:00
label={ hightlightCopy }
onChange={
({ target: { checked } }) => handleForm({
isHighlighted: !!checked
})
}
2015-10-16 20:05:22 -07:00
type='checkbox'
wrapperClassName={ checkboxClass } />
2015-09-23 13:31:27 -07:00
<div className='spacer' />
<Row>
<Col
lg={ 6 }
lgOffset={ 3 }>
<Button
block={ true }
2015-09-24 20:28:04 -07:00
bsSize='large'
bsStyle='primary'
type='submit'>
2015-09-23 13:31:27 -07:00
Preview My Ad
</Button>
</Col>
</Row>
2015-09-14 17:31:48 -07:00
</form>
</Panel>
2015-09-14 17:31:48 -07:00
</Col>
</Row>
</div>
);
}
})
);