Skip to content
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

fix: prompt authentication error message when xblock is not publicly accessible #1492

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 58 additions & 10 deletions src/courseware/course/sequence/Unit/ContentIFrame.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import PropTypes from 'prop-types';
import React from 'react';
import React, { useEffect, useState } from 'react';

import { ErrorPage } from '@edx/frontend-platform/react';
import { StrictDict } from '@edx/react-unit-test-utils';
import { ModalDialog, Modal } from '@openedx/paragon';
import {
ModalDialog, Modal, Icon, PageBanner,
} from '@openedx/paragon';

import PageLoading from '@src/generic/PageLoading';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { WarningFilled } from '@openedx/paragon/icons';
import * as hooks from './hooks';

/**
Expand All @@ -27,6 +31,49 @@ export const testIDs = StrictDict({
modalIFrame: 'modal-iframe-test-id',
});

const IFrameComponent = ({
url, shouldShowContent, hasLoaded, loadingMessage, showError, title, contentIFrameProps,
}) => {
const [httpStatusCode, setHttpStatusCode] = useState(500);
useEffect(() => {
getAuthenticatedHttpClient().get(url).then(res => {
setHttpStatusCode(res.status);
}).catch((error) => {
setHttpStatusCode(error.response.status);
});
}, []);
return (
<>
{(shouldShowContent && !hasLoaded) && (
// eslint-disable-next-line no-nested-ternary
showError ? (httpStatusCode >= 400 && httpStatusCode < 500
? (
<PageBanner
variant="warning"
>
<Icon src={WarningFilled} className="mie-2" /> Please signin to view this content
</PageBanner>
) : <ErrorPage />) : <PageLoading srMessage={loadingMessage} />
)}
{shouldShowContent && (
<div className="unit-iframe-wrapper">
<iframe title={title} {...contentIFrameProps} data-testid={testIDs.contentIFrame} />
</div>
)}
</>
);
};
IFrameComponent.propTypes = {
url: PropTypes.string.isRequired,
shouldShowContent: PropTypes.bool.isRequired,
hasLoaded: PropTypes.bool.isRequired,
loadingMessage: PropTypes.bool.isRequired,
showError: PropTypes.bool.isRequired,
title: PropTypes.string.isRequired,
// eslint-disable-next-line react/forbid-prop-types
contentIFrameProps: PropTypes.any.isRequired,
};

const ContentIFrame = ({
iframeUrl,
shouldShowContent,
Expand Down Expand Up @@ -81,14 +128,15 @@ const ContentIFrame = ({

return (
<>
{(shouldShowContent && !hasLoaded) && (
showError ? <ErrorPage /> : <PageLoading srMessage={loadingMessage} />
)}
{shouldShowContent && (
<div className="unit-iframe-wrapper">
<iframe title={title} {...contentIFrameProps} data-testid={testIDs.contentIFrame} />
</div>
)}
<IFrameComponent
contentIFrameProps={contentIFrameProps}
showError={showError}
title={title}
hasLoaded={hasLoaded}
url={iframeUrl}
loadingMessage={loadingMessage}
shouldShowContent={shouldShowContent}
/>
{modalOptions.isOpen && (modalOptions.isFullscreen
? (
<ModalDialog
Expand Down