Fix job saving on form submit
parent
5dab7fddbc
commit
15c9417911
|
@ -1,4 +1,4 @@
|
|||
import { helpers } from 'rx';
|
||||
import { CompositeDisposable, helpers } from 'rx';
|
||||
import React, { PropTypes } from 'react';
|
||||
import { reduxForm } from 'redux-form';
|
||||
// import debug from 'debug';
|
||||
|
@ -70,14 +70,17 @@ const fieldValidators = {
|
|||
locale: makeRequired(isAscii),
|
||||
description: makeRequired(helpers.identity),
|
||||
email: makeRequired(isEmail),
|
||||
url: isValidURL,
|
||||
logo: isValidURL,
|
||||
url: makeRequired(isValidURL),
|
||||
logo: makeOptional(isValidURL),
|
||||
company: makeRequired(isAscii),
|
||||
howToApply: makeRequired(isAscii)
|
||||
};
|
||||
|
||||
function makeOptional(validator) {
|
||||
return val => val ? validator(val) : true;
|
||||
}
|
||||
function makeRequired(validator) {
|
||||
return (val) => !!val && validator(val);
|
||||
return (val) => val ? validator(val) : false;
|
||||
}
|
||||
|
||||
function validateForm(values) {
|
||||
|
@ -86,7 +89,7 @@ function validateForm(values) {
|
|||
if (fieldValidators[field](values[field])) {
|
||||
return null;
|
||||
}
|
||||
return { [field]: fieldValidators[field](values[field]) };
|
||||
return { [field]: !fieldValidators[field](values[field]) };
|
||||
})
|
||||
.filter(Boolean)
|
||||
.reduce((errors, error) => ({ ...errors, ...error }), {});
|
||||
|
@ -103,17 +106,32 @@ function getBsStyle(field) {
|
|||
}
|
||||
|
||||
export class NewJob extends React.Component {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
this._subscriptions = new CompositeDisposable();
|
||||
}
|
||||
|
||||
static displayName = 'NewJob';
|
||||
|
||||
static propTypes = {
|
||||
fields: PropTypes.object,
|
||||
handleSubmit: PropTypes.func
|
||||
handleSubmit: PropTypes.func,
|
||||
saveJob: PropTypes.func
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
// this.prop.getSavedForm();
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
this._subscriptions.dispose();
|
||||
}
|
||||
|
||||
handleSubmit(job) {
|
||||
const subscription = this.props.saveJob(job).subscribe();
|
||||
this._subscriptions.add(subscription);
|
||||
}
|
||||
|
||||
handleCertClick(name) {
|
||||
const { fields } = this.props;
|
||||
Object.keys(certTypes).forEach(certType => {
|
||||
|
@ -372,6 +390,6 @@ export default reduxForm(
|
|||
},
|
||||
null,
|
||||
{
|
||||
onSubmit: saveJob
|
||||
saveJob
|
||||
}
|
||||
)(NewJob);
|
||||
|
|
|
@ -4,5 +4,6 @@ export types from './types';
|
|||
|
||||
import fetchJobsSaga from './fetch-jobs-saga';
|
||||
import saveJobSaga from './save-job-saga';
|
||||
export formNormalizer from './jobs-form-normalizer';
|
||||
|
||||
export const sagas = [ fetchJobsSaga, saveJobSaga ];
|
||||
|
|
|
@ -8,7 +8,11 @@ const normalizeOptions = {
|
|||
stripWWW: false
|
||||
};
|
||||
|
||||
function formatUrl(url, shouldKeepTrailingSlash = true) {
|
||||
function ifDefinedNormalize(normalizer) {
|
||||
return value => value ? normalizer(value) : value;
|
||||
}
|
||||
|
||||
function formatUrl(url) {
|
||||
if (
|
||||
typeof url === 'string' &&
|
||||
url.length > 4 &&
|
||||
|
@ -16,7 +20,7 @@ function formatUrl(url, shouldKeepTrailingSlash = true) {
|
|||
) {
|
||||
// prevent trailing / from being stripped during typing
|
||||
let lastChar = '';
|
||||
if (shouldKeepTrailingSlash && url.substring(url.length - 1) === '/') {
|
||||
if (url.substring(url.length - 1) === '/') {
|
||||
lastChar = '/';
|
||||
}
|
||||
return normalizeUrl(url, normalizeOptions) + lastChar;
|
||||
|
@ -26,13 +30,13 @@ function formatUrl(url, shouldKeepTrailingSlash = true) {
|
|||
|
||||
export default {
|
||||
NewJob: {
|
||||
position: inHTMLData,
|
||||
locale: inHTMLData,
|
||||
description: inHTMLData,
|
||||
email: inHTMLData,
|
||||
url: value => formatUrl(uriInSingleQuotedAttr(value)),
|
||||
logo: value => formatUrl(uriInSingleQuotedAttr(value)),
|
||||
company: inHTMLData,
|
||||
howToApply: inHTMLData
|
||||
position: ifDefinedNormalize(inHTMLData),
|
||||
locale: ifDefinedNormalize(inHTMLData),
|
||||
description: ifDefinedNormalize(inHTMLData),
|
||||
email: ifDefinedNormalize(inHTMLData),
|
||||
url: ifDefinedNormalize(value => formatUrl(uriInSingleQuotedAttr(value))),
|
||||
logo: ifDefinedNormalize(value => formatUrl(uriInSingleQuotedAttr(value))),
|
||||
company: ifDefinedNormalize(inHTMLData),
|
||||
howToApply: ifDefinedNormalize(inHTMLData)
|
||||
}
|
||||
};
|
||||
|
|
|
@ -4,6 +4,7 @@ const types = [
|
|||
|
||||
'findJob',
|
||||
'saveJob',
|
||||
'saveJobCompleted',
|
||||
|
||||
'saveForm',
|
||||
'clearForm',
|
||||
|
|
|
@ -32,7 +32,7 @@ export default stampit({
|
|||
});
|
||||
},
|
||||
createService$({ service: resource, params, body, config }) {
|
||||
return Observable.create(function(observer) {
|
||||
return Observable.create(observer => {
|
||||
this.services.create(
|
||||
resource,
|
||||
params,
|
||||
|
|
Loading…
Reference in New Issue