diff --git a/src/App.js b/src/App.js index 7d5f5c69..39337a00 100644 --- a/src/App.js +++ b/src/App.js @@ -1,14 +1,18 @@ //@flow -import React from "react" +import React, { useEffect } from "react" import Container from "@material-ui/core/Container" import CssBaseline from "@material-ui/core/CssBaseline" import { makeStyles } from "@material-ui/core/styles" +import { useDispatch } from "react-redux" import { Switch, Route, useLocation } from "react-router-dom" import SelectedFolderDetails from "components/Home/SelectedFolderDetails" import SubmissionFolderList from "components/Home/SubmissionFolderList" import Nav from "components/Nav" +import { ObjectTypes } from "constants/wizardObject" +import { setObjectsArray } from "features/objectsArraySlice" +import schemaAPIService from "services/schemaAPI" import Page401 from "views/ErrorPages/Page401" import Page403 from "views/ErrorPages/Page403" import Page404 from "views/ErrorPages/Page404" @@ -60,6 +64,44 @@ const NavigationMenu = () => { */ const App = (): React$Element => { const classes = useStyles() + const dispatch = useDispatch() + + // Fetch array of schemas from backend and store it in frontend + // Fetch only if the initial array is empty + // if there is any errors while fetching, it will return a manually created ObjectsArray instead + useEffect(() => { + if (location.pathname === "/" || pathsWithoutNav.indexOf(location.pathname) !== -1) return + let isMounted = true + const getSchemas = async () => { + const response = await schemaAPIService.getAllSchemas() + + if (isMounted) { + if (response.ok) { + const schemas = response.data + .filter(schema => schema.title !== "Project" && schema.title !== "Submission") + .map(schema => schema.title.toLowerCase()) + dispatch(setObjectsArray(schemas)) + } else { + dispatch( + setObjectsArray([ + ObjectTypes.study, + ObjectTypes.sample, + ObjectTypes.experiment, + ObjectTypes.run, + ObjectTypes.analysis, + ObjectTypes.dac, + ObjectTypes.policy, + ObjectTypes.dataset, + ]) + ) + } + } + } + getSchemas() + return () => { + isMounted = false + } + }, []) return ( diff --git a/src/views/Home.js b/src/views/Home.js index 16f3a671..a6e30318 100644 --- a/src/views/Home.js +++ b/src/views/Home.js @@ -12,14 +12,11 @@ import SubmissionIndexCard from "components/Home/SubmissionIndexCard" import UserDraftTemplates from "components/Home/UserDraftTemplates" import WizardStatusMessageHandler from "components/NewDraftWizard/WizardForms/WizardStatusMessageHandler" import { FolderSubmissionStatus } from "constants/wizardFolder" -import { ObjectTypes } from "constants/wizardObject" import { WizardStatus } from "constants/wizardStatus" -import { setObjectsArray } from "features/objectsArraySlice" import { setPublishedFolders } from "features/publishedFoldersSlice" import { setUnpublishedFolders } from "features/unpublishedFoldersSlice" import { fetchUserById } from "features/userSlice" import folderAPIService from "services/folderAPI" -import schemaAPIService from "services/schemaAPI" const useStyles = makeStyles(theme => ({ tableCard: { @@ -36,7 +33,6 @@ const useStyles = makeStyles(theme => ({ const Home = (): React$Element => { const dispatch = useDispatch() const user = useSelector(state => state.user) - const objectsArray = useSelector(state => state.objectsArray) const unpublishedFolders = useSelector(state => state.unpublishedFolders) const publishedFolders = useSelector(state => state.publishedFolders) @@ -75,44 +71,6 @@ const Home = (): React$Element => { } }, []) - // Fetch array of schemas from backend and store it in frontend - // Fetch only if the initial array is empty - // if there is any errors while fetching, it will return a manually created ObjectsArray instead - useEffect(() => { - if (objectsArray?.length === 0) { - let isMounted = true - const getSchemas = async () => { - const response = await schemaAPIService.getAllSchemas() - - if (isMounted) { - if (response.ok) { - const schemas = response.data - .filter(schema => schema.title !== "Project" && schema.title !== "Submission") - .map(schema => schema.title.toLowerCase()) - dispatch(setObjectsArray(schemas)) - } else { - dispatch( - setObjectsArray([ - ObjectTypes.study, - ObjectTypes.sample, - ObjectTypes.experiment, - ObjectTypes.run, - ObjectTypes.analysis, - ObjectTypes.dac, - ObjectTypes.policy, - ObjectTypes.dataset, - ]) - ) - } - } - } - getSchemas() - return () => { - isMounted = false - } - } - }, []) - // Contains both unpublished and published folders (max. 5 items/each) return (