diff --git a/app/components/AdminPage.js b/app/components/AdminPage.js index d069938f376412abb714f71924e90cb170899059..1db40bc5a12a2bb20cf5deb786f9289d84ba4945 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 0000000000000000000000000000000000000000..ea6240230c09859a1fe27718aac91bc3f7ece0e5 --- /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