Newer
Older
import { SubmissionError } from 'redux-form'
import { get, isBoolean, isEmpty, debounce, isEqual, pick, omit } from 'lodash'
export const validate = (values, props) => {
const errors = {}
if (isEmpty(get(values, 'authors', []))) {
errors.authors = 'Authors are required.'
}
if (!isBoolean(values.authorForm) && values.authorForm > -1) {
errors.authors = 'You have an unsaved author.'
}
if (isBoolean(values.authorForm) && values.authorForm) {
errors.authors = 'Finish or cancel adding a new author.'
}
if (isEmpty(get(values, 'files.manuscripts', []))) {
errors.files = 'At least one manuscript file is required.'
}
return errors
}
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const _onChange = (
values,
dispatch,
{ project, version, autosaveRequest, updateFragment },
) => {
const previousValues = pick(version, ['metadata', 'authors', 'files'])
const newValues = omit(values, ['agree', 'authorForm'])
if (!isEqual(newValues, previousValues)) {
autosaveRequest()
updateFragment(project, {
...version,
...newValues,
})
}
}
export const onChange = debounce(_onChange, 1000, { maxWait: 5000 })
export const onSubmit = (
values,
dispatch,
{
step,
history,
nextStep,
submitManuscript,
version: { id: fragmentId },
project: { id: collectionId, customId },
},
) => {
if (step !== 2) {
nextStep()
} else {
submitManuscript(collectionId, fragmentId)
.then(r => {
history.push('/confirmation-page', {
customId,
version: fragmentId,
project: collectionId,
})
})
.catch(err => {
if (err.validationErrors) {
throw new SubmissionError()
}
})
}
}