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 all 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 @@ -22,16 +22,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
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,
},
},
);
};
28 changes: 24 additions & 4 deletions packages/datatrak-web/src/routes/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
SurveySelectPage,
LoginPage,
VerifyEmailPage,
NotFoundPage,
ErrorPage,
RegisterPage,
VerifyEmailResendPage,
SurveyReviewScreen,
Expand All @@ -24,7 +24,7 @@ import {
AccountSettingsPage,
SurveyResponsePage,
} from '../views';
import { useCurrentUser } from '../api';
import { useCurrentUser, useSurvey } from '../api';
import { ROUTES } from '../constants';
import { CentredLayout, BackgroundPageLayout, MainPageLayout } from '../layout';
import { SurveyLayout, useSurveyForm } from '../features';
Expand Down Expand Up @@ -52,14 +52,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 }) => {
const { surveyCode } = useParams();
const { isError, error } = useSurvey(surveyCode);
if (isError) {
return <ErrorPage 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 Expand Up @@ -134,7 +154,7 @@ export const Routes = () => {
</PrivateRoute>
}
/>
<Route path="*" element={<NotFoundPage />} />
<Route path="*" element={<ErrorPage />} />
</Route>
</RouterRoutes>
);
Expand Down
24 changes: 24 additions & 0 deletions packages/datatrak-web/src/views/ErrorPage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Tupaia
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd
*/
import React from 'react';
import { DialogActions } from '@material-ui/core';
import { Button, ErrorDisplay } from '../components';

interface ErrorPageProps {
error?: Error;
title?: string;
}

export const ErrorPage = ({ error, title = '404: Page not found' }: ErrorPageProps) => {
return (
<ErrorDisplay title={title} error={error}>
<DialogActions>
<Button to="/" color="primary">
Return to home
</Button>
</DialogActions>
</ErrorDisplay>
);
};
14 changes: 0 additions & 14 deletions packages/datatrak-web/src/views/NotFoundPage.tsx

This file was deleted.

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 @@ -12,7 +12,6 @@ import { CancelConfirmModal } from '../components';
import { SurveyToolbar, useSurveyForm, useValidationResolver, SurveyContext } 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
2 changes: 1 addition & 1 deletion packages/datatrak-web/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export { LoginPage } from './LoginPage';
export { RegisterPage } from './RegisterPage';
export { VerifyEmailResendPage } from './VerifyEmailResendPage';
export { VerifyEmailPage } from './VerifyEmailPage';
export { NotFoundPage } from './NotFoundPage';
export { ErrorPage } from './ErrorPage';
export { ProjectSelectPage } from './ProjectSelectPage';
export { SurveyResponsePage } from './SurveyResponsePage';
export { SurveySuccessScreen, SurveyReviewScreen, SurveyScreen } from '../features';
Expand Down