From e8534b6f06f9f22c0cbb2ca1a8b498d13134e697 Mon Sep 17 00:00:00 2001 From: ahamelers <audrey@ahamelers.com> Date: Mon, 25 Feb 2019 10:12:14 +0000 Subject: [PATCH] #18 activity page improvements --- app/components/activity/EventDescription.jsx | 98 ++++++++++++------- app/components/activity/MetaEdit.jsx | 7 +- .../citation-search/PubMedSearch.jsx | 8 +- app/components/mailer/MailerContainer.jsx | 2 +- server/email/index.js | 11 ++- .../xpub-server/entities/email/resolvers.js | 46 ++++----- .../entities/email/typeDefs.graphqls | 2 +- 7 files changed, 100 insertions(+), 74 deletions(-) diff --git a/app/components/activity/EventDescription.jsx b/app/components/activity/EventDescription.jsx index 13ff6926d..3e9445ddf 100644 --- a/app/components/activity/EventDescription.jsx +++ b/app/components/activity/EventDescription.jsx @@ -2,10 +2,12 @@ import React from 'react' import { Query } from 'react-apollo' import styled from 'styled-components' import moment from 'moment' -import { Icon, Action } from '@pubsweet/ui' +import { Icon, Action, Button } from '@pubsweet/ui' import { th } from '@pubsweet/ui-toolkit' import { B } from '../ui' import { AllTypes } from '../upload-files' +import { UserContext } from '../App' +import Mailer from '../mailer' import { GET_USER, GET_JOURNAL } from './operations' const NoteEvent = styled.div` @@ -22,6 +24,11 @@ const EmailBody = styled.div` white-space: pre-wrap; padding-top: calc(${th('gridUnit')} * 2); ` +const ReplyButton = styled(Button)` + min-width: 0; + padding: calc(${th('gridUnit')} / 2) ${th('gridUnit')}; + float: right; +` const Username = ({ id }) => ( <Query query={GET_USER} variables={{ id }}> {({ data, loading }) => { @@ -45,14 +52,6 @@ const Journal = ({ id }) => ( </Query> ) -const toggleHidden = button => { - const array = Array.from(button.children) - array.forEach(element => { - element.classList.toggle('hidden') - }) - button.parentNode.nextElementSibling.classList.toggle('hidden') -} - const cleanUp = column => column.replace('meta,', '').replace('_', ' ') const isJson = str => { try { @@ -105,42 +104,67 @@ const ParseJson = ({ col, val }) => { } } +class EmailMessage extends React.Component { + state = { open: false, mail: false } + static contextType = UserContext + render() { + const currentUser = this.context + const { email, manuscript, sender } = this.props + const { open, mail } = this.state + return ( + <React.Fragment> + <NoteEvent> + <div> + <B>Sent email to: </B> + {email.to === 'helpdesk' ? 'Helpdesk' : <Username id={email.to} />} + <br /> + <B>Subject: </B> + {email.subject} + </div> + <Action + onClick={() => this.setState({ open: !open })} + title="Message body" + > + <Icon color="currentColor" size={3}> + chevron-{open ? 'down' : 'right'} + </Icon> + </Action> + </NoteEvent> + {open && ( + <div> + <ReplyButton onClick={() => this.setState({ mail: true })}> + Reply + </ReplyButton> + <EmailBody>{email.message}</EmailBody> + </div> + )} + {mail && ( + <Mailer + close={() => this.setState({ mail: false })} + currentUser={currentUser} + manuscript={manuscript} + recipients={[sender.id]} + subject={`Re: ${email.subject}`} + /> + )} + </React.Fragment> + ) + } +} + const EventDescription = ({ audit, manuscript }) => { const { originalData, objectType, changes } = audit if (objectType === 'note') { const content = JSON.parse(changes.content) if (content && content.to) { return ( - <React.Fragment> - <NoteEvent> - <div> - <B>Sent email to: </B> - {content.to === 'helpdesk' ? ( - 'Helpdesk' - ) : ( - <Username id={content.to} /> - )} - <br /> - <B>Subject: </B> - {content.subject} - </div> - <Action - onClick={e => toggleHidden(e.currentTarget)} - title="Message body" - > - <Icon color="currentColor" size={3}> - chevron-right - </Icon> - <Icon className="hidden" color="currentColor" size={3}> - chevron-down - </Icon> - </Action> - </NoteEvent> - <EmailBody className="hidden">{content.message}</EmailBody> - </React.Fragment> + <EmailMessage + email={content} + manuscript={manuscript} + sender={audit.user} + /> ) } - return `Note: ${content}` } diff --git a/app/components/activity/MetaEdit.jsx b/app/components/activity/MetaEdit.jsx index 13f0196cd..1a8a199df 100644 --- a/app/components/activity/MetaEdit.jsx +++ b/app/components/activity/MetaEdit.jsx @@ -120,7 +120,12 @@ class CitationEdit extends React.Component { {show === 'search' ? ( <React.Fragment> <H3>Citation search</H3> - <PubMedSearch citationData={change} /> + <PubMedSearch + citationData={e => { + change(e) + close() + }} + /> <Exit close={close} /> </React.Fragment> ) : ( diff --git a/app/components/citation-search/PubMedSearch.jsx b/app/components/citation-search/PubMedSearch.jsx index 4fb5acd6f..89bf5809b 100755 --- a/app/components/citation-search/PubMedSearch.jsx +++ b/app/components/citation-search/PubMedSearch.jsx @@ -349,7 +349,13 @@ class PubMedSearch extends React.Component { </Action> </Notification> ) : ( - <Notification type="info">No results found.</Notification> + <React.Fragment> + {hitcount === 0 && ( + <Notification type="info"> + No results found. + </Notification> + )} + </React.Fragment> )} </React.Fragment> )} diff --git a/app/components/mailer/MailerContainer.jsx b/app/components/mailer/MailerContainer.jsx index 180fe9b4f..a633d4c12 100644 --- a/app/components/mailer/MailerContainer.jsx +++ b/app/components/mailer/MailerContainer.jsx @@ -15,7 +15,7 @@ const handleSubmit = async ( manuscriptId: props.manuscript.id, to: values.recipients, subject: values.subject, - messageInHtml: values.message, + message: values.message, }, refetchQueries: [ { diff --git a/server/email/index.js b/server/email/index.js index d0c61033c..dce9b336f 100755 --- a/server/email/index.js +++ b/server/email/index.js @@ -15,13 +15,16 @@ const { const { sender, url, testAddress } = config['epmc-email'] -const sendMail = (email, subject, messageInHtml, from = null) => { - logger.info(`Email recipient: ${email}`) +const sendMail = (to, subject, message, from = null, cc = null) => { + logger.info(`Email recipient: ${to}`) const mailData = { from: from || sender, - to: testAddress || email, + to: testAddress || to, subject: `[Europe PMC plus] ${subject}`, - html: htmlEmailBase(messageInHtml, url), + html: htmlEmailBase(message, url), + } + if (cc) { + mailData.cc = testAddress || cc } Email.send(mailData) } diff --git a/server/xpub-server/entities/email/resolvers.js b/server/xpub-server/entities/email/resolvers.js index 2f608bd32..552572204 100644 --- a/server/xpub-server/entities/email/resolvers.js +++ b/server/xpub-server/entities/email/resolvers.js @@ -10,43 +10,31 @@ const { url } = config['epmc-email'] const resolvers = { Mutation: { - async epmc_email( - _, - { manuscriptId, to, subject, messageInHtml }, - { user }, - ) { + async epmc_email(_, { manuscriptId, to, subject, message }, { user }) { if (!user) { throw new Error('You are not authenticated!') } const uniqueTo = [...new Set(to)] + // Get email addresses + const sendTo = await uniqueTo.map(async userId => + userId === 'helpdesk' + ? 'helpdesk@europepmc.org' + : UserManager.findEmail(userId), + ) + const content = { to, subject, message } + const note = { + manuscriptId, + notesType: 'userMessage', + content: JSON.stringify(content), + } await Promise.all( - uniqueTo.map(async userId => { - const email = - userId === 'helpdesk' - ? 'helpdesk@europepmc.org' - : await UserManager.findEmail(userId) - await userMessage(email, subject, messageInHtml) - // Create a note - const content = { - to: userId, - subject, - message: messageInHtml, - } - const note = { - manuscriptId, - notesType: 'userMessage', - content: JSON.stringify(content), - } - await NoteManager.create(note, user) - }), + // Send email + await userMessage(sendTo, subject, message), + // Create a note + await NoteManager.create(note, user), ) - return true }, - /* async epmc_addReviewer(_, data, { user }) { - await reviewerEmail(data) - return true - }, */ async epmc_emailPasswordResetLink(_, { email }, ctx) { const user = await UserManager.findByEmail(email) if (!user) { diff --git a/server/xpub-server/entities/email/typeDefs.graphqls b/server/xpub-server/entities/email/typeDefs.graphqls index 5d8c3655b..a8bc60b4f 100644 --- a/server/xpub-server/entities/email/typeDefs.graphqls +++ b/server/xpub-server/entities/email/typeDefs.graphqls @@ -1,6 +1,6 @@ extend type Mutation { # Send email - epmc_email(manuscriptId: ID!, to: [ID]!, subject: String!, messageInHtml: String!): Boolean! + epmc_email(manuscriptId: ID!, to: [ID]!, subject: String!, message: String!): Boolean! # Send email to reset password epmc_emailPasswordResetLink(email: String!): Boolean! -- GitLab