Skip to content
Snippets Groups Projects
Commit 5a9a42f1 authored by Jure's avatar Jure
Browse files

fix: adjust how reviews are cached client-side and add relations on the server

parent 784928aa
No related branches found
No related tags found
No related merge requests found
...@@ -294,7 +294,7 @@ const decisionSections = ({ ...@@ -294,7 +294,7 @@ const decisionSections = ({
const DecisionPage = ({ match }) => { const DecisionPage = ({ match }) => {
// Hooks from the old world // Hooks from the old world
const [makeDecision] = useMutation(makeDecisionMutation, { const [makeDecision] = useMutation(makeDecisionMutation, {
refetchQueries: [query], // refetchQueries: [query],
}) })
const [updateReviewMutation] = useMutation(updateReviewMutationQuery) const [updateReviewMutation] = useMutation(updateReviewMutationQuery)
...@@ -356,25 +356,57 @@ const DecisionPage = ({ match }) => { ...@@ -356,25 +356,57 @@ const DecisionPage = ({ match }) => {
id: review.id || undefined, id: review.id || undefined,
input: reviewData, input: reviewData,
}, },
update: (proxy, { data: { updateReview } }) => { update: (cache, { data: { updateReview } }) => {
const data = proxy.readQuery({ cache.modify({
query, id: cache.identify(manuscript),
variables: { fields: {
id: manuscript.id, reviews(existingReviewRefs = [], { readField }) {
const newReviewRef = cache.writeFragment({
data: updateReview,
fragment: gql`
fragment NewReview on Review {
id
}
`,
})
if (
existingReviewRefs.some(
ref => readField('id', ref) === updateReview.id,
)
) {
return existingReviewRefs
}
return [...existingReviewRefs, newReviewRef]
},
}, },
}) })
const reviewIndex = data.manuscript.reviews.findIndex(
review => review.id === updateReview.id,
)
if (reviewIndex < 0) {
data.manuscript.reviews.push(updateReview)
} else {
data.manuscript.reviews[reviewIndex] = updateReview
}
proxy.writeQuery({ query, data })
}, },
}) })
} }
// const manuscriptFragment = cache.readFragment({
// id: cache.identify(manuscript),
// fragment: gql`
// fragment MyManuscript on Manuscript {
// id
// reviews
// }
// `,
// })
// cache.writeFragment()
// const reviewIndex = data.manuscript.reviews.findIndex(
// review => review.id === updateReview.id,
// )
// if (reviewIndex < 0) {
// data.manuscript.reviews.push(updateReview)
// } else {
// data.manuscript.reviews[reviewIndex] = updateReview
// }
// cache.writeQuery({ query, data })
// },
// }, // },
// }).then(() => { // }).then(() => {
// history.push('/dashboard') // history.push('/dashboard')
......
import React, { useContext } from 'react' import React, { useContext } from 'react'
import { NoteEditor } from 'xpub-edit' import { NoteEditor } from 'xpub-edit'
import { cloneDeep, omit } from 'lodash' import { cloneDeep, omit } from 'lodash'
import { FieldArray, Field } from 'formik' import { Field } from 'formik'
import { import {
Button, Button,
Flexbox, Flexbox,
...@@ -28,16 +28,18 @@ import { ...@@ -28,16 +28,18 @@ import {
SectionAction, SectionAction,
} from '../style' } from '../style'
const NoteDecision = (updateReview, uploadFile) => props => ( const NoteDecision = ({ updateReview, uploadFile }) => (
<> <>
<Field <Field
component={NoteInput} component={NoteInput}
key="commentinput"
name="comments" name="comments"
updateReview={updateReview} updateReview={updateReview}
validate={required} validate={required}
/> />
<Field <Field
component={AttachmentsInput('note')} component={AttachmentsInput('note')}
key="attachmentinput"
updateReview={updateReview} updateReview={updateReview}
uploadFile={uploadFile} uploadFile={uploadFile}
/> />
...@@ -132,17 +134,13 @@ const RecommendationInput = ({ ...@@ -132,17 +134,13 @@ const RecommendationInput = ({
} }
const DecisionForm = ({ handleSubmit, uploadFile, updateReview, isValid }) => ( const DecisionForm = ({ handleSubmit, uploadFile, updateReview, isValid }) => (
<Container> <Container key="decisionform">
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
<SectionHeader> <SectionHeader>
<Title>Decision</Title> <Title>Decision</Title>
</SectionHeader> </SectionHeader>
<SectionRow key="note"> <SectionRow key="note">
<FieldArray <NoteDecision updateReview={updateReview} uploadFile={uploadFile} />
component={NoteDecision(updateReview, uploadFile)}
key="comments-array"
name="comments"
/>
</SectionRow> </SectionRow>
<SectionRowGrid> <SectionRowGrid>
<Field <Field
......
...@@ -294,6 +294,20 @@ const resolvers = { ...@@ -294,6 +294,20 @@ const resolvers = {
submission(parent) { submission(parent) {
return JSON.stringify(parent.submission) return JSON.stringify(parent.submission)
}, },
async reviews(parent, _, ctx) {
return parent.reviews
? parent.reviews
: (
await ctx.models.Manuscript.query().findById(parent.id)
).$relatedQuery('reviews')
},
async teams(parent, _, ctx) {
return parent.teams
? parent.teams
: (
await ctx.models.Manuscript.query().findById(parent.id)
).$relatedQuery('teams')
},
}, },
ManuscriptVersion: { ManuscriptVersion: {
submission(parent) { submission(parent) {
......
...@@ -51,6 +51,29 @@ class Review extends BaseModel { ...@@ -51,6 +51,29 @@ class Review extends BaseModel {
} }
} }
static get relationMappings() {
const { Manuscript, User } = require('@pubsweet/models')
return {
manuscript: {
relation: BaseModel.BelongsToOneRelation,
modelClass: Manuscript,
join: {
from: 'reviews.manuscriptId',
to: 'manuscripts.id',
},
},
user: {
relation: BaseModel.BelongsToOneRelation,
modelClass: User,
join: {
from: 'reviews.userId',
to: 'users.id',
},
},
}
}
async $beforeDelete() { async $beforeDelete() {
const File = require('../../model-file/src/file') const File = require('../../model-file/src/file')
const files = await File.findByObject({ const files = await File.findByObject({
......
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