From f2d9d8a7549ecef878c2a8e336543745105c46d2 Mon Sep 17 00:00:00 2001
From: Jure Triglav <juretriglav@gmail.com>
Date: Wed, 29 Jul 2020 10:00:37 +0200
Subject: [PATCH] fix: use polling in a dead-end tree location to prevent
 rerenders

---
 app/components/AdminPage.js    |  6 ++----
 app/components/RolesUpdater.js | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 4 deletions(-)
 create mode 100644 app/components/RolesUpdater.js

diff --git a/app/components/AdminPage.js b/app/components/AdminPage.js
index d069938f37..1db40bc5a1 100644
--- a/app/components/AdminPage.js
+++ b/app/components/AdminPage.js
@@ -32,6 +32,7 @@ import Menu from './Menu'
 import { Spinner } from './shared'
 
 import currentRolesVar from '../shared/currentRolesVar'
+import RolesUpdater from './RolesUpdater'
 
 const getParams = routerPath => {
   const path = '/journal/versions/:version'
@@ -79,10 +80,7 @@ const AdminPage = ({ children, history, match }) => {
   const journal = useContext(JournalContext)
   const [conversion] = useContext(XpubContext)
 
-  // Get the current user every 5 seconds (this includes authorization info)
   const { loading, error, data } = useQuery(GET_CURRENT_USER, {
-    pollInterval: 5000,
-    notifyOnNetworkStatusChange: true,
     fetchPolicy: 'network-only',
     // TODO: useCallback used because of bug: https://github.com/apollographql/apollo-client/issues/6301
     onCompleted: useCallback(data => updateStuff(data), []),
@@ -199,7 +197,7 @@ const AdminPage = ({ children, history, match }) => {
           path="/journal/admin/manuscripts"
         />
       </Switch>
-      {/* <Router history={history}>{children}</Router> */}
+      <RolesUpdater />
     </Root>
   )
 }
diff --git a/app/components/RolesUpdater.js b/app/components/RolesUpdater.js
new file mode 100644
index 0000000000..ea6240230c
--- /dev/null
+++ b/app/components/RolesUpdater.js
@@ -0,0 +1,25 @@
+import { useCallback } from 'react'
+import { useQuery } from '@apollo/client'
+import { GET_CURRENT_USER } from '../queries'
+
+import currentRolesVar from '../shared/currentRolesVar'
+
+const updateStuff = data => {
+  if (data?.currentUser) {
+    return currentRolesVar(data.currentUser._currentRoles)
+  }
+}
+
+const RolesUpdater = ({ children, history, match }) => {
+  // This updates the current roles app-wide using Apollo's makeVar
+  useQuery(GET_CURRENT_USER, {
+    pollInterval: 5000,
+    notifyOnNetworkStatusChange: true,
+    fetchPolicy: 'network-only',
+    // TODO: useCallback used because of bug: https://github.com/apollographql/apollo-client/issues/6301
+    onCompleted: useCallback(data => updateStuff(data), []),
+  })
+  return null
+}
+
+export default RolesUpdater
-- 
GitLab