Newer
Older
import moment from 'moment'
import { get, find, capitalize, omit, isEmpty, isEqual, debounce } from 'lodash'
import {
autosaveRequest,
autosaveSuccess,
} from 'pubsweet-component-wizard/src/redux/autosave'
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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 => ({
author: parseAuthor(version),
title: parseTitle(version),
submitted: parseSubmissionDate(version),
type: parseType(version),
abstract: get(version, 'metadata.abstract'),
version: get(version, 'version'),
})
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} - updated on ${moment(f.submitted).format(
'DD.MM.YYYY',
)}`,
}))
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 = {}) => {
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 = {}) => {
const comments = [
{
public: true,
files: review.files || [],
},
]
if (review.hasConfidential) {
comments.push({
public: false,
content: review.confidential,
files: [],
})
}
return {
...omit(review, [
'public',
'confidential',
'hasConfidential',
'files',
'userId',
]),
recommendationType: 'review',
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const onChange = (
values,
dispatch,
{ project, version, createRecommendation, updateRecommendation },
previousValues,
) => {
const newValues = parseReviewRequest(values)
const prevValues = parseReviewRequest(previousValues)
if (!isEqual(newValues, prevValues) && !isEmpty(prevValues)) {
dispatch(autosaveRequest())
if (newValues.id) {
updateRecommendation(project.id, version.id, newValues).then(r =>
dispatch(autosaveSuccess(r.updatedOn)),
)
} else {
createRecommendation(project.id, version.id, newValues).then(r =>
dispatch(autosaveSuccess(r.updatedOn)),
)
}
}
}
export const onReviewChange = debounce(onChange, 1000, { maxWait: 5000 })
export const onReviewSubmit = (
values,
dispatch,
{
isSubmitting,
showModal,
hideModal,
project,
version,
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(r.updatedOn)))
.then(hideModal)
},
onCancel: hideModal,
})
}