From 19b6d559bbeafa5692bbe8935ce8dc8aae3a31ad Mon Sep 17 00:00:00 2001
From: Nicoleta Ursu <nicoleta.ursu@endava.com>
Date: Mon, 5 Apr 2021 17:35:48 +0300
Subject: [PATCH] fix: adding filtering manuscripts by topic

---
 .../component-manuscripts/src/Manuscript.js        | 12 +++++++++---
 .../component-manuscripts/src/Manuscripts.jsx      |  4 ++--
 app/components/component-manuscripts/src/style.js  |  2 +-
 server/model-manuscript/src/graphql.js             | 14 ++++++++++----
 4 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/app/components/component-manuscripts/src/Manuscript.js b/app/components/component-manuscripts/src/Manuscript.js
index f088855a04..864530aeaf 100644
--- a/app/components/component-manuscripts/src/Manuscript.js
+++ b/app/components/component-manuscripts/src/Manuscript.js
@@ -63,7 +63,7 @@ const User = ({ manuscriptId, manuscript, submitter, history, ...props }) => {
     })
   }
 
-  const filterByTopic = (topic) => {
+  const filterByTopic = topic => {
     props.setSelectedTopic(topic)
     history.replace(`${urlFrag}/admin/manuscripts?topic=${topic}`)
   }
@@ -80,10 +80,16 @@ const User = ({ manuscriptId, manuscript, submitter, history, ...props }) => {
       <Cell>{convertTimestampToDate(manuscript.updated)}</Cell>
       {process.env.INSTANCE_NAME === 'ncrc' && (
         <Cell>
-          {manuscript.submission.topics.map((topic) => {
-            return <StyledTopic key={topic} title={convertCamelCaseToText(topic)} onClick={() => filterByTopic(topic)}>
+          {manuscript.submission.topics.map(topic => {
+            return (
+              <StyledTopic
+                key={topic}
+                onClick={() => filterByTopic(topic)}
+                title={convertCamelCaseToText(topic)}
+              >
                 {convertCamelCaseToText(topic)}
               </StyledTopic>
+            )
           })}
         </Cell>
       )}
diff --git a/app/components/component-manuscripts/src/Manuscripts.jsx b/app/components/component-manuscripts/src/Manuscripts.jsx
index a27c414f01..2d904ac63c 100644
--- a/app/components/component-manuscripts/src/Manuscripts.jsx
+++ b/app/components/component-manuscripts/src/Manuscripts.jsx
@@ -71,14 +71,14 @@ const Manuscripts = ({ history, ...props }) => {
       sort,
       offset: (page - 1) * limit,
       limit,
-      filter: { submission: JSON.stringify({ topics: selectedTopic }) }
+      filter: history.location.search ? { submission: JSON.stringify({ topics: selectedTopic }) } : {}
     },
     fetchPolicy: 'network-only',
   })
 
   useEffect(() => {
     refetch()
-  }, [selectedTopic])
+  }, [history.location.search])
 
   if (loading) return <Spinner />
   if (error) return `Error! ${error.message}`
diff --git a/app/components/component-manuscripts/src/style.js b/app/components/component-manuscripts/src/style.js
index 5578a159f3..51c00cc650 100644
--- a/app/components/component-manuscripts/src/style.js
+++ b/app/components/component-manuscripts/src/style.js
@@ -52,4 +52,4 @@ export const StyledTableLabel = styled.p`
   padding: 0 10px;
   text-align: center;
   white-space: nowrap;
-`
\ No newline at end of file
+`
diff --git a/server/model-manuscript/src/graphql.js b/server/model-manuscript/src/graphql.js
index cad324e3ae..da9d205514 100644
--- a/server/model-manuscript/src/graphql.js
+++ b/server/model-manuscript/src/graphql.js
@@ -378,8 +378,12 @@ const resolvers = {
       }
     },
     async paginatedManuscripts(_, { sort, offset, limit, filter }, ctx) {
-      const parsedSubmission = JSON.parse(filter?.submission)
+      let parsedSubmission;
 
+      if(filter.submission) {
+        parsedSubmission = JSON.parse(filter.submission)
+      }
+      
       const query = ctx.models.Manuscript.query()
         .where({ parentId: null })
         .withGraphFetched('[submitter, manuscriptVersions(orderByCreated)]')
@@ -394,11 +398,13 @@ const resolvers = {
       }
 
       if (process.env.INSTANCE_NAME === 'ncrc') {
-        if (filter && parsedSubmission.topics ) {
-          query.whereRaw("(submission->'topics')::jsonb \\? ?", [parsedSubmission.topics])
+        if (filter && parsedSubmission) {
+          query.whereRaw("(submission->'topics')::jsonb \\? ?", [
+            parsedSubmission.topics,
+          ])
         }
       }
-     
+
       const totalCount = await query.resultSize()
 
       if (sort) {
-- 
GitLab