MetaSec.jsx 13.1 KB
Newer Older
Audrey Hamelers's avatar
WIP #15    
Audrey Hamelers committed
1
import React from 'react'
ahamelers's avatar
#773    
ahamelers committed
2
import { Query, compose, graphql } from 'react-apollo'
3
import { Icon, Action, Button, H2 } from '@pubsweet/ui'
Audrey Hamelers's avatar
WIP #15    
Audrey Hamelers committed
4
5
6
import { th } from '@pubsweet/ui-toolkit'
import styled from 'styled-components'
import moment from 'moment'
ahamelers's avatar
#773    
ahamelers committed
7
import { Notification } from '../ui'
Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
8
9
10
11
12
13
import {
  CHECK_DUPES,
  CLAIM_MANUSCRIPT,
  UNCLAIM_MANUSCRIPT,
} from '../operations'
import { UserContext } from '../App'
14
import MetaEdit from './MetaEdit'
ahamelers's avatar
#773    
ahamelers committed
15
import { QUERY_ACTIVITY_INFO, LINK_AND_DELETE } from './operations'
Audrey Hamelers's avatar
WIP #15    
Audrey Hamelers committed
16

Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
17
const Heading = styled.div`
ahamelers's avatar
#17    
ahamelers committed
18
  border-bottom: ${th('borderWidth')} ${th('borderStyle')} ${th('colorBorder')};
Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
19
  margin: calc(${th('gridUnit')} * 3) 0px calc(${th('gridUnit')} * 2);
ahamelers's avatar
#17    
ahamelers committed
20
  padding-bottom: ${th('gridUnit')};
Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  h2 {
    display: inline-block;
    margin: 0;
  }
  dl {
    display: inline-block;
    margin-left: calc(${th('gridUnit')} * 2);
  }
`
const ClaimAction = styled(Button)`
  font-size: ${th('fontSizeBaseSmall')};
  line-height: ${th('fontSize')};
  padding: 0 calc(${th('gridUnit')} * 1.5);
  margin-left: calc(${th('gridUnit')} * 2);
  margin-bottom: 0;
  vertical-align: text-bottom;
  min-width: 0;
ahamelers's avatar
#17    
ahamelers committed
38
`
Audrey Hamelers's avatar
WIP #15    
Audrey Hamelers committed
39
40
41
42
43
44
45
46
47
48
49
const Container = styled.div`
  margin: 0 0 calc(${th('gridUnit')} * 2);
  column-count: 3;
  @media screen and (max-width: 1000px) {
    column-count: 2;
  }
  @media screen and (max-width: 600px) {
    column-count: 1;
  }
`
const DL = styled.dl`
50
  margin: 0 0 ${th('gridUnit')};
Audrey Hamelers's avatar
WIP #15    
Audrey Hamelers committed
51
52
53
54
55
56
57
58
  dt {
    font-weight: 600;
    margin-right: ${th('gridUnit')};
    display: inline;
  }
  dd {
    margin: 0;
    display: inline;
59
60
61
    button {
      margin-left: calc(${th('gridUnit')} / 2);
    }
Audrey Hamelers's avatar
WIP #15    
Audrey Hamelers committed
62
63
  }
`
64
65
66
67
const DupeButton = styled(Button)`
  padding: calc(${th('gridUnit')} / 2) ${th('gridUnit')};
  font-size: ${th('fontSizeBaseSmall')};
`
Audrey Hamelers's avatar
#847    
Audrey Hamelers committed
68
export const EditIcon = () => (
69
70
  <Icon color="currentColor" size={1.9} style={{ verticalAlign: 'bottom' }}>
    edit-3
Audrey Hamelers's avatar
WIP #15    
Audrey Hamelers committed
71
72
73
  </Icon>
)

