import moment from 'moment' import { get, find, capitalize, omit, isEmpty, debounce } from 'lodash' import { actions } from 'pubsweet-client/src' import { change as changeForm } from 'redux-form' import { autosaveRequest, autosaveSuccess, autosaveFailure, } from 'pubsweet-component-wizard/src/redux/autosave' export const parseTitle = version => { const title = get(version, 'metadata.title') if (title) { return title.replace(/<p[^>]*>/g, '').replace(/<\/p>/g, '') } return 'Untitled' } export const parseAuthor = version => { const author = find(get(version, 'authors'), a => a.isSubmitting) return author ? `${author.firstName} ${author.lastName}` : 'N/A' } export const parseType = version => { const type = get(version, 'metadata.type') return type ? type.replace('-', ' ') : 'N/A' } export const parseSubmissionDate = version => { const submitted = get(version, 'submitted') const submittedDate = moment(submitted) const today = moment() const daysAgo = moment.duration(today - moment(submitted)).days() return submitted ? `${submittedDate.format('DD.MM.YYYY')} ${ daysAgo > 0 ? `(${daysAgo} days)` : '' }` : 'N/A' } export const mapStatusToLabel = ({ visibleStatus, status }) => { if (visibleStatus) { return visibleStatus } else if (status) { return capitalize(status) } return 'Draft' } export const parseVersion = version => ({ type: parseType(version), title: parseTitle(version), author: parseAuthor(version), version: get(version, 'version'), submitted: parseSubmissionDate(version), abstract: get(version, 'metadata.abstract'), }) export const parseJournalIssue = (journal, metadata) => journal.issueTypes.find(t => t.value === get(metadata, 'issue')) export const parseSearchParams = url => { const params = new URLSearchParams(url) const parsedObject = {} /* eslint-disable */ for ([key, value] of params) { parsedObject[key] = value } /* eslint-enable */ return parsedObject } export const parseVersionOptions = (fragments = []) => fragments.map(f => ({ value: f.id, label: `Version ${f.version}`, })) const alreadyAnswered = `You have already answered this invitation.` export const redirectToError = redirectFn => err => { const errorText = get(JSON.parse(err.response), 'error') if (errorText.includes('has already been answered')) { redirectFn('/error-page', alreadyAnswered) } else { redirectFn('/error-page', 'Oops! Something went wrong.') } } export const parseReviewResponseToForm = (review = {}) => { if (isEmpty(review)) return {} const comments = review.comments || [] const publicComment = comments.find(c => c.public) const privateComment = comments.find(c => !c.public) return { ...review, public: get(publicComment, 'content'), files: get(publicComment, 'files'), confidential: get(privateComment, 'content'), hasConfidential: !!get(privateComment, 'content'), } } export const parseReviewRequest = (review = {}) => { if (isEmpty(review)) return {} const comments = [ { public: true, content: review.public || undefined, files: review.files || [], }, ] if (review.hasConfidential) { comments.push({ public: false, content: review.confidential || undefined, files: [], }) } return { ...omit(review, [ 'public', 'confidential', 'hasConfidential', 'files', 'userId', ]), recommendationType: 'review', comments, } } const onChange = ( values, dispatch, { project, version, createRecommendation, updateRecommendation }, ) => { const newValues = parseReviewRequest(values) // if (!isEqual(newValues, prevValues)) { dispatch(autosaveRequest()) if (newValues.id) { updateRecommendation(project.id, version.id, newValues) .then(r => dispatch(autosaveSuccess(get(r, 'updatedOn')))) .catch(e => dispatch(autosaveFailure(e))) } else { createRecommendation(project.id, version.id, newValues) .then(r => { dispatch(changeForm('reviewerReport', 'id', r.id)) return dispatch(autosaveSuccess(get(r, 'updatedOn'))) }) .catch(e => dispatch(autosaveFailure(e))) } // } } export const onReviewChange = debounce(onChange, 1000, { maxWait: 5000 }) export const onReviewSubmit = ( values, dispatch, { project, version, showModal, hideModal, isSubmitting, updateRecommendation, }, ) => { showModal({ title: 'Ready to Submit your Report?', subtitle: 'Once submitted, the report can`t be modified', confirmText: 'Submit report', onConfirm: () => { const newValues = parseReviewRequest(values) newValues.submittedOn = Date.now() dispatch(autosaveRequest()) updateRecommendation(project.id, version.id, newValues) .then(r => dispatch(autosaveSuccess(get(r, 'updatedOn')))) .then(() => { dispatch(actions.getFragments({ id: project.id })) hideModal() }) }, onCancel: hideModal, }) } const _onRevisionChange = (values, dispatch, { project, version }) => { const newValues = omit(values, 'authorForm') dispatch( actions.updateFragment(project, { ...version, revision: newValues, }), ) } export const onRevisionChange = debounce(_onRevisionChange, 1000, { maxWait: 5000, }) export const onRevisionSubmit = ( values, dispatch, { history, version, project, showModal, hideModal, setModalError, submitRevision, }, ) => { showModal({ title: 'Ready to submit your revision?', subtitle: `Once submitted, the submission can't be modified.`, onConfirm: () => { submitRevision(project.id, version.id) .then(r => { dispatch(actions.getFragments({ id: project.id })) return r }) .then(r => { history.push(`/projects/${r.collectionId}/version/${r.id}/details`) hideModal() }) .catch(e => setModalError('Something went wrong.')) }, onCancel: hideModal, }) } // revision validators export const requiredHTML = value => { if (value && value.replace('<p></p>', '').replace('<h1></h1>', '')) { return undefined } return 'Required' } export const requiredFiles = (values, formValues) => { const manuscripts = get(formValues, 'files.manuscripts') if (!manuscripts || manuscripts.length === 0) { return 'At least one main manuscript file is needed.' } return undefined }