Files
frontend-app-authoring/src/course-unit/SubsectionUnitRedirect.tsx
Chris Chávez dad736f9d1 refactor: Migration of course details to React query (#2724)
- Migrates the `courseDetails` part from the Redux Store to React Query.
- Creates a new `CourseAuthoringContext` 
- Update the pages in `<CourseAuthoringRoutes>` to use the newly created context.
- Migrates some files to Typescript
- Migrates some tests to use `src/testUtils.tsx`
2025-12-06 00:14:32 +00:00

33 lines
1.3 KiB
TypeScript

import { LoadingSpinner } from '@src/generic/Loading';
import { useCourseAuthoringContext } from '@src/CourseAuthoringContext';
import { useParams, Navigate } from 'react-router-dom';
import { useCourseItemData } from '../course-outline/data/apiHooks';
const SubsectionUnitRedirect = () => {
const { courseId } = useCourseAuthoringContext();
let { subsectionId } = useParams();
// if the call is made via the click on breadcrumbs the re won't be courseId available
// in such cases the page should redirect to the 1st unit of he subsection
const { data: courseItemData, isLoading } = useCourseItemData(subsectionId);
let firstUnitId = courseItemData?.childInfo?.children?.[0]?.id;
if (isLoading) {
return <LoadingSpinner />;
}
if (firstUnitId) {
firstUnitId = encodeURIComponent(firstUnitId);
return <Navigate replace to={`/course/${courseId}/container/${firstUnitId}`} />;
}
if (subsectionId) {
// if no unit then navigate to the subsection outline
subsectionId = encodeURIComponent(subsectionId);
return <Navigate replace to={`/course/${courseId}?show=${subsectionId}`} />;
}
// navigate to the course page if no subsectionId and no unitId
return <Navigate replace to={`/course/${courseId}`} />;
};
export default SubsectionUnitRedirect;