74
class MetaSec extends React.Component {
Audrey Hamelers's avatar
#945    
Audrey Hamelers committed
75
  state = { edit: null, notif: null }
ahamelers's avatar
#773    
ahamelers committed
76
77
78
79
80
  componentDidMount() {
    this._mounted = true
    this.notifTimer = null
  }
  async componentDidUpdate() {
Audrey Hamelers's avatar
#945    
Audrey Hamelers committed
81
    const { notif } = this.state
ahamelers's avatar
#773    
ahamelers committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    if (this.notifTimer) {
      clearTimeout(this.notifTimer)
    }
    if (notif && notif.type !== 'error') {
      this.notifTimer = setTimeout(() => {
        if (this._mounted) {
          this.setState({ notif: null })
        }
      }, 3000)
    }
  }
  componentWillUnmount() {
    this._mounted = false
    clearTimeout(this.notifTimer)
  }
Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
97
  static contextType = UserContext
98
  render() {
Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
99
    const currentUser = this.context
Audrey Hamelers's avatar
#945    
Audrey Hamelers committed
100
101
102
103
104
105
    const {
      manuscript,
      claimManuscript,
      unclaimManuscript,
      linkDeleteManuscript,
    } = this.props
Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
106
    const { teams, audits, meta, journal, claiming } = manuscript
107
108
109
110
111
112
113
114
    const {
      fundingGroup,
      releaseDelay,
      articleIds,
      volume,
      issue,
      location,
      publicationDates,
115
116
      title,
      notes,
117
    } = meta
118
    let duplicates = []
119
    const pmid = articleIds && articleIds.find(aid => aid.pubIdType === 'pmid')
120
121
    const pmcid =
      articleIds && articleIds.find(aid => aid.pubIdType === 'pmcid')
122
123
124
125
126
127
128
129
    const submitter = teams.find(team => team.role === 'submitter')
    const { title: st, givenNames: sg, surname: ss } = submitter
      ? submitter.teamMembers[0].alias.name
      : ''
    const reviewer = teams.find(team => team.role === 'reviewer')
    const { title: rt, givenNames: rg, surname: rs } = reviewer
      ? reviewer.teamMembers[0].alias.name
      : ''
Audrey Hamelers's avatar
#476    
Audrey Hamelers committed
130
131
132
133
134
    const reviewerNote =
      notes &&
      notes.find(n => n.notesType === 'selectedReviewer') &&
      JSON.parse(notes.find(n => n.notesType === 'selectedReviewer').content)
    const rnName = reviewerNote && reviewerNote.name
135
136
137
138
139
140
141
    const auditInfo = audits.reduce((info, audit) => {
      if (!info.timeInProcess) {
        info.timeInProcess = moment(audit.created).fromNow(true)
      }
      info.lastActivity = moment(audit.created).fromNow(true)
      return info
    }, {})
ahamelers's avatar
ahamelers committed
142
143
144
145
146
147
    const lastStatusChange = audits.reduce((log, current) => {
      if (Object.keys(current.changes).includes('status')) {
        return current
      }
      return log
    }, {})
148
    const year =
149
150
151
152
153
154
155
156
157
158
      publicationDates &&
      publicationDates.reduce((retYear, date) => {
        const getYear = date.jatsDate
          ? date.jatsDate.year
          : moment(date.date).year()
        if (date.type === 'ppub' || !retYear) {
          return getYear
        }
        return retYear
      }, null)
ahamelers's avatar
#773    
ahamelers committed
159
    const { edit, notif } = this.state
160
    return (
ahamelers's avatar
#17    
ahamelers committed
161
      <React.Fragment>
ahamelers's avatar
#773    
ahamelers committed
162
163
164
165
166
167
        {notif && (
          <React.Fragment>
            <br />
            <Notification type={notif.type}>{notif.message}</Notification>
          </React.Fragment>
        )}
ahamelers's avatar
#17    
ahamelers committed
168
169
170
        <DL style={{ float: 'right', marginTop: '1rem' }}>
          <dt>Status:</dt>
          <dd>
Audrey Hamelers's avatar
#432    
Audrey Hamelers committed
171
172
173
174
175
            {manuscript.deleted ? (
              <del>{manuscript.status}</del>
            ) : (
              manuscript.status
            )}
ahamelers's avatar
#17    
ahamelers committed
176
177
178
179
180
181
182
            <Action onClick={() => this.setState({ edit: 'status' })}>
              <Icon color="currentColor" size={1.75}>
                send
              </Icon>
            </Action>
          </dd>
        </DL>
Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
183
184
185
186
187
188
        <Heading>
          <H2>Quick view</H2>
          {claiming ? (
            <DL>
              <dt>Claimed:</dt>
              <dd>
Audrey Hamelers's avatar
#854    
Audrey Hamelers committed
189
                {claiming.givenNames}
Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
                {currentUser.id === claiming.id && (
                  <Action
                    onClick={async () =>
                      unclaimManuscript({
                        variables: { id: manuscript.id },
                      })
                    }
                  >
                    <Icon color="currentColor" size={1.9}>
                      x
                    </Icon>
                  </Action>
                )}
              </dd>
            </DL>
          ) : (
            <ClaimAction
              onClick={async () =>
                claimManuscript({
                  variables: { id: manuscript.id },
                })
              }
            >
              Claim
            </ClaimAction>
          )}
        </Heading>
ahamelers's avatar
#17    
ahamelers committed
217
        <Container>
Audrey Hamelers's avatar
#476    
Audrey Hamelers committed
218
          {reviewerNote ? (
ahamelers's avatar
#17    
ahamelers committed
219
            <React.Fragment>
220
              <DL>
ahamelers's avatar
#17    
ahamelers committed
221
                <dt>Submitter:</dt>
Audrey Hamelers's avatar
Audrey Hamelers committed
222
                <dd>{`${st ? `${st} ` : ''}${sg} ${ss}`}</dd>
223
              </DL>
Audrey Hamelers's avatar
#476    
Audrey Hamelers committed
224
              {rnName && (
ahamelers's avatar
#17    
ahamelers committed
225
                <DL>
Audrey Hamelers's avatar
#476    
Audrey Hamelers committed
226
227
228
229
230
231
232
                  <dt>Invited reviewer:</dt>
                  <dd>
                    {`${rnName.givenNames} ${rnName.surname}`}
                    <Action onClick={() => this.setState({ edit: 'reviewer' })}>
                      <EditIcon />
                    </Action>
                  </dd>
ahamelers's avatar
#17    
ahamelers committed
233
234
235
                </DL>
              )}
            </React.Fragment>
Audrey Hamelers's avatar
#476    
Audrey Hamelers committed
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
          ) : (
            <React.Fragment>
              {reviewer &&
              reviewer.teamMembers[0].user.id ===
                submitter.teamMembers[0].user.id ? (
                <DL>
                  <dt>Submitter/Reviewer:</dt>
                  <dd>
                    {`${st ? `${st} ` : ''}${sg} ${ss}`}
                    <Action onClick={() => this.setState({ edit: 'reviewer' })}>
                      <EditIcon />
                    </Action>
                  </dd>
                </DL>
              ) : (
                <React.Fragment>
                  <DL>
                    <dt>Submitter:</dt>
                    <dd>{`${st ? `${st} ` : ''}${sg} ${ss}`}</dd>
                  </DL>
                  {reviewer && (
                    <DL>
                      <dt>Reviewer:</dt>
                      <dd>
                        {`${rt ? `${rt} ` : ''}${rg} ${rs}`}
                        <Action
                          onClick={() => this.setState({ edit: 'reviewer' })}
                        >
                          <EditIcon />
                        </Action>
                      </dd>
                    </DL>
                  )}
                </React.Fragment>
              )}
            </React.Fragment>
ahamelers's avatar
#17    
ahamelers committed
272
          )}
273
          <DL>
ahamelers's avatar
#17    
ahamelers committed
274
275
            <dt>Time in process:</dt>
            <dd>{auditInfo.timeInProcess || 'N/A'}</dd>
276
277
          </DL>
          <DL>
ahamelers's avatar
#17    
ahamelers committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
            <dt>Last activity:</dt>
            <dd>{`${auditInfo.lastActivity} ago` || 'N/A'}</dd>
          </DL>
          {fundingGroup && (
            <DL>
              <dt>Grants:</dt>
              <dd>
                {fundingGroup.map((f, t) => (
                  <span key={f.awardId}>
                    {`${f.fundingSource} ${f.awardId}`}
                    {t !== fundingGroup.length - 1 && ', '}
                  </span>
                ))}
                <Action onClick={() => this.setState({ edit: 'grants' })}>
                  <EditIcon />
                </Action>
              </dd>
            </DL>
          )}
          {releaseDelay && (
            <DL>
              <dt>Embargo:</dt>
              <dd>
                {releaseDelay} month{releaseDelay !== '1' && 's'}
                <Action onClick={() => this.setState({ edit: 'embargo' })}>
                  <EditIcon />
                </Action>
              </dd>
            </DL>
          )}
          <DL>
            <dt>Citation:</dt>
310
            <dd>
ahamelers's avatar
#17    
ahamelers committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
              {(journal && journal.meta.nlmta) || 'Unmatched'}
              {year && `. ${year}`}
              {volume && `, ${volume}`}
              {issue && `(${issue})`}
              {location && location.fpage ? (
                <React.Fragment>
                  {`:${location.fpage}`}
                  {location.lpage && `-${location.lpage}`}.
                </React.Fragment>
              ) : (
                <React.Fragment>
                  {location &&
                    location.elocationId &&
                    `:${location.elocationId}.`}
                </React.Fragment>
              )}
327
328
              {pmid && ` PMID: ${pmid.id}.`}
              {pmcid && ` PMCID: ${pmcid.id}.`}
ahamelers's avatar
#17    
ahamelers committed
329
              <Action onClick={() => this.setState({ edit: 'citation' })}>
330
331
332
333
                <EditIcon />
              </Action>
            </dd>
          </DL>
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
          <Query
            fetchPolicy="cache-and-network"
            query={CHECK_DUPES}
            variables={{
              id: manuscript.id,
              articleIds: articleIds ? articleIds.map(aid => aid.id) : null,
              title,
            }}
          >
            {({ data, loading }) => {
              if (loading || !data || !data.checkDuplicates) {
                return '...'
              }
              const { manuscripts } = data.checkDuplicates
              const dupeNote = notes
                ? notes.find(n => n.notesType === 'notDuplicates')
                : null
              const notDupes = dupeNote ? JSON.parse(dupeNote.content) : []
              duplicates = manuscripts.filter(m => !notDupes.includes(m.id))
              return (
                <React.Fragment>
                  {duplicates && duplicates.length > 0 && (
                    <DupeButton
                      onClick={() => this.setState({ edit: 'dupes' })}
                      primary
                    >
                      Resolve duplicates
                    </DupeButton>
                  )}
                  {edit && (
                    <MetaEdit
                      close={() => this.setState({ edit: null })}
Audrey Hamelers's avatar
#476    
Audrey Hamelers committed
366
                      currentUser={currentUser}
367
                      duplicates={duplicates}
ahamelers's avatar
ahamelers committed
368
369
370
371
372
                      lastStatus={
                        lastStatusChange &&
                        lastStatusChange.originalData &&
                        lastStatusChange.originalData.status
                      }
Audrey Hamelers's avatar
#945    
Audrey Hamelers committed
373
374
375
376
377
378
379
380
381
382
383
384
                      linkAndDelete={async v => {
                        const { data } = await linkDeleteManuscript({
                          variables: { id: manuscript.id },
                        })
                        const result = data.linkDeleteManuscript
                        this.setState({
                          notif: {
                            type: result.success ? 'success' : 'error',
                            message: result.message,
                          },
                        })
                      }}
385
386
387
388
389
390
391
392
393
394
395
396
397
398
                      manuscript={manuscript}
                      refetch={[
                        {
                          query: QUERY_ACTIVITY_INFO,
                          variables: { id: manuscript.id },
                        },
                      ]}
                      toEdit={edit}
                    />
                  )}
                </React.Fragment>
              )
            }}
          </Query>
ahamelers's avatar
#17    
ahamelers committed
399
400
        </Container>
      </React.Fragment>
401
402
    )
  }
Audrey Hamelers's avatar
WIP #15    
Audrey Hamelers committed
403
404
}

ahamelers's avatar
#773    
ahamelers committed
405
406
407
408
409
410
411
412
413
export default compose(
  graphql(LINK_AND_DELETE, {
    name: 'linkDeleteManuscript',
    options: props => ({
      refetchQueries: [
        { query: QUERY_ACTIVITY_INFO, variables: { id: props.manuscript.id } },
      ],
    }),
  }),
Audrey Hamelers's avatar
#844    
Audrey Hamelers committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
  graphql(CLAIM_MANUSCRIPT, {
    name: 'claimManuscript',
    options: props => ({
      refetchQueries: [
        { query: QUERY_ACTIVITY_INFO, variables: { id: props.manuscript.id } },
      ],
    }),
  }),
  graphql(UNCLAIM_MANUSCRIPT, {
    name: 'unclaimManuscript',
    options: props => ({
      refetchQueries: [
        { query: QUERY_ACTIVITY_INFO, variables: { id: props.manuscript.id } },
      ],
    }),
  }),
ahamelers's avatar
#773    
ahamelers committed
430
)(MetaSec)