fix: fixed rerun link (#1023)
* fix: fixed rerun link * refactor: code refactoring * refactor: updated tests * refactor: after review
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { useIntl } from '@edx/frontend-platform/i18n';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
import { RequestStatus } from '../data/constants';
|
||||
import { updateSavingStatus } from '../generic/data/slice';
|
||||
import {
|
||||
getSavingStatus,
|
||||
getRedirectUrlObj,
|
||||
getCourseRerunData,
|
||||
getCourseData,
|
||||
} from '../generic/data/selectors';
|
||||
@@ -17,11 +15,9 @@ import { fetchStudioHomeData } from '../studio-home/data/thunks';
|
||||
const useCourseRerun = (courseId) => {
|
||||
const intl = useIntl();
|
||||
const dispatch = useDispatch();
|
||||
const navigate = useNavigate();
|
||||
const savingStatus = useSelector(getSavingStatus);
|
||||
const courseData = useSelector(getCourseData);
|
||||
const courseRerunData = useSelector(getCourseRerunData);
|
||||
const redirectUrlObj = useSelector(getRedirectUrlObj);
|
||||
|
||||
const {
|
||||
displayName = '',
|
||||
@@ -46,10 +42,6 @@ const useCourseRerun = (courseId) => {
|
||||
useEffect(() => {
|
||||
if (savingStatus === RequestStatus.SUCCESSFUL) {
|
||||
dispatch(updateSavingStatus({ status: '' }));
|
||||
const { url } = redirectUrlObj;
|
||||
if (url) {
|
||||
navigate('/home');
|
||||
}
|
||||
}
|
||||
}, [savingStatus]);
|
||||
|
||||
|
||||
@@ -27,11 +27,14 @@ import messages from './messages';
|
||||
import { CreateOrRerunCourseForm } from '.';
|
||||
import { initialState } from './factories/mockApiResponses';
|
||||
|
||||
const mockedUsedNavigate = jest.fn();
|
||||
|
||||
jest.mock('react-router-dom', () => ({
|
||||
...jest.requireActual('react-router-dom'),
|
||||
useParams: () => ({
|
||||
courseId: 'course-id-mock',
|
||||
}),
|
||||
useNavigate: () => mockedUsedNavigate,
|
||||
}));
|
||||
|
||||
let axiosMock;
|
||||
@@ -134,8 +137,6 @@ describe('<CreateOrRerunCourseForm />', () => {
|
||||
});
|
||||
|
||||
describe('handleOnClickCreate', () => {
|
||||
delete window.location;
|
||||
window.location = { assign: jest.fn() };
|
||||
it('should call window.location.assign with url', async () => {
|
||||
render(<RootWrapper {...props} />);
|
||||
await mockStore();
|
||||
@@ -156,7 +157,7 @@ describe('<CreateOrRerunCourseForm />', () => {
|
||||
await axiosMock.onPost(getCreateOrRerunCourseUrl()).reply(200, { url });
|
||||
await executeThunk(updateCreateOrRerunCourseQuery({ org: 'testX', run: 'some' }), store.dispatch);
|
||||
|
||||
expect(window.location.assign).toHaveBeenCalledWith(`${process.env.STUDIO_BASE_URL}${url}`);
|
||||
expect(mockedUsedNavigate).toHaveBeenCalledWith(url);
|
||||
});
|
||||
it('should call window.location.assign with url and destinationCourseKey', async () => {
|
||||
render(<RootWrapper {...props} />);
|
||||
@@ -179,7 +180,7 @@ describe('<CreateOrRerunCourseForm />', () => {
|
||||
});
|
||||
await executeThunk(updateCreateOrRerunCourseQuery({ org: 'testX', run: 'some' }), store.dispatch);
|
||||
|
||||
expect(window.location.assign).toHaveBeenCalledWith(`${process.env.STUDIO_BASE_URL}${url}${destinationCourseKey}`);
|
||||
expect(mockedUsedNavigate).toHaveBeenCalledWith(`${url}${destinationCourseKey}`);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { getConfig } from '@edx/frontend-platform';
|
||||
import { useIntl } from '@edx/frontend-platform/i18n';
|
||||
import { useFormik } from 'formik';
|
||||
import * as Yup from 'yup';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
import { RequestStatus } from '../../data/constants';
|
||||
import { getStudioHomeData } from '../../studio-home/data/selectors';
|
||||
@@ -20,6 +20,7 @@ import messages from './messages';
|
||||
const useCreateOrRerunCourse = (initialValues) => {
|
||||
const intl = useIntl();
|
||||
const dispatch = useDispatch();
|
||||
const navigate = useNavigate();
|
||||
const redirectUrlObj = useSelector(getRedirectUrlObj);
|
||||
const createOrRerunCourseSavingStatus = useSelector(getSavingStatus);
|
||||
const allOrganizations = useSelector(getOrganizations);
|
||||
@@ -93,9 +94,9 @@ const useCreateOrRerunCourse = (initialValues) => {
|
||||
// is in the destionationCourseKey attribute from the api.
|
||||
if (url) {
|
||||
if (destinationCourseKey) {
|
||||
window.location.assign(`${getConfig().STUDIO_BASE_URL}${url}${destinationCourseKey}`);
|
||||
navigate(`${url}${destinationCourseKey}`);
|
||||
} else {
|
||||
window.location.assign(`${getConfig().STUDIO_BASE_URL}${url}`);
|
||||
navigate(url);
|
||||
}
|
||||
}
|
||||
} else if (createOrRerunCourseSavingStatus === RequestStatus.FAILED) {
|
||||
|
||||
Reference in New Issue
Block a user