diff --git a/picture-gallery-client/src/ImageGalleryLayout.tsx b/picture-gallery-client/src/ImageGalleryLayout.tsx index 62e18d5..2174d30 100644 --- a/picture-gallery-client/src/ImageGalleryLayout.tsx +++ b/picture-gallery-client/src/ImageGalleryLayout.tsx @@ -2,25 +2,32 @@ import React, { useEffect, useState } from "react"; import { Photo } from "react-photo-album"; import Box from "@mui/material/Box"; import CssBaseline from "@mui/material/CssBaseline"; -import { useLocation } from "react-router-dom"; +import { useLocation, useNavigate } from "react-router-dom"; +import { CircularProgress } from "@mui/material"; import { Folders } from "./ImageGallery/models"; import ImageGalleryAppBar from "./ImageGallery/ImageGalleryAppBar"; import DrawerHeader from "./MuiLayout/DrawerHeader"; import ImageGalleryDrawer from "./ImageGallery/ImageGalleryDrawer"; import ImageGallery from "./ImageGallery/ImageGallery"; import Main from "./MuiLayout/Main"; +import env from "./env"; const drawerWidth = 240; function ImageGalleryLayout() { const [open, setOpen] = useState(true); + const [error, setError] = useState(false); + const [imagesLoaded, setImagesLoaded] = useState(false); + const [images, setImages] = useState([]); const [folders, setFolders] = useState({ name: "Home", fullPath: "/", numberOfFiles: 0, children: [], }); + const location = useLocation(); + const navigate = useNavigate(); const handleDrawerOpen = () => { setOpen(true); @@ -29,12 +36,25 @@ function ImageGalleryLayout() { const handleDrawerClose = () => { setOpen(false); }; - const [images, setImages] = useState([]); useEffect(() => { + setImages([]); + setError(false); + setImagesLoaded(false); fetch(`/images${location.pathname}`) .then((res) => res.json()) - .then((data) => setImages(data.images)); + .then((data) => { + if (data.images === undefined) { + if (location.pathname !== "/") { + navigate("/"); + } else { + setError(true); + } + } else { + setImages(data.images); + setImagesLoaded(true); + } + }); }, [location.pathname]); useEffect(() => { @@ -43,6 +63,15 @@ function ImageGalleryLayout() { .then((data) => setFolders(data)); }, []); + if (error) { + return ( +

+ Unrecoverable error: Request for pictures failed. Please contact the + administrator. +

+ ); + } + return ( @@ -59,7 +88,13 @@ function ImageGalleryLayout() { />
- + {imagesLoaded ? ( + + ) : ( + + )}
); diff --git a/picture-gallery-server/src/controller/images.ts b/picture-gallery-server/src/controller/images.ts index 7828f4c..1984dd6 100644 --- a/picture-gallery-server/src/controller/images.ts +++ b/picture-gallery-server/src/controller/images.ts @@ -38,6 +38,6 @@ export const getImages = res.json(a({ images })); } catch (e) { consoleLogger.warn(`Error when trying to access ${req.path}: ${e}`); - res.status(400).json(`Path ${req.path} not accessible.`); + res.status(400).json({ message: `Path ${req.path} not accessible.` }); } };