FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor: read courseware route ids from useParams, not the Redux slice by brian-smith-tcril · Pull Request #2069 · openedx/frontend-app-learning · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (2) .jsx  (9) .ts  (2) .tsx  (2) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';

import { getConfig } from '@edx/frontend-platform';
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
Expand All @@ -16,7 +16,7 @@ import { logClick } from './utils';

const CatalogSuggestion = ({ variant }) => {
const intl = useIntl();
const { courseId } = useSelector(state => state.courseware);
const { courseId } = useParams();
const { org } = useModel('courseHomeMeta', courseId);
const { administrator } = getAuthenticatedUser();

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { faLinkedinIn } from '@fortawesome/free-brands-svg-icons';

import { FormattedDate, FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
import { Helmet } from 'react-helmet';
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import {
Alert,
breakpoints,
Expand Down Expand Up @@ -37,7 +37,7 @@ const LINKEDIN_BLUE = '#2867B2';
const CourseCelebration = () => {
const intl = useIntl();
const wideScreen = useWindowSize().width >= breakpoints.medium.minWidth;
const { courseId } = useSelector(state => state.courseware);
const { courseId } = useParams();
const requestCert = useRequestCert();
const {
certificateData,
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { QueryClientProvider } from '@tanstack/react-query';

import { getCourseMetadata } from '../../data/api';
import { getCourseHomeCourseMetadata } from '../../../course-home/data/api';
import { fetchCourseSuccess } from '../../data/slice';
import { addModel } from '../../../generic/model-store';
import { buildSimpleCourseBlocks } from '../../../shared/data/__factories__/courseBlocks.factory';
import { buildOutlineFromBlocks } from '../../data/__factories__/learningSequencesOutline.factory';
Expand Down Expand Up @@ -62,7 +61,6 @@ describe('Course Exit Pages', () => {
]);
store.dispatch(addModel({ modelType: 'coursewareMeta', model: metadata }));
store.dispatch(addModel({ modelType: 'courseHomeMeta', model: { id: courseId, ...homeMetadata } }));
store.dispatch(fetchCourseSuccess({ courseId }));
history.push(`/course/${courseId}`);
render(
<QueryClientProvider client={createTestQueryClient(store)}>
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect } from 'react';
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Helmet } from 'react-helmet';
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import { Alert, Button } from '@openedx/paragon';
import { getConfig } from '@edx/frontend-platform';

Expand All @@ -16,7 +16,7 @@ import { logClick, logVisit } from './utils';

const CourseInProgress = () => {
const intl = useIntl();
const { courseId } = useSelector(state => state.courseware);
const { courseId } = useParams();
const {
org,
tabs,
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect } from 'react';
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Helmet } from 'react-helmet';
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import { Alert, Button } from '@openedx/paragon';
import { getConfig } from '@edx/frontend-platform';

Expand All @@ -16,7 +16,7 @@ import { logClick, logVisit } from './utils';

const CourseNonPassing = () => {
const intl = useIntl();
const { courseId } = useSelector(state => state.courseware);
const { courseId } = useParams();
const {
org,
tabs,
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
import {
FormattedMessage, useIntl, defineMessages,
} from '@edx/frontend-platform/i18n';
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import {
Hyperlink, DataTable, CardView, Card,
} from '@openedx/paragon';
Expand Down Expand Up @@ -132,7 +132,7 @@ const IntlCard = CourseCard;

const CourseRecommendations = ({ variant }) => {
const intl = useIntl();
const courseId = useSelector(state => state.courseware.courseId);
const { courseId } = useParams();
const { org, number } = useModel('courseHomeMeta', courseId);

const courseKey = `${org}+${number}`;
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import PropTypes from 'prop-types';

import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
Expand All @@ -14,7 +14,7 @@ import { useModel } from '../../../generic/model-store';

const UpgradeFootnote = ({ deadline, href }) => {
const intl = useIntl();
const { courseId } = useSelector(state => state.courseware);
const { courseId } = useParams();
const { org } = useModel('courseHomeMeta', courseId);
const { administrator } = getAuthenticatedUser();

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ jest.mock('react', () => ({
useCallback: jest.fn((cb, prereqs) => ({ cb, prereqs })),
}));

jest.mock('react-redux', () => ({
useSelector: jest.fn(),
}));

jest.mock('@edx/frontend-platform/logging', () => ({
logError: jest.fn(),
}));
Expand All @@ -56,6 +52,7 @@ jest.mock('@src/generic/model-store', () => ({
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockNavigate,
useParams: () => ({ sequenceId: 'test-sequence-id' }),
}));

jest.mock('@src/courseware/course/sequence/sequence-navigation/hooks');
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React, { useState } from 'react';
import { camelCaseObject, getConfig } from '@edx/frontend-platform';
import { sendTrackEvent } from '@edx/frontend-platform/analytics';
import { useSelector } from 'react-redux';
import { useNavigate } from 'react-router-dom';
import { useNavigate, useParams } from 'react-router-dom';
import { useQueryClient } from '@tanstack/react-query';
import { throttle } from 'lodash';

Expand All @@ -13,7 +12,6 @@ import { courseHomeQueryKeys } from '@src/course-home/data/queryKeys';
import { coursewareQueryKeys } from '@src/courseware/data/queryKeys';
import { eventTypes } from '@src/course-home/data/thunks';
import { useEventListener } from '@src/generic/hooks';
import { getSequenceId } from '@src/courseware/data/selectors';
import { useModel } from '@src/generic/model-store';
import { useSequenceNavigationMetadata } from '@src/courseware/course/sequence/sequence-navigation/hooks';
import { messageTypes } from '../constants';
Expand All @@ -38,7 +36,7 @@ const useIFrameBehavior = ({

const queryClient = useQueryClient();
const postEvent = usePostEvent();
const activeSequenceId = useSelector(getSequenceId);
const { sequenceId: activeSequenceId } = useParams();
const navigate = useNavigate();
const activeSequence = useModel('sequences', activeSequenceId);
const activeUnitId = activeSequence.unitIds?.length > 0
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { Factory } from 'rosie';
import {
fireEvent, initializeTestStore, render, screen,
Expand Down Expand Up @@ -102,7 +102,9 @@ describe('Unit Button', () => {
const unitPath = `/course/${courseId}/${sequenceId}/${unit.id}`;
render(
<MemoryRouter initialEntries={[`/preview${unitPath}`]}>
<UnitButton {...mockData} />
<Routes>
<Route path="/preview/course/:courseId/:sequenceId/:unitId" element={<UnitButton {...mockData} />} />
</Routes>
</MemoryRouter>,
);
expect(screen.getByRole('link')).toHaveAttribute('href', `/preview${unitPath}`);
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { useCallback } from 'react';
import { Link, useLocation } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { Link, useLocation, useParams } from 'react-router-dom';
import classNames from 'classnames';
import { Button, Icon } from '@openedx/paragon';
import { Bookmark } from '@openedx/paragon/icons';

import { useModel } from '../../../../generic/model-store';
import type { RootState } from '../../../../store';
import UnitIcon from './UnitIcon';
import CompleteIcon from './CompleteIcon';

Expand Down Expand Up @@ -37,7 +35,7 @@ const UnitButton = ({
bookmarked = false,
complete = false,
} = useModel('units', unitId);
const { courseId, sequenceId } = useSelector((state: RootState) => state.courseware);
const { courseId, sequenceId } = useParams();
const { pathname } = useLocation();
const basePath = `/course/${courseId}/${sequenceId}/${unitId}`;
const unitPath = pathname.startsWith('/preview') ? `/preview${basePath}` : basePath;
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ export const CourseOutline = () => {
handleToggleCollapse,
} = useCourseOutlineSidebar();

const resolvedSectionId = selectedSection
|| Object.keys(sections).find(
(sectionId):boolean => sections[sectionId].sequenceIds.includes(activeSequenceId),
)!;
const activeSectionId = activeSequenceId && Object.keys(sections)
.find((sectionId) => sections[sectionId].sequenceIds.includes(activeSequenceId));
const resolvedSectionId = (selectedSection || activeSectionId)!;
const sectionsIds = Object.keys(sections);
const sequenceIds: string[] = sections[resolvedSectionId]?.sequenceIds || [];
const backButtonTitle: string | undefined = sections[resolvedSectionId]?.title;
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('<CourseOutlineTray />', () => {
let unit;
let unitId;
let courseId;
let activeSequenceId;
let mockData;

const { innerWidth: originalInnerWidth, innerHeight: originalInnerHeight } = window;
Expand All @@ -33,11 +34,12 @@ describe('<CourseOutlineTray />', () => {
store = await initializeTestStore(options);
const state = store.getState();
courseId = state.courseware.courseId;
[activeSequenceId] = Object.keys(state.models.sequences);
[unitId] = Object.keys(state.models.units);

if (!options?.preventOutlineSidebarLoad) {
const outline = await getCourseOutline(courseId);
const [activeSequenceId] = Object.keys(outline.sequences);
[activeSequenceId] = Object.keys(outline.sequences);
sequence = outline.sequences[activeSequenceId];
const activeSectionId = Object.keys(outline.sections)[0];
section = outline.sections[activeSectionId];
Expand All @@ -59,9 +61,9 @@ describe('<CourseOutlineTray />', () => {
<QueryClientProvider client={createTestQueryClient(store)}>
<IntlProvider locale="en">
<SidebarContext.Provider value={{ ...mockData, ...testData }}>
<MemoryRouter initialEntries={[`/course/${courseId}`]}>
<MemoryRouter initialEntries={[`/course/${courseId}/${activeSequenceId}/${unitId}`]}>
<Routes>
<Route path="/course/:courseId" element={<CourseOutlineTray />} />
<Route path="/course/:courseId/:sequenceId/:unitId" element={<CourseOutlineTray />} />
</Routes>
</MemoryRouter>
</SidebarContext.Provider>
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
useContext, useLayoutEffect, useRef, useState,
} from 'react';
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import { sendTrackEvent, sendTrackingLogEvent } from '@edx/frontend-platform/analytics';
import { breakpoints } from '@openedx/paragon';
Expand All @@ -12,20 +11,14 @@ import {
useCourseOutlineStructure,
useCoursewareOutlineSidebarToggles,
} from '@src/courseware/data/apiHooks';
import {
getSequenceId,
getSequenceStatus,
} from '@src/courseware/data/selectors';
import SidebarContext from '../../SidebarContext';
import { ID } from './constants';

// eslint-disable-next-line import/prefer-default-export
export const useCourseOutlineSidebar = () => {
const checkBlockCompletion = useCheckBlockCompletion();
const sequenceStatus = useSelector(getSequenceStatus);
const activeSequenceId = useSelector(getSequenceId);

const { courseId } = useParams();
const { courseId, sequenceId: activeSequenceId } = useParams();
const { data: sidebarToggles } = useCoursewareOutlineSidebarToggles(courseId);
const isEnabledCompletionTracking = sidebarToggles?.enableCompletionTracking;
const outlineQuery = useCourseOutlineStructure(courseId);
Expand Down Expand Up @@ -134,6 +127,5 @@ export const useCourseOutlineSidebar = () => {
sequences,
units,
handleUnitClick,
sequenceStatus,
};
};
7 changes: 2 additions & 5 deletions src/data/hooks.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { useSelector } from 'react-redux';
import { RootState } from '../store';
import { useParams } from 'react-router-dom';

// eslint-disable-next-line import/prefer-default-export
export const useContextId = () => useSelector<RootState>(
state => state.courseware.courseId ?? state.courseHome.courseId,
);
export const useContextId = () => useParams().courseId;
Loading

Back | FazBrowse Home | New Git URL