Skip to content

Commit

Permalink
Moved object type fetching from home view to application start
Browse files Browse the repository at this point in the history
  • Loading branch information
saulipurhonen committed Apr 30, 2021
1 parent 02ba39d commit 7c4d413
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
44 changes: 43 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -60,6 +64,44 @@ const NavigationMenu = () => {
*/
const App = (): React$Element<typeof React.Fragment> => {
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 (
<React.Fragment>
Expand Down
42 changes: 0 additions & 42 deletions src/views/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -36,7 +33,6 @@ const useStyles = makeStyles(theme => ({
const Home = (): React$Element<typeof Grid> => {
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)
Expand Down Expand Up @@ -75,44 +71,6 @@ const Home = (): React$Element<typeof Grid> => {
}
}, [])

// 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 (
<Grid container direction="column" justify="space-between" alignItems="stretch">
Expand Down

0 comments on commit 7c4d413

Please sign in to comment.