-
Notifications
You must be signed in to change notification settings - Fork 295
feat: added support to check if active enterprise is same as EnterpriseCourseEnrollment object #967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/alerts/active-enteprise-alert/ActiveEnterpriseAlert.jsx
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 |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import React from 'react'; | ||
| import { FormattedMessage, injectIntl, intlShape } from '@edx/frontend-platform/i18n'; | ||
| import PropTypes from 'prop-types'; | ||
| import { Alert, Hyperlink } from '@edx/paragon'; | ||
| import { WarningFilled } from '@edx/paragon/icons'; | ||
|
|
||
| import { getConfig } from '@edx/frontend-platform'; | ||
| import genericMessages from './messages'; | ||
|
|
||
| function ActiveEnterpriseAlert({ intl, payload }) { | ||
| const { text } = payload; | ||
| const changeActiveEnterprise = ( | ||
| <Hyperlink | ||
| style={{ textDecoration: 'underline' }} | ||
| destination={ | ||
| `${getConfig().LMS_BASE_URL}/enterprise/select/active/?success_url=${encodeURIComponent(global.location.href)}` | ||
| } | ||
| > | ||
| {intl.formatMessage(genericMessages.changeActiveEnterpriseLowercase)} | ||
| </Hyperlink> | ||
| ); | ||
|
|
||
| return ( | ||
| <Alert variant="warning" icon={WarningFilled}> | ||
| {text} | ||
| <FormattedMessage | ||
| id="learning.activeEnterprise.alert" | ||
| description="Prompts the user to log-in with the correct enterprise to access the course content." | ||
| defaultMessage=" {changeActiveEnterprise}." | ||
| values={{ | ||
| changeActiveEnterprise, | ||
| }} | ||
| /> | ||
| </Alert> | ||
| ); | ||
| } | ||
|
|
||
| ActiveEnterpriseAlert.propTypes = { | ||
| intl: intlShape.isRequired, | ||
| payload: PropTypes.shape({ | ||
| text: PropTypes.string, | ||
| }).isRequired, | ||
| }; | ||
|
|
||
| export default injectIntl(ActiveEnterpriseAlert); | ||
26 changes: 26 additions & 0 deletions
26
src/alerts/active-enteprise-alert/ActiveEnterpriseAlert.test.jsx
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import React from 'react'; | ||
| import { getConfig } from '@edx/frontend-platform'; | ||
| import { | ||
| initializeTestStore, render, screen, | ||
| } from '../../setupTest'; | ||
| import ActiveEnterpriseAlert from './ActiveEnterpriseAlert'; | ||
|
|
||
| describe('ActiveEnterpriseAlert', () => { | ||
| const mockData = { | ||
| payload: { | ||
| text: 'test message', | ||
| }, | ||
| }; | ||
| beforeAll(async () => { | ||
| await initializeTestStore({ excludeFetchCourse: true, excludeFetchSequence: true }); | ||
| }); | ||
|
|
||
| it('Shows alert message and links', () => { | ||
| render(<ActiveEnterpriseAlert {...mockData} />); | ||
| expect(screen.getByRole('alert')).toBeInTheDocument(); | ||
| expect(screen.getByText('test message')).toBeInTheDocument(); | ||
| expect(screen.getByRole('link', { name: 'change enterprise now' })).toHaveAttribute( | ||
| 'href', `${getConfig().LMS_BASE_URL}/enterprise/select/active/?success_url=http%3A%2F%2Flocalhost%2F`, | ||
| ); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import React, { useMemo } from 'react'; | ||
| import { ALERT_TYPES, useAlert } from '../../generic/user-messages'; | ||
| import { useModel } from '../../generic/model-store'; | ||
|
|
||
| const ActiveEnterpriseAlert = React.lazy(() => import('./ActiveEnterpriseAlert')); | ||
|
|
||
| export default function useActiveEnterpriseAlert(courseId) { | ||
| const { courseAccess } = useModel('courseHomeMeta', courseId); | ||
| /** | ||
| * This alert should render if | ||
| * 1. course access code is incorrect_active_enterprise | ||
| */ | ||
| const isVisible = courseAccess && !courseAccess.hasAccess && courseAccess.errorCode === 'incorrect_active_enterprise'; | ||
|
|
||
| const payload = { | ||
| text: courseAccess && courseAccess.userMessage, | ||
| courseId, | ||
| }; | ||
| useAlert(isVisible, { | ||
| code: 'clientActiveEnterpriseAlert', | ||
| topic: 'outline', | ||
| dismissible: false, | ||
| type: ALERT_TYPES.ERROR, | ||
| payload: useMemo(() => payload, Object.values(payload).sort()), | ||
| }); | ||
|
|
||
| return { clientActiveEnterpriseAlert: ActiveEnterpriseAlert }; | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import useActiveEnterpriseAlert from './hooks'; | ||
|
|
||
| export default useActiveEnterpriseAlert; |
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { defineMessages } from '@edx/frontend-platform/i18n'; | ||
|
|
||
| const messages = defineMessages({ | ||
| changeActiveEnterpriseLowercase: { | ||
| id: 'learning.activeEnterprise.change.alert', | ||
| defaultMessage: 'change enterprise now', | ||
| description: 'Text in a link, prompting the user to change active enterprise. Used in learning.activeEnterprise.change.alert"', | ||
| }, | ||
| }); | ||
|
|
||
| export default messages; |
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
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 |
|---|---|---|
|
|
@@ -40,6 +40,12 @@ export default () => { | |
| global.location.assign(`${getConfig().LMS_BASE_URL}${consentPath}`); | ||
| }} | ||
| /> | ||
| <PageRoute | ||
| path={`${path}/home/:courseId`} | ||
| render={({ match }) => { | ||
| global.location.assign(`/course/${match.params.courseId}/home`); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use Redirect or history.push
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this whole file we are using the |
||
| }} | ||
| /> | ||
| </Switch> | ||
| </div> | ||
| ); | ||
|
|
||
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
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 |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import React, { useEffect } from 'react'; | ||
| import { LearningHeader as Header } from '@edx/frontend-component-header'; | ||
| import Footer from '@edx/frontend-component-footer'; | ||
| import { useParams } from 'react-router-dom'; | ||
| import { useDispatch, useSelector } from 'react-redux'; | ||
| import { Redirect } from 'react-router'; | ||
| import { injectIntl, intlShape } from '@edx/frontend-platform/i18n'; | ||
| import useActiveEnterpriseAlert from '../alerts/active-enteprise-alert'; | ||
| import { AlertList } from './user-messages'; | ||
| import { fetchDiscussionTab } from '../course-home/data/thunks'; | ||
| import { LOADED, LOADING } from '../course-home/data/slice'; | ||
| import PageLoading from './PageLoading'; | ||
| import messages from '../tab-page/messages'; | ||
|
|
||
| function CourseAccessErrorPage({ intl }) { | ||
| const { courseId } = useParams(); | ||
|
|
||
| const dispatch = useDispatch(); | ||
| const activeEnterpriseAlert = useActiveEnterpriseAlert(courseId); | ||
| useEffect(() => { | ||
| dispatch(fetchDiscussionTab(courseId)); | ||
| }, [courseId]); | ||
|
|
||
| const { | ||
| courseStatus, | ||
| } = useSelector(state => state.courseHome); | ||
|
|
||
| if (courseStatus === LOADING) { | ||
| return ( | ||
| <> | ||
| <Header /> | ||
| <PageLoading | ||
| srMessage={intl.formatMessage(messages.loading)} | ||
| /> | ||
| <Footer /> | ||
| </> | ||
| ); | ||
| } | ||
| if (courseStatus === LOADED) { | ||
| return (<Redirect to={`/redirect/home/${courseId}`} />); | ||
| } | ||
| return ( | ||
| <> | ||
| <Header /> | ||
| <main id="main-content" className="container my-5 text-center" data-testid="access-denied-main"> | ||
| <AlertList | ||
| topic="outline" | ||
| className="mx-5 mt-3" | ||
| customAlerts={{ | ||
| ...activeEnterpriseAlert, | ||
| }} | ||
| /> | ||
| </main> | ||
| <Footer /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| CourseAccessErrorPage.propTypes = { | ||
| intl: intlShape.isRequired, | ||
| }; | ||
|
|
||
| export default injectIntl(CourseAccessErrorPage); |
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import React from 'react'; | ||
| import { history } from '@edx/frontend-platform'; | ||
| import { Route } from 'react-router'; | ||
| import { initializeTestStore, render, screen } from '../setupTest'; | ||
| import CourseAccessErrorPage from './CourseAccessErrorPage'; | ||
|
|
||
| const mockDispatch = jest.fn(); | ||
| let mockCourseStatus; | ||
| jest.mock('react-redux', () => ({ | ||
| ...jest.requireActual('react-redux'), | ||
| useDispatch: () => mockDispatch, | ||
| useSelector: () => ({ courseStatus: mockCourseStatus }), | ||
| })); | ||
| jest.mock('./PageLoading', () => () => <div data-testid="page-loading" />); | ||
|
|
||
| describe('CourseAccessErrorPage', () => { | ||
| let courseId; | ||
| let accessDeniedUrl; | ||
| beforeEach(async () => { | ||
| const store = await initializeTestStore({ excludeFetchSequence: true }); | ||
| courseId = store.getState().courseware.courseId; | ||
| accessDeniedUrl = `/course/${courseId}/access-denied`; | ||
| history.push(accessDeniedUrl); | ||
| }); | ||
|
|
||
| it('Displays loading in start on page rendering', () => { | ||
| mockCourseStatus = 'loading'; | ||
| render( | ||
| <Route path="/course/:courseId/access-denied"> | ||
| <CourseAccessErrorPage /> | ||
| </Route>, | ||
| ); | ||
| expect(screen.getByTestId('page-loading')).toBeInTheDocument(); | ||
| expect(history.location.pathname).toBe(accessDeniedUrl); | ||
| }); | ||
|
|
||
| it('Redirect user to homepage if user has access', () => { | ||
| mockCourseStatus = 'loaded'; | ||
| render( | ||
| <Route path="/course/:courseId/access-denied"> | ||
| <CourseAccessErrorPage /> | ||
| </Route>, | ||
| ); | ||
| expect(history.location.pathname).toBe('/redirect/home/course-v1:edX+DemoX+Demo_Course'); | ||
| }); | ||
|
|
||
| it('For access denied it should render access denied page', () => { | ||
| mockCourseStatus = 'denied'; | ||
|
|
||
| render( | ||
| <Route path="/course/:courseId/access-denied"> | ||
| <CourseAccessErrorPage /> | ||
| </Route>, | ||
| ); | ||
| expect(screen.getByTestId('access-denied-main')).toBeInTheDocument(); | ||
| expect(history.location.pathname).toBe(accessDeniedUrl); | ||
| }); | ||
| }); |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we please add
/enterprise/select/active/in constants, so that It can be written in one place and imported everywhere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our project, We are not saving URL paths anywhere, I tried to follow code patterns here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I think we should follow this practice to call it everywhere from one place.