From 96029a0f0c0794fbc326ba9f2fdf7c2572688528 Mon Sep 17 00:00:00 2001 From: ramzan <55637406+ramzan@users.noreply.github.com> Date: Fri, 17 Jul 2020 05:41:49 +0000 Subject: [PATCH] Check if the item exists before rendering the page Redirect to 404 page if the item doesn't exist. Fixes #3. --- src/Pim/index.tsx | 115 +++++++++++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 43 deletions(-) diff --git a/src/Pim/index.tsx b/src/Pim/index.tsx index 1a20130..91b875a 100644 --- a/src/Pim/index.tsx +++ b/src/Pim/index.tsx @@ -126,8 +126,22 @@ const styles = (theme: any) => ({ }, }); +function PageNotFound() { + return ( + +

404 Page Not Found

+
+ ); +} + + const CollectionRoutes = withStyles(styles)(withRouter( class CollectionRoutesInner extends React.PureComponent { + + private itemUndefined(itemUid: string) { + return this.props.items[itemUid] === undefined; + } + public render() { const props = this.props; const { classes } = this.props; @@ -155,6 +169,9 @@ const CollectionRoutes = withStyles(styles)(withRouter( exact render={({ match }) => { const itemUid = decodeURIComponent(match.params.itemUid); + if (this.itemUndefined(itemUid)) { + return PageNotFound(); + } return ( {(itemUid in props.items) && @@ -175,53 +192,65 @@ const CollectionRoutes = withStyles(styles)(withRouter( ( - - - - )} + render={({ match }) => { + const paramItemUid = decodeURIComponent(match.params.itemUid); + if (this.itemUndefined(paramItemUid)) { + return PageNotFound(); + } + return ( + + + + ); + }} /> ( - -
- - - -
- -
- )} + render={({ match, history }) => { + const itemUid = decodeURIComponent(match.params.itemUid); + if (this.itemUndefined(itemUid)) { + return PageNotFound(); + } + return ( + +
+ + + +
+ +
+ ); + }} /> );