Skip to content

Commit

Permalink
Use route-level error boundary for task selection page
Browse files Browse the repository at this point in the history
In the case of a 404 error, present a "Project Not Found" message like before and for the rest of the error while fetching project summary, the fallback error component will be displayed.
  • Loading branch information
HelNershingThapa committed Sep 14, 2023
1 parent 352e23e commit 752be28
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
1 change: 1 addition & 0 deletions frontend/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const router = createBrowserRouter(
);
return { Component: SelectTask };
}}
ErrorBoundary={FallbackComponent}
/>
<Route
path="projects/:id/map"
Expand Down
16 changes: 5 additions & 11 deletions frontend/src/views/taskSelection.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { useEffect } from 'react';
import { useSelector } from 'react-redux';
import { useNavigate, useParams } from 'react-router-dom';
import { ErrorBoundary } from 'react-error-boundary';

import { TaskSelection } from '../components/taskSelection';
import { NotFound } from './notFound';
import { useProjectSummaryQuery } from '../api/projects';
import { Preloader } from '../components/preloader';
import { FallbackComponent } from './fallback';

const ProjectError = ({ error }) => <span>Error:{error.message}</span>;

export function SelectTask() {
const { id } = useParams();
const navigate = useNavigate();
const token = useSelector((state) => state.auth.token);
const { data, status, error } = useProjectSummaryQuery(id);
const { data, status, error } = useProjectSummaryQuery(id, {
useErrorBoundary: (error) => error.response.status !== 404,
});


useEffect(() => {
if (!token) {
Expand All @@ -31,12 +30,7 @@ export function SelectTask() {
if (error.response.status === 404) {
return <NotFound projectId={id} />;
}
return <ProjectError error={error} />;
}

return (
<ErrorBoundary FallbackComponent={FallbackComponent}>
<TaskSelection project={data} />
</ErrorBoundary>
);
return <TaskSelection project={data} />;
}

0 comments on commit 752be28

Please sign in to comment.