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

feat(ui): lazy load modals #631

Merged
merged 2 commits into from
May 11, 2019
Merged
Show file tree
Hide file tree
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
29 changes: 22 additions & 7 deletions ui/src/Components/MainModal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import { observable, action } from "mobx";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCog } from "@fortawesome/free-solid-svg-icons/faCog";
import { faSpinner } from "@fortawesome/free-solid-svg-icons/faSpinner";

import { AlertStore } from "Stores/AlertStore";
import { Settings } from "Stores/Settings";
import { TooltipWrapper } from "Components/TooltipWrapper";
import { Modal } from "Components/Modal";
import { MainModalContent } from "./MainModalContent";

// https://github.com/facebook/react/issues/14603
const MainModalContent = React.lazy(() =>
import("./MainModalContent").then(module => ({
default: module.MainModalContent
}))
);

const MainModal = observer(
class MainModal extends Component {
Expand Down Expand Up @@ -49,12 +56,20 @@ const MainModal = observer(
</TooltipWrapper>
</li>
<Modal isOpen={this.toggle.show}>
<MainModalContent
alertStore={alertStore}
settingsStore={settingsStore}
onHide={this.toggle.hide}
isVisible={this.toggle.show}
/>
<React.Suspense
fallback={
<h1 className="display-1 text-secondary p-5 m-auto">
<FontAwesomeIcon icon={faSpinner} size="lg" spin />
</h1>
}
>
<MainModalContent
alertStore={alertStore}
settingsStore={settingsStore}
onHide={this.toggle.hide}
isVisible={this.toggle.show}
/>
</React.Suspense>
</Modal>
</React.Fragment>
);
Expand Down
12 changes: 11 additions & 1 deletion ui/src/Components/MainModal/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,22 @@ describe("<MainModal />", () => {
expect(tree.find("MainModalContent")).toHaveLength(0);
});

it("renders the modal when it is shown", () => {
it("renders a spinner placeholder while modal content is loading", () => {
const tree = MountedMainModal();
const toggle = tree.find(".nav-link");
toggle.simulate("click");
expect(tree.find("FontAwesomeIcon")).not.toHaveLength(0);
expect(tree.find("MainModalContent")).toHaveLength(0);
expect(tree.find(".modal-content").find("svg.fa-spinner")).toHaveLength(1);
});

it("renders modal content if fallback is not used", () => {
const tree = MountedMainModal();
const toggle = tree.find(".nav-link");
toggle.simulate("click");
expect(tree.find("FontAwesomeIcon")).not.toHaveLength(0);
expect(tree.find("MainModalContent")).toHaveLength(1);
expect(tree.find(".modal-content").find("svg.fa-spinner")).toHaveLength(0);
});

it("hides the modal when toggle() is called twice", () => {
Expand Down
29 changes: 22 additions & 7 deletions ui/src/Components/SilenceModal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ import { observer } from "mobx-react";

import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faBellSlash } from "@fortawesome/free-solid-svg-icons/faBellSlash";
import { faSpinner } from "@fortawesome/free-solid-svg-icons/faSpinner";

import { AlertStore } from "Stores/AlertStore";
import { SilenceFormStore } from "Stores/SilenceFormStore";
import { Settings } from "Stores/Settings";
import { Modal } from "Components/Modal";
import { TooltipWrapper } from "Components/TooltipWrapper";
import { SilenceModalContent } from "./SilenceModalContent";

import "./index.css";

// https://github.com/facebook/react/issues/14603
const SilenceModalContent = React.lazy(() =>
import("./SilenceModalContent").then(module => ({
default: module.SilenceModalContent
}))
);

const SilenceModal = observer(
class SilenceModal extends Component {
static propTypes = {
Expand Down Expand Up @@ -42,12 +49,20 @@ const SilenceModal = observer(
isOpen={silenceFormStore.toggle.visible}
onExited={silenceFormStore.data.resetProgress}
>
<SilenceModalContent
alertStore={alertStore}
silenceFormStore={silenceFormStore}
settingsStore={settingsStore}
onHide={silenceFormStore.toggle.hide}
/>
<React.Suspense
fallback={
<h1 className="display-1 text-secondary p-5 m-auto">
<FontAwesomeIcon icon={faSpinner} size="lg" spin />
</h1>
}
>
<SilenceModalContent
alertStore={alertStore}
silenceFormStore={silenceFormStore}
settingsStore={settingsStore}
onHide={silenceFormStore.toggle.hide}
/>
</React.Suspense>
</Modal>
</React.Fragment>
);
Expand Down
12 changes: 11 additions & 1 deletion ui/src/Components/SilenceModal/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@ describe("<SilenceModal />", () => {
expect(tree.find("SilenceModalContent")).toHaveLength(0);
});

it("renders the modal when it is shown", () => {
it("renders a spinner placeholder while modal content is loading", () => {
const tree = MountedSilenceModal();
const toggle = tree.find(".nav-link");
toggle.simulate("click");
expect(tree.find("FontAwesomeIcon")).not.toHaveLength(0);
expect(tree.find("SilenceModalContent")).toHaveLength(0);
expect(tree.find(".modal-content").find("svg.fa-spinner")).toHaveLength(1);
});

it("renders modal content if fallback is not used", () => {
const tree = MountedSilenceModal();
const toggle = tree.find(".nav-link");
toggle.simulate("click");
expect(tree.find("FontAwesomeIcon")).not.toHaveLength(0);
expect(tree.find("SilenceModalContent")).toHaveLength(1);
expect(tree.find(".modal-content").find("svg.fa-spinner")).toHaveLength(0);
});

it("hides the modal when toggle() is called twice", () => {
Expand Down
6 changes: 1 addition & 5 deletions ui/src/Components/TooltipWrapper/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from "react";
import PropTypes from "prop-types";

import { Tooltip } from "react-tippy";

Expand All @@ -10,14 +9,11 @@ const TooltipWrapper = ({ children, ...props }) => (
delay={[1000, 100]}
size="small"
touchHold={true}
style={{ display: "inline-block", "maxWidth": "100%" }}
style={{ display: "inline-block", maxWidth: "100%" }}
{...props}
>
{children}
</Tooltip>
);
Tooltip.propTypes = {
children: PropTypes.node.isRequired
};

export { TooltipWrapper };