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

Open Editors in a Modal (library components only) [FC-0062] #1357

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions src/editors/EditorContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const context = React.createContext<EditorContext | undefined>(undefined);
export function useEditorContext() {
const ctx = React.useContext(context);
if (ctx === undefined) {
/* istanbul ignore next */
throw new Error('This component needs to be wrapped in <EditorContextProvider>');
}
return ctx;
Expand Down
1 change: 1 addition & 0 deletions src/editors/EditorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Props extends EditorComponent {
lmsEndpointUrl?: string;
studioEndpointUrl?: string;
fullScreen?: boolean;
children?: never;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/editors/containers/EditorContainer/hooks.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ describe('EditorContainer hooks', () => {
destination: reactRedux.useSelector(selectors.app.returnUrl),
analyticsEvent: analyticsEvt.editorCancelClick,
analytics: reactRedux.useSelector(selectors.app.analytics),
returnFunction: null,
}),
);
});
Expand Down
8 changes: 7 additions & 1 deletion src/editors/containers/EditorContainer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ export const cancelConfirmModalToggle = () => {
};
};

export const handleCancel = ({ onClose, returnFunction }) => {
export const handleCancel = ({
onClose = null,
returnFunction = null,
}: {
onClose?: (() => void) | null;
returnFunction?: (() => (result: any) => void) | null;
}): ((result?: any) => void) => {
if (onClose) {
return onClose;
}
Expand Down
6 changes: 6 additions & 0 deletions src/editors/containers/EditorContainer/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ describe('EditorContainer', () => {
fireEvent.click(closeButton);
// Now we should see the confirmation message:
expect(await screen.findByText(confirmMessage)).toBeInTheDocument();

expect(defaultPropsHtml.onClose).not.toHaveBeenCalled();
// And can confirm the cancelation:
const confirmButton = await screen.findByRole('button', { name: 'OK' });
fireEvent.click(confirmButton);
expect(defaultPropsHtml.onClose).toHaveBeenCalled();
});

test('it disables the save button until the fields have been loaded', async () => {
Expand Down
23 changes: 14 additions & 9 deletions src/editors/containers/EditorContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import './index.scss';

interface WrapperProps {
fullScreen: boolean;
children: React.ReactNode;
bradenmacdonald marked this conversation as resolved.
Show resolved Hide resolved
}

const OuterWrapper: React.FC<WrapperProps> = ({ fullScreen, children }) => {
export const EditorModalWrapper: React.FC<WrapperProps> = ({ children }) => {
const { fullScreen } = useEditorContext();
if (fullScreen) {
return (
<div
Expand All @@ -42,7 +42,13 @@
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line just above here throws a warning because some "required" props (onClose and title) from Paragon are missing.

    <ModalDialog isOpen size="xl" isOverflowVisible={false} onClose={() => {}} title="">{children}</ModalDialog>

Or maybe it is better just to fix it in Paragon instead.

};

const FooterWrapper: React.FC<WrapperProps> = ({ fullScreen, children }) => {
export const EditorModalBody: React.FC<WrapperProps> = ({ children }) => {
const { fullScreen } = useEditorContext();
return <ModalDialog.Body className={fullScreen ? 'pb-6' : 'pb-0'}>{ children }</ModalDialog.Body>;
};

export const FooterWrapper: React.FC<WrapperProps> = ({ children }) => {
const { fullScreen } = useEditorContext();
if (fullScreen) {
return <div className="editor-footer fixed-bottom">{children}</div>;
}
Expand All @@ -65,7 +71,6 @@
}) => {
const intl = useIntl();
const dispatch = useDispatch();
const { fullScreen } = useEditorContext();
const isInitialized = hooks.isInitialized();
const { isCancelConfirmOpen, openCancelConfirmModal, closeCancelConfirmModal } = hooks.cancelConfirmModalToggle();
const handleCancel = hooks.handleCancel({ onClose, returnFunction });
Expand All @@ -79,9 +84,9 @@
returnFunction,
});
return (
<OuterWrapper fullScreen={fullScreen}>
<EditorModalWrapper>
{saveFailed && (
<Toast show onClose={clearSaveFailed}>

Check warning on line 89 in src/editors/containers/EditorContainer/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/editors/containers/EditorContainer/index.tsx#L89

Added line #L89 was not covered by tests
<FormattedMessage {...messages.contentSaveFailed} />
</Toast>
)}
Expand All @@ -93,7 +98,7 @@
onClick={() => {
handleCancel();
if (returnFunction) {
closeCancelConfirmModal();

Check warning on line 101 in src/editors/containers/EditorContainer/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/editors/containers/EditorContainer/index.tsx#L101

Added line #L101 was not covered by tests
}
}}
>
Expand All @@ -119,10 +124,10 @@
/>
</div>
</ModalDialog.Header>
<ModalDialog.Body className={`pb-0 ${fullScreen ? 'mb-6' : ''}`}>
<EditorModalBody>
{isInitialized && children}
</ModalDialog.Body>
<FooterWrapper fullScreen={fullScreen}>
</EditorModalBody>
<FooterWrapper>
<ModalDialog.Footer className="shadow-sm">
<ActionRow>
<Button
Expand All @@ -144,7 +149,7 @@
</ActionRow>
</ModalDialog.Footer>
</FooterWrapper>
</OuterWrapper>
</EditorModalWrapper>
);
};

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`SelectTypeWrapper snapshot 1`] = `
<EditorModalWrapper>
<ModalDialog.Header
className="shadow-sm zindex-10"
>
<ModalDialog.Title>
<FormattedMessage
defaultMessage="Select problem type"
description="Title for select problem type modal"
id="authoring.problemEditor.selectType.title"
/>
<div
className="pgn__modal-close-container"
>
<IconButton
alt="Exit the editor"
iconAs="Icon"
src={[MockFunction icons.Close]}
/>
</div>
</ModalDialog.Title>
</ModalDialog.Header>
<EditorModalBody>
<h1>
test child
</h1>
</EditorModalBody>
<FooterWrapper>
<ModalDialog.Footer
className="border-top-0"
>
<ActionRow>
<ActionRow.Spacer />
<Button
aria-label="Cancel"
variant="tertiary"
>
<FormattedMessage
defaultMessage="Cancel"
description="Label for cancel button."
id="authoring.problemeditor.selecttype.cancelButton.label"
/>
</Button>
<Button
aria-label="Select"
disabled={false}
onClick={[Function]}
>
<FormattedMessage
defaultMessage="Select"
description="Label for select button."
id="authoring.problemeditor.selecttype.selectButton.label"
/>
</Button>
</ActionRow>
</ModalDialog.Footer>
</FooterWrapper>
</EditorModalWrapper>
`;
Loading
Loading