Simple error handling in ImageGalleryLayout
Cannot use react-query at the moment as react 18 is not supported yet.
This commit is contained in:
parent
316b3205ab
commit
416298e673
|
|
@ -2,25 +2,32 @@ import React, { useEffect, useState } from "react";
|
||||||
import { Photo } from "react-photo-album";
|
import { Photo } from "react-photo-album";
|
||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
import CssBaseline from "@mui/material/CssBaseline";
|
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 { Folders } from "./ImageGallery/models";
|
||||||
import ImageGalleryAppBar from "./ImageGallery/ImageGalleryAppBar";
|
import ImageGalleryAppBar from "./ImageGallery/ImageGalleryAppBar";
|
||||||
import DrawerHeader from "./MuiLayout/DrawerHeader";
|
import DrawerHeader from "./MuiLayout/DrawerHeader";
|
||||||
import ImageGalleryDrawer from "./ImageGallery/ImageGalleryDrawer";
|
import ImageGalleryDrawer from "./ImageGallery/ImageGalleryDrawer";
|
||||||
import ImageGallery from "./ImageGallery/ImageGallery";
|
import ImageGallery from "./ImageGallery/ImageGallery";
|
||||||
import Main from "./MuiLayout/Main";
|
import Main from "./MuiLayout/Main";
|
||||||
|
import env from "./env";
|
||||||
|
|
||||||
const drawerWidth = 240;
|
const drawerWidth = 240;
|
||||||
|
|
||||||
function ImageGalleryLayout() {
|
function ImageGalleryLayout() {
|
||||||
const [open, setOpen] = useState(true);
|
const [open, setOpen] = useState(true);
|
||||||
|
const [error, setError] = useState(false);
|
||||||
|
const [imagesLoaded, setImagesLoaded] = useState(false);
|
||||||
|
const [images, setImages] = useState<Photo[]>([]);
|
||||||
const [folders, setFolders] = useState<Folders>({
|
const [folders, setFolders] = useState<Folders>({
|
||||||
name: "Home",
|
name: "Home",
|
||||||
fullPath: "/",
|
fullPath: "/",
|
||||||
numberOfFiles: 0,
|
numberOfFiles: 0,
|
||||||
children: [],
|
children: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const handleDrawerOpen = () => {
|
const handleDrawerOpen = () => {
|
||||||
setOpen(true);
|
setOpen(true);
|
||||||
|
|
@ -29,12 +36,25 @@ function ImageGalleryLayout() {
|
||||||
const handleDrawerClose = () => {
|
const handleDrawerClose = () => {
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
};
|
};
|
||||||
const [images, setImages] = useState<Photo[]>([]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
setImages([]);
|
||||||
|
setError(false);
|
||||||
|
setImagesLoaded(false);
|
||||||
fetch(`/images${location.pathname}`)
|
fetch(`/images${location.pathname}`)
|
||||||
.then((res) => res.json())
|
.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]);
|
}, [location.pathname]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -43,6 +63,15 @@ function ImageGalleryLayout() {
|
||||||
.then((data) => setFolders(data));
|
.then((data) => setFolders(data));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
return (
|
||||||
|
<p>
|
||||||
|
Unrecoverable error: Request for pictures failed. Please contact the
|
||||||
|
administrator.
|
||||||
|
</p>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box sx={{ display: "flex" }}>
|
<Box sx={{ display: "flex" }}>
|
||||||
<CssBaseline />
|
<CssBaseline />
|
||||||
|
|
@ -59,7 +88,13 @@ function ImageGalleryLayout() {
|
||||||
/>
|
/>
|
||||||
<Main open={open} drawerwidth={drawerWidth}>
|
<Main open={open} drawerwidth={drawerWidth}>
|
||||||
<DrawerHeader />
|
<DrawerHeader />
|
||||||
<ImageGallery images={images} />
|
{imagesLoaded ? (
|
||||||
|
<ImageGallery images={images} />
|
||||||
|
) : (
|
||||||
|
<CircularProgress
|
||||||
|
style={{ color: env.REACT_APP_APPBAR_COLOR ?? "#1976D2" }}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</Main>
|
</Main>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,6 @@ export const getImages =
|
||||||
res.json(a<Folder>({ images }));
|
res.json(a<Folder>({ images }));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
consoleLogger.warn(`Error when trying to access ${req.path}: ${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.` });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue