Skip to content

Commit

Permalink
fix(materials): display custom error page when file or folder is miss…
Browse files Browse the repository at this point in the history
…ing in course materials
  • Loading branch information
phungmanhcuong committed Oct 24, 2024
1 parent 80f1e6b commit 2df7c03
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { Typography } from '@mui/material';

import Page from 'lib/components/core/layouts/Page';

interface BaseDownloadFilePageProps {
interface BaseRetrieveMaterialPageProps {
illustration: ReactNode;
title: string;
description: string;
children?: ReactNode;
}

const BaseDownloadFilePage = (
props: BaseDownloadFilePageProps,
const BaseRetrieveMaterialPage = (
props: BaseRetrieveMaterialPageProps,
): JSX.Element => (
<Page className="h-full m-auto flex flex-col items-center justify-center text-center">
{props.illustration}
Expand All @@ -32,4 +32,4 @@ const BaseDownloadFilePage = (
</Page>
);

export default BaseDownloadFilePage;
export default BaseRetrieveMaterialPage;
10 changes: 5 additions & 5 deletions client/app/bundles/course/material/files/DownloadingFilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Link from 'lib/components/core/Link';
import useEffectOnce from 'lib/hooks/useEffectOnce';
import useTranslation from 'lib/hooks/useTranslation';

import BaseDownloadFilePage from './components/BaseDownloadFilePage';
import BaseRetrievingPage from '../BaseRetrieveMaterialPage';

const DEFAULT_FILE_NAME = 'file';

Expand Down Expand Up @@ -51,7 +51,7 @@ const SuccessDownloadingFilePage = (
const { t } = useTranslation();

return (
<BaseDownloadFilePage
<BaseRetrievingPage
description={t(translations.downloadingDescription)}
illustration={
<DownloadingOutlined className="text-[6rem]" color="success" />
Expand All @@ -61,7 +61,7 @@ const SuccessDownloadingFilePage = (
<Link className="mt-10" href={props.url}>
{t(translations.tryDownloadingAgain)}
</Link>
</BaseDownloadFilePage>
</BaseRetrievingPage>
);
};

Expand All @@ -71,7 +71,7 @@ const ErrorStartingDownloadFilePage = (
const { t } = useTranslation();

return (
<BaseDownloadFilePage
<BaseRetrievingPage
description={t(translations.clickToDownloadFileDescription)}
illustration={
<div className="relative">
Expand All @@ -93,7 +93,7 @@ const ErrorStartingDownloadFilePage = (
>
{props.name}
</Button>
</BaseDownloadFilePage>
</BaseRetrievingPage>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Cancel, InsertDriveFileOutlined } from '@mui/icons-material';
import Link from 'lib/components/core/Link';
import useTranslation from 'lib/hooks/useTranslation';

import BaseDownloadFilePage from './components/BaseDownloadFilePage';
import BaseRetrievingPage from '../BaseRetrieveMaterialPage';

const translations = defineMessages({
problemRetrievingFile: {
Expand All @@ -30,7 +30,7 @@ const ErrorRetrievingFilePage = (): JSX.Element => {
const workbinURL = `/courses/${params.courseId}/materials/folders/${params.folderId}`;

return (
<BaseDownloadFilePage
<BaseRetrievingPage
description={t(translations.problemRetrievingFileDescription)}
illustration={
<div className="relative">
Expand All @@ -47,7 +47,7 @@ const ErrorRetrievingFilePage = (): JSX.Element => {
<Link className="mt-10" to={workbinURL}>
{t(translations.goToTheWorkbin)}
</Link>
</BaseDownloadFilePage>
</BaseRetrievingPage>
);
};

Expand Down
15 changes: 15 additions & 0 deletions client/app/bundles/course/material/folderLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { LoaderFunction, redirect } from 'react-router-dom';
import { getIdFromUnknown } from 'utilities';

import CourseAPI from 'api/course';

const folderLoader: LoaderFunction = async ({ params }) => {
const folderId = getIdFromUnknown(params?.folderId);
if (!folderId) return redirect('/');

const { data } = await CourseAPI.folders.fetch(folderId);

return data;
};

export default folderLoader;
29 changes: 19 additions & 10 deletions client/app/bundles/course/material/folders/handles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@ const getFolderTitle = async (
courseUrl: string,
folderId: number,
): Promise<CrumbPath> => {
const { data } = await CourseAPI.folders.fetch(folderId);
try {
const { data } = await CourseAPI.folders.fetch(folderId);
const workbinUrl = `${courseUrl}/materials/folders/${data.breadcrumbs[0].id}`;

const workbinUrl = `${courseUrl}/materials/folders/${data.breadcrumbs[0].id}`;

return {
activePath: workbinUrl,
content: data.breadcrumbs.map((crumb) => ({
title: crumb.name,
url: `materials/folders/${crumb.id}`,
})),
};
return {
activePath: workbinUrl,
content: data.breadcrumbs.map((crumb) => ({
title: crumb.name,
url: `materials/folders/${crumb.id}`,
})),
};
} catch (error) {
return {
activePath: courseUrl,
content: {
title: 'Folder Not Found',
url: courseUrl,
},
};
}
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import { defineMessages } from 'react-intl';
import { useParams } from 'react-router-dom';
import { Cancel, FolderOutlined } from '@mui/icons-material';

import Link from 'lib/components/core/Link';
import useTranslation from 'lib/hooks/useTranslation';

import BaseRetrievePage from '../../BaseRetrieveMaterialPage';

const translations = defineMessages({
problemRetrievingFolder: {
id: 'course.material.folders.ErrorRetrievingFolderPage.problemRetrievingFolder',
defaultMessage: 'Problem retrieving folder',
},
problemRetrievingFolderDescription: {
id: 'course.material.folders.ErrorRetrievingFolderPage.problemRetrievingFolderDescription',
defaultMessage:
"Either it no longer exists, you don't have the permission to access it, or something unexpected happened when we were trying to retrieve it.",
},
goToTheWorkbin: {
id: 'course.material.folders.ErrorRetrievingFolderPage.goToTheWorkbin',
defaultMessage: 'Go to the Workbin',
},
});

const ErrorRetrievingFolderPage = (): JSX.Element => {
const { t } = useTranslation();

const params = useParams();
const workbinURL = `/courses/${params.courseId}`;

return (
<BaseRetrievePage
description={t(translations.problemRetrievingFolderDescription)}
illustration={
<div className="relative">
<FolderOutlined className="text-[6rem]" color="disabled" />

<Cancel
className="absolute bottom-0 -right-2 text-[4rem] bg-white rounded-full"
color="error"
/>
</div>
}
title={t(translations.problemRetrievingFolder)}
>
<Link className="mt-10" to={workbinURL}>
{t(translations.goToTheWorkbin)}
</Link>
</BaseRetrievePage>
);
};

export default ErrorRetrievingFolderPage;
4 changes: 4 additions & 0 deletions client/app/routers/AuthenticatedApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import LessonPlanShow from 'bundles/course/lesson-plan/pages/LessonPlanShow';
import LevelsIndex from 'bundles/course/level/pages/LevelsIndex';
import DownloadingFilePage from 'bundles/course/material/files/DownloadingFilePage';
import ErrorRetrievingFilePage from 'bundles/course/material/files/ErrorRetrievingFilePage';
import ErrorRetrievingFolderPage from 'bundles/course/material/folders/pages/ErrorRetrievingFolderPage';
import FolderShow from 'bundles/course/material/folders/pages/FolderShow';
import TimelineDesigner from 'bundles/course/reference-timelines/TimelineDesigner';
import ResponseEdit from 'bundles/course/survey/pages/ResponseEdit';
Expand Down Expand Up @@ -127,6 +128,7 @@ import {
forumTopicHandle,
} from 'course/forum/handles';
import { leaderboardHandle } from 'course/leaderboard/handles';
import folderLoader from 'course/material/folderLoader';
import { folderHandle } from 'course/material/folders/handles';
import materialLoader from 'course/material/materialLoader';
import { videoWatchHistoryHandle } from 'course/statistics/handles';
Expand Down Expand Up @@ -221,7 +223,9 @@ const authenticatedRouter: Translated<RouteObject[]> = (t) =>
children: [
{
index: true,
loader: folderLoader,
element: <FolderShow />,
errorElement: <ErrorRetrievingFolderPage />,
},
{
path: 'files/:materialId',
Expand Down

0 comments on commit 2df7c03

Please sign in to comment.