Skip to content
Snippets Groups Projects
Commit c1f66570 authored by ahamelers's avatar ahamelers
Browse files

fixed multiple review bug

parent 869da0db
No related branches found
No related tags found
2 merge requests!66Shared data model,!67Dev
import React from 'react'
import { withRouter } from 'react-router'
import { Mutation } from 'react-apollo'
import { Mutation, Query } from 'react-apollo'
import moment from 'moment'
import styled from 'styled-components'
import { th, lighten } from '@pubsweet/ui-toolkit'
import { Button, H3, H4 } from '@pubsweet/ui'
import {
Loading,
LoadingIcon,
Buttons,
SectionContent as Content,
SectionHeader as Header,
......@@ -90,7 +92,14 @@ const AList = ({ list }) => (
</Annotations>
)
const ReviewForm = ({ manuscript, previews, reviews, files, history }) => (
const ReviewFormDisplay = ({
manuscript,
previews,
review,
deleted,
files,
history,
}) => (
<Mutation
mutation={REVIEW_MANUSCRIPT}
refetchQueries={() => [
......@@ -101,10 +110,8 @@ const ReviewForm = ({ manuscript, previews, reviews, files, history }) => (
]}
>
{(updateManuscript, { data }) => {
const { id, status, teams } = manuscript
const review = reviews.find(r => !r.deleted) || null
let deleted = reviews.filter(r => r.deleted)
const { annotations } = review || []
const { id, status, teams } = manuscript
const reviewer = teams.find(t => t.role === 'reviewer').teamMembers[0]
const setStatus = async newStatus => {
await updateManuscript({
......@@ -259,4 +266,27 @@ const ReviewForm = ({ manuscript, previews, reviews, files, history }) => (
</Mutation>
)
export default withRouter(ReviewForm)
const ReviewFormRouter = withRouter(ReviewFormDisplay)
const ReviewForm = ({ manuscript, ...props }) => (
<Query query={GET_REVIEWS} variables={{ manuscriptId: manuscript.id }}>
{({ data: { deletedReviews }, loading }) => {
if (loading || !deletedReviews) {
return (
<Loading>
<LoadingIcon />
</Loading>
)
}
return (
<ReviewFormRouter
deleted={deletedReviews}
manuscript={manuscript}
{...props}
/>
)
}}
</Query>
)
export default ReviewForm
......@@ -29,7 +29,7 @@ import HTMLPreview from './HTMLPreview'
import ReviewForm from './ReviewForm'
import Annotator from './Annotator'
import ReviewInstructions from './ReviewInstructions'
import { CONVERT_XML, GET_REVIEWS } from './operations'
import { CONVERT_XML, CURRENT_REVIEW } from './operations'
const PreviewPageDiv = styled(PreviewPage)`
& > div > div {
......@@ -136,8 +136,7 @@ class Review extends React.Component {
}
}
render() {
const { manuscript, currentUser, reviews } = this.props
const review = reviews.find(r => !r.deleted) || null
const { manuscript, currentUser, review } = this.props
const { files: allfiles, status, teams } = manuscript
const sourceFile = allfiles.find(f => f.type === 'manuscript')
const files = allfiles.filter(
......@@ -345,7 +344,7 @@ class Review extends React.Component {
files={originalFiles}
manuscript={manuscript}
previews={!!html && !!pdf}
reviews={reviews}
review={review}
/>
)}
</PanelContent>
......@@ -365,8 +364,11 @@ const ReviewPage = ({ match, ...props }) => (
variables={{ id: match.params.id }}
>
{({ data: { manuscript }, loading: loadingOne }) => (
<Query query={GET_REVIEWS} variables={{ manuscriptId: match.params.id }}>
{({ data, loading: loadingTwo, refetch }) => {
<Query
query={CURRENT_REVIEW}
variables={{ manuscriptId: match.params.id }}
>
{({ data: { currentReview }, loading, refetch }) => {
if (loadingOne || !manuscript) {
return (
<Loading>
......@@ -379,7 +381,7 @@ const ReviewPage = ({ match, ...props }) => (
manuscript={manuscript}
match={match}
reload={() => refetch()}
reviews={(data && data.reviewsByMId) || []}
review={currentReview}
{...props}
/>
)
......
......@@ -7,36 +7,51 @@ export const CONVERT_XML = gql`
}
`
export const GET_REVIEWS = gql`
query($manuscriptId: ID!) {
reviewsByMId(manuscriptId: $manuscriptId) {
const ReviewFragment = gql`
fragment ReviewFragment on Review {
id
created
user {
id
created
deleted
user {
id
identities {
... on Local {
name {
givenNames
surname
}
identities {
... on Local {
name {
givenNames
surname
}
}
}
annotations {
id
updated
quote
text
file {
type
}
}
annotations {
id
updated
quote
text
file {
type
}
}
}
`
export const CURRENT_REVIEW = gql`
query($manuscriptId: ID!) {
currentReview(manuscriptId: $manuscriptId) {
...ReviewFragment
}
}
${ReviewFragment}
`
export const GET_REVIEWS = gql`
query($manuscriptId: ID!) {
deletedReviews(manuscriptId: $manuscriptId) {
...ReviewFragment
}
}
${ReviewFragment}
`
export const REVIEW_MANUSCRIPT = gql`
mutation ReviewManuscript($data: ManuscriptInput!) {
reviewManuscript(data: $data) {
......
......@@ -12,18 +12,27 @@ const AnnotationManager = {
deleteByReviewId: Annotation.deleteByReviewId,
save: async input => {
const annotation = rowToEntity(input)
const { fileId, userId, reviewId } = annotation
const { fileId, userId } = annotation
let { reviewId } = annotation
let annId = annotation.id
if (!reviewId) {
try {
const file = await FileAccess.selectById(fileId)
const review = new ReviewAccess({
manuscriptId: file.manuscriptId,
userId,
updatedBy: userId,
})
const savedReview = await review.save()
annotation.reviewId = savedReview.id
const review = await ReviewAccess.getCurrentByManuscriptId(
file.manuscriptId,
)
reviewId = review ? review.id : null
if (!reviewId) {
const review = new ReviewAccess({
manuscriptId: file.manuscriptId,
userId,
updatedBy: userId,
})
const savedReview = await review.save()
annotation.reviewId = savedReview.id
} else {
annotation.reviewId = reviewId
}
} catch (error) {
throw new Error(error)
}
......
......@@ -75,9 +75,22 @@ class Review extends EpmcBaseModel {
return rowToEntity(rows[0])
}
static async selectByManuscriptId(manId) {
static async getCurrentByManuscriptId(manId) {
const row = await Review.query()
.where('review.manuscript_id', manId)
.whereNull('review.deleted')
.eager('[user, annotations.file]')
.modifyEager('annotations', builder => {
builder.whereNull('deleted')
})
.first()
return rowToEntity(row)
}
static async getDeletedByManuscriptId(manId) {
const rows = await Review.query()
.where('review.manuscript_id', manId)
.whereNotNull('review.deleted')
.eager('[user, annotations.file]')
.modifyEager('annotations', builder => {
builder.whereNull('deleted')
......
......@@ -27,9 +27,13 @@ const transform = review => {
const ReviewManager = {
find: Review.selectById,
findByManuscriptId: async id => {
const reviews = await Review.selectByManuscriptId(id)
return reviews.map(r => transform(r))
getCurrent: async id => {
const review = await Review.getCurrentByManuscriptId(id)
return review ? transform(review) : null
},
getDeleted: async id => {
const reviews = await Review.getDeletedByManuscriptId(id)
return reviews ? reviews.map(r => transform(r)) : []
},
delete: Review.delete,
deleteByManuscriptId: Review.deleteByManuscriptId,
......
......@@ -4,12 +4,19 @@ const { ReviewManager } = rfr('server/xpub-model')
const resolvers = {
Query: {
async reviewsByMId(_, { manuscriptId }, { user }) {
async currentReview(_, { manuscriptId }, { user }) {
if (!user) {
throw new Error('You are not authenticated!')
}
return ReviewManager.findByManuscriptId(manuscriptId)
return ReviewManager.getCurrent(manuscriptId)
},
async deletedReviews(_, { manuscriptId }, { user }) {
if (!user) {
throw new Error('You are not authenticated!')
}
return ReviewManager.getDeleted(manuscriptId)
},
},
}
......
extend type Query {
reviewsByMId(manuscriptId: ID!): [Review]
currentReview(manuscriptId: ID!): Review
deletedReviews(manuscriptId: ID!): [Review]
}
\ No newline at end of file
......@@ -14,7 +14,6 @@ type Issn {
}
extend type Review {
deleted: DateTime
annotations: [Annotation]
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment