-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Survey not found error handling #5160
Changes from 5 commits
488ea44
9334fbf
091a7dc
9e4df9a
48df010
42c3be7
f763096
f8a5620
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ import { | |
ResetPasswordPage, | ||
AccountSettingsPage, | ||
} from './views'; | ||
import { useUser } from './api/queries'; | ||
import { useSurvey, useUser } from './api/queries'; | ||
import { ROUTES } from './constants'; | ||
import { CentredLayout, BackgroundPageLayout, MainPageLayout } from './layout'; | ||
import { SurveyLayout, useSurveyForm } from './features'; | ||
|
@@ -102,14 +102,34 @@ const SurveyStartRedirect = () => { | |
const SurveyPageRedirect = ({ children }) => { | ||
const { screenNumber } = useParams(); | ||
const { visibleScreens } = useSurveyForm(); | ||
|
||
if (visibleScreens && visibleScreens.length && visibleScreens.length < Number(screenNumber)) { | ||
return <SurveyStartRedirect />; | ||
} | ||
return children; | ||
}; | ||
|
||
/** | ||
* This is to redirect the user to the survey not found page if they try to access a survey that does not exist | ||
*/ | ||
const SurveyNotFoundRedirect = ({ children }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good idea. |
||
const { surveyCode } = useParams(); | ||
const { isError, error } = useSurvey(surveyCode); | ||
if (isError) { | ||
return <NotFoundPage error={error as Error} title="Error fetching survey" />; | ||
} | ||
return children; | ||
}; | ||
|
||
export const SurveyPageRoutes = ( | ||
<Route path={ROUTES.SURVEY} element={<SurveyPage />}> | ||
<Route | ||
path={ROUTES.SURVEY} | ||
element={ | ||
<SurveyNotFoundRedirect> | ||
<SurveyPage /> | ||
</SurveyNotFoundRedirect> | ||
} | ||
> | ||
<Route index element={<SurveyStartRedirect />} /> | ||
<Route path={ROUTES.SURVEY_SUCCESS} element={<SurveySuccessScreen />} /> | ||
<Route element={<SurveyLayout />}> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import React, { ReactNode } from 'react'; | ||
import styled from 'styled-components'; | ||
import { Paper, Typography } from '@material-ui/core'; | ||
|
||
const Wrapper = styled.div` | ||
width: 100%; | ||
height: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
`; | ||
|
||
const Container = styled(Paper).attrs({ | ||
elevation: 0, | ||
})` | ||
padding: 1rem; | ||
width: 100%; | ||
max-width: 40rem; | ||
border: 1px solid ${({ theme }) => theme.palette.divider}; | ||
h1 { | ||
margin-bottom: 1rem; | ||
} | ||
${({ theme }) => theme.breakpoints.up('sm')} { | ||
padding: 1.5rem 2.5rem; | ||
} | ||
`; | ||
|
||
export const ErrorDisplay = ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also a great idea - it's handy to have this component |
||
error, | ||
children, | ||
title, | ||
}: { | ||
error?: Error | null; | ||
children?: ReactNode; | ||
title; | ||
}) => { | ||
return ( | ||
<Wrapper> | ||
<Container> | ||
<Typography variant="h1">{title}</Typography> | ||
{error && <Typography variant="body1">{error.message}</Typography>} | ||
{children} | ||
</Container> | ||
</Wrapper> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,22 @@ | |
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import React from 'react'; | ||
import { DialogActions } from '@material-ui/core'; | ||
import { Button, ErrorDisplay } from '../components'; | ||
|
||
export const NotFoundPage = () => { | ||
interface NotFoundPageProps { | ||
error?: Error; | ||
title?: string; | ||
} | ||
|
||
export const NotFoundPage = ({ error, title = '404: Page not found' }: NotFoundPageProps) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can I make a request to update the NotFoundPage to be called ErrorPage since it's now generic. For example we might use it for Permission Denied errors or something. Also would you be able to tweak the styles so the Paper is in the middle of the page 🙏 |
||
return ( | ||
<div> | ||
<h1>404</h1> | ||
<p>Page not found</p> | ||
</div> | ||
<ErrorDisplay title={title} error={error}> | ||
<DialogActions> | ||
<Button to="/" color="primary"> | ||
Return to home | ||
</Button> | ||
</DialogActions> | ||
</ErrorDisplay> | ||
); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah great find - I assume that this will fix the other places such as login where the request errors show up multiple times
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually think the errors showing up multiple times is a side effect of various queries being present on different screens and possibly causing re-renders when we have redirects. So from what I could tell, for the most part when we handle surveys not being found before it redirects infinitely, then this issue is handled (90% of the time)