Skip to content
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

Merged
merged 8 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/datatrak-web/src/AppProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/
import React, { ReactNode } from 'react';
import { MutationCache, QueryClient, QueryClientProvider } from 'react-query';
import { MutationCache, QueryCache, QueryClient, QueryClientProvider } from 'react-query';
import { ThemeProvider as MuiThemeProvider, StylesProvider } from '@material-ui/core/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import { ThemeProvider } from 'styled-components';
Expand All @@ -21,16 +21,20 @@ const defaultQueryClient = new QueryClient({
}
},
}),
queryCache: new QueryCache({
// use the errorToast function to display errors by default. If you want to override this, apply an meta.applyCustomErrorHandling to the mutation or an onError to the query
onError: (error: any, query) => {
if (!query?.meta || !query?.meta?.applyCustomErrorHandling) {
errorToast(error.message);
}
},
}),
defaultOptions: {
queries: {
staleTime: 1000 * 60 * 5, // 5 minutes
refetchOnWindowFocus: false,
retry: 0,
retry: false,
Copy link
Contributor

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

Copy link
Contributor Author

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)

keepPreviousData: false,
// use the errorToast function to display errors by default. If you want to override this, apply an onError function to the query
onError: (error: any) => {
errorToast(error.message);
},
},
},
});
Expand Down
24 changes: 22 additions & 2 deletions packages/datatrak-web/src/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea.
On a side note this file is getting a bit big. Maybe next time we add something we should split it up a bit

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 />}>
Expand Down
3 changes: 3 additions & 0 deletions packages/datatrak-web/src/api/queries/useSurvey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const useSurvey = (surveyCode?: string) => {
(): Promise<DatatrakWebSurveyRequest.ResBody> => get(`surveys/${surveyCode}`),
{
enabled: !!surveyCode,
meta: {
applyCustomErrorHandling: true,
},
},
);
};
51 changes: 51 additions & 0 deletions packages/datatrak-web/src/components/ErrorDisplay.tsx
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 = ({
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
);
};
1 change: 1 addition & 0 deletions packages/datatrak-web/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export { Tile } from './Tile';
export * from './Icons';
export { SmallModal } from './SmallModal';
export { CancelConfirmModal } from './CancelConfirmModal';
export { ErrorDisplay } from './ErrorDisplay';
20 changes: 15 additions & 5 deletions packages/datatrak-web/src/views/NotFoundPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
);
};
1 change: 0 additions & 1 deletion packages/datatrak-web/src/views/SurveyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
} from '../features';
import { SurveyParams } from '../types';
import { HEADER_HEIGHT, SURVEY_TOOLBAR_HEIGHT } from '../constants';

// wrap the entire page so that other content can be centered etc
const PageWrapper = styled.div`
display: flex;
Expand Down