Check if the item exists before rendering the page

Redirect to 404 page if the item doesn't exist.

Fixes #3.
master
ramzan 4 years ago committed by GitHub
parent ad6ac59c4f
commit 96029a0f0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -126,8 +126,22 @@ const styles = (theme: any) => ({
}, },
}); });
function PageNotFound() {
return (
<Container>
<h1>404 Page Not Found</h1>
</Container>
);
}
const CollectionRoutes = withStyles(styles)(withRouter( const CollectionRoutes = withStyles(styles)(withRouter(
class CollectionRoutesInner extends React.PureComponent<CollectionRoutesPropsType> { class CollectionRoutesInner extends React.PureComponent<CollectionRoutesPropsType> {
private itemUndefined(itemUid: string) {
return this.props.items[itemUid] === undefined;
}
public render() { public render() {
const props = this.props; const props = this.props;
const { classes } = this.props; const { classes } = this.props;
@ -155,6 +169,9 @@ const CollectionRoutes = withStyles(styles)(withRouter(
exact exact
render={({ match }) => { render={({ match }) => {
const itemUid = decodeURIComponent(match.params.itemUid); const itemUid = decodeURIComponent(match.params.itemUid);
if (this.itemUndefined(itemUid)) {
return PageNotFound();
}
return ( return (
<Container style={{ maxWidth: '30rem' }}> <Container style={{ maxWidth: '30rem' }}>
{(itemUid in props.items) && {(itemUid in props.items) &&
@ -175,53 +192,65 @@ const CollectionRoutes = withStyles(styles)(withRouter(
<Route <Route
path={routeResolver.getRoute(props.routePrefix + '._id.log')} path={routeResolver.getRoute(props.routePrefix + '._id.log')}
exact exact
render={({ match }) => ( render={({ match }) => {
<Container> const paramItemUid = decodeURIComponent(match.params.itemUid);
<ItemChangeLog if (this.itemUndefined(paramItemUid)) {
syncInfo={props.syncInfo} return PageNotFound();
paramItemUid={decodeURIComponent(match.params.itemUid)} }
/> return (
</Container> <Container>
)} <ItemChangeLog
syncInfo={props.syncInfo}
paramItemUid={paramItemUid}
/>
</Container>
);
}}
/> />
<Route <Route
path={routeResolver.getRoute(props.routePrefix + '._id')} path={routeResolver.getRoute(props.routePrefix + '._id')}
exact exact
render={({ match, history }) => ( render={({ match, history }) => {
<Container> const itemUid = decodeURIComponent(match.params.itemUid);
<div style={{ textAlign: 'right', marginBottom: 15 }}> if (this.itemUndefined(itemUid)) {
<Button return PageNotFound();
variant="contained" }
className={classes.button} return (
onClick={() => <Container>
history.push(routeResolver.getRoute( <div style={{ textAlign: 'right', marginBottom: 15 }}>
props.routePrefix + '._id.log', <Button
{ itemUid: match.params.itemUid })) variant="contained"
} className={classes.button}
> onClick={() =>
<IconChangeHistory className={classes.leftIcon} /> history.push(routeResolver.getRoute(
Change History props.routePrefix + '._id.log',
</Button> { itemUid: match.params.itemUid }))
}
<Button >
color="secondary" <IconChangeHistory className={classes.leftIcon} />
variant="contained" Change History
disabled={!props.componentEdit} </Button>
className={classes.button}
style={{ marginLeft: 15 }} <Button
onClick={() => color="secondary"
history.push(routeResolver.getRoute( variant="contained"
props.routePrefix + '._id.edit', disabled={!props.componentEdit}
{ itemUid: match.params.itemUid })) className={classes.button}
} style={{ marginLeft: 15 }}
> onClick={() =>
<IconEdit className={classes.leftIcon} /> history.push(routeResolver.getRoute(
Edit props.routePrefix + '._id.edit',
</Button> { itemUid: match.params.itemUid }))
</div> }
<ComponentView item={props.items[decodeURIComponent(match.params.itemUid)]} /> >
</Container> <IconEdit className={classes.leftIcon} />
)} Edit
</Button>
</div>
<ComponentView item={props.items[decodeURIComponent(match.params.itemUid)]} />
</Container>
);
}}
/> />
</Switch> </Switch>
); );

Loading…
Cancel
Save