Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactoring: remove React deprecated events (#809)

Guilherme Oenningf95d5bb25ccc
diff --git a/public/components/SignInModal.tsx b/public/components/SignInModal.tsx
index b8370ff68..d479dab56 100644
--- a/public/components/SignInModal.tsx
+++ b/public/components/SignInModal.tsx
@@ -1,69 +1,50 @@
-import React from "react";
+import React, { useState, useEffect } from "react";
import { Modal, SignInControl, LegalFooter } from "@fider/components/common";
interface SignInModalProps {
isOpen: boolean;
+ onClose: () => void;
}
-interface SignInModalState {
- isOpen: boolean;
- email: string;
- emailSent: boolean;
-}
-
-export class SignInModal extends React.Component<SignInModalProps, SignInModalState> {
- constructor(props: SignInModalProps) {
- super(props);
- this.state = {
- isOpen: this.props.isOpen,
- email: "",
- emailSent: false
- };
-
- this.onEmailSent = this.onEmailSent.bind(this);
- }
+export const SignInModal: React.StatelessComponent<SignInModalProps> = props => {
+ const [confirmationAddress, setConfirmationAddress] = useState("");
- public componentWillReceiveProps(nextProps: SignInModalProps) {
- this.setState({
- isOpen: nextProps.isOpen
- });
- }
+ useEffect(() => {
+ if (confirmationAddress) {
+ setTimeout(() => setConfirmationAddress(""), 5000);
+ }
+ }, [confirmationAddress]);
- private onEmailSent(email: string): void {
- this.setState({ email, emailSent: true }, () => {
- setTimeout(() => {
- this.setState({ email: "", emailSent: false });
- }, 5000);
- });
- }
-
- private closeModal = () => {
- this.setState({ isOpen: false, emailSent: false });
+ const onEmailSent = (email: string): void => {
+ setConfirmationAddress(email);
};
- public render() {
- const content = this.state.emailSent ? (
- <>
- <p>
- We have just sent a confirmation link to <b>{this.state.email}</b>. <br /> Click the link and you’ll be signed
- in.
- </p>
- <p>
- <a href="#" onClick={this.closeModal}>
- OK
- </a>
- </p>
- </>
- ) : (
- <SignInControl useEmail={true} onEmailSent={this.onEmailSent} />
- );
+ const closeModal = () => {
+ setConfirmationAddress("");
+ props.onClose();
+ };
- return (
- <Modal.Window isOpen={this.state.isOpen}>
- <Modal.Header>Sign in to raise your voice</Modal.Header>
- <Modal.Content>{content}</Modal.Content>
- <LegalFooter />
- </Modal.Window>
- );
- }
-}
+ const content = confirmationAddress ? (
+ <>
+ <p>
+ We have just sent a confirmation link to <b>{confirmationAddress}</b>. <br /> Click the link and you’ll be
+ signed in.
+ </p>
+ <p>
+ <a href="#" onClick={closeModal}>
+ OK
+ </a>
+ </p>
+ </>
+ ) : (
+ <SignInControl useEmail={true} onEmailSent={onEmailSent} />
+ );
+
+ return (
+ <Modal.Window isOpen={props.isOpen} onClose={closeModal}>
+ <Modal.Header>Sign in to raise your voice</Modal.Header>
+ <Modal.Content>{content}</Modal.Content>
+ <LegalFooter />
+ </Modal.Window>
+ );
+};
diff --git a/public/components/VoteCounter.spec.tsx b/public/components/VoteCounter.spec.tsx
index bad9e509a..5d657dc53 100644
--- a/public/components/VoteCounter.spec.tsx
+++ b/public/components/VoteCounter.spec.tsx
@@ -1,7 +1,7 @@
import React from "react";
import { shallow } from "enzyme";
import { Post, UserRole, PostStatus, UserStatus } from "@fider/models";
-import { VoteCounter } from "@fider/components";
+import { VoteCounter, SignInModal } from "@fider/components";
import { httpMock, fiderMock, rerender } from "@fider/services/testing";
let post: Post;
@@ -68,7 +68,7 @@ describe("<VoteCounter />", () => {
const wrapper = shallow(<VoteCounter post={post} />);
wrapper.find("button").simulate("click");
await rerender(wrapper);
- expect(wrapper.find("SignInModal").length).toBe(1);
+ expect(wrapper.find(SignInModal).length).toBe(1);
expect(mock.post).toHaveBeenCalledTimes(0);
expect(mock.delete).toHaveBeenCalledTimes(0);
});
diff --git a/public/components/VoteCounter.tsx b/public/components/VoteCounter.tsx
index 64d982ed8..9d8500aad 100644
--- a/public/components/VoteCounter.tsx
+++ b/public/components/VoteCounter.tsx
@@ -43,6 +43,10 @@ export class VoteCounter extends React.Component<VoteCounterProps, VoteCounterSt
}
};
+ private hideModal = () => {
+ this.setState({ showSignIn: false });
+ };
+
public render() {
const status = PostStatus.Get(this.props.post.status);
@@ -68,7 +72,7 @@ export class VoteCounter extends React.Component<VoteCounterProps, VoteCounterSt
return (
<>
- <SignInModal isOpen={this.state.showSignIn} />
+ <SignInModal isOpen={this.state.showSignIn} onClose={this.hideModal} />
<div className="c-vote-counter">{status.closed ? disabled : vote}</div>
</>
);
diff --git a/public/components/common/Header.tsx b/public/components/common/Header.tsx
index 5f808b630..9a9f3fcca 100644
--- a/public/components/common/Header.tsx
+++ b/public/components/common/Header.tsx
@@ -35,6 +35,10 @@ export class Header extends React.Component<{}, HeaderState> {
}
};
+ private hideModal = () => {
+ this.setState({ showSignIn: false });
+ };
+
public render() {
const items = Fider.session.isAuthenticated && (
<div className="c-menu-user">
@@ -68,7 +72,7 @@ export class Header extends React.Component<{}, HeaderState> {
return (
<div id="c-header">
<EnvironmentInfo />
- <SignInModal isOpen={this.state.showSignIn} />
+ <SignInModal isOpen={this.state.showSignIn} onClose={this.hideModal} />
<div className="c-menu">
<div className="container">
<a href="/" className="c-menu-item-title">
diff --git a/public/components/common/Modal.tsx b/public/components/common/Modal.tsx
index d77983a8c..3435cb0ab 100644
--- a/public/components/common/Modal.tsx
+++ b/public/components/common/Modal.tsx
@@ -1,6 +1,6 @@
import "./Modal.scss";
-import React from "react";
+import React, { useEffect, useRef } from "react";
import ReactDOM from "react-dom";
import { classSet } from "@fider/services";
@@ -10,11 +10,7 @@ interface ModalWindowProps {
size?: "small" | "large" | "fluid";
canClose?: boolean;
center?: boolean;
- onClose?: () => void;
-}
-
-interface ModalWindowState {
- isOpen: boolean;
+ onClose: () => void;
}
interface ModalFooterProps {
@@ -22,88 +18,62 @@ interface ModalFooterProps {
children?: React.ReactNode;
}
-class ModalWindow extends React.Component<ModalWindowProps, ModalWindowState> {
- private root?: HTMLElement;
-
- constructor(props: ModalWindowProps) {
- super(props);
- this.state = {
- isOpen: this.props.isOpen
- };
- }
-
- public static defaultProps: Partial<ModalWindowProps> = {
- size: "small",
- canClose: true,
- center: true
- };
+const ModalWindow: React.StatelessComponent<ModalWindowProps> = props => {
+ const root = useRef<HTMLElement>(document.getElementById("root-modal"));
- public componentWillUpdate(nextProps: ModalWindowProps, nextState: ModalWindowState) {
- if (nextState.isOpen) {
- document.addEventListener("keydown", this.keyDown, false);
+ useEffect(() => {
+ if (props.isOpen) {
+ document.body.style.overflow = "hidden";
+ document.addEventListener("keydown", keyDown, false);
} else {
- document.removeEventListener("keydown", this.keyDown, false);
+ document.body.style.overflow = "";
+ document.removeEventListener("keydown", keyDown, false);
}
- }
+ }, [props.isOpen]);
- public componentWillReceiveProps(nextProps: ModalWindowProps) {
- this.setState({
- isOpen: nextProps.isOpen
- });
- }
+ const swallow = (evt: React.MouseEvent<HTMLDivElement>) => {
+ evt.stopPropagation();
+ };
- private keyDown = (event: KeyboardEvent) => {
+ const keyDown = (event: KeyboardEvent) => {
if (event.keyCode === 27) {
// ESC
- this.close();
+ close();
}
};
- private close = () => {
- if (this.props.canClose) {
- this.setState({ isOpen: false });
- if (this.props.onClose) {
- this.props.onClose();
- }
+ const close = () => {
+ if (props.canClose) {
+ props.onClose();
}
};
- private swallow = (evt: React.MouseEvent<HTMLDivElement>) => {
- evt.stopPropagation();
- };
-
- private getContainer = (): HTMLElement => {
- if (!this.root) {
- this.root = document.getElementById("root-modal")!;
- }
- return this.root;
- };
-
- public render() {
- if (!this.state.isOpen) {
- document.body.style.overflow = "";
- return null;
- }
-
- document.body.style.overflow = "hidden";
+ if (!props.isOpen) {
+ return null;
+ }
- const className = classSet({
- "c-modal-window": true,
- [`${this.props.className}`]: !!this.props.className,
- "m-center": this.props.center,
- [`m-${this.props.size}`]: true
- });
+ const className = classSet({
+ "c-modal-window": true,
+ [`${props.className}`]: !!props.className,
+ "m-center": props.center,
+ [`m-${props.size}`]: true
+ });
+
+ return ReactDOM.createPortal(
+ <div aria-disabled={true} className="c-modal-dimmer" onClick={close}>
+ <div className={className} onClick={swallow}>
+ {props.children}
+ </div>
+ </div>,
+ root.current!
+ );
+};
- return ReactDOM.createPortal(
- <div aria-disabled={true} className="c-modal-dimmer" onClick={this.close}>
- <div className={className} onClick={this.swallow}>
- {this.props.children}
- </div>
- </div>,
- this.getContainer()
- );
- }
-}
+ModalWindow.defaultProps = {
+ size: "small",
+ canClose: true,
+ center: true
+};
export const Modal = {
Window: ModalWindow,
diff --git a/public/components/common/form/ImageUploader.tsx b/public/components/common/form/ImageUploader.tsx
index dc00ffc2e..63879ff7b 100644
--- a/public/components/common/form/ImageUploader.tsx
+++ b/public/components/common/form/ImageUploader.tsx
@@ -107,7 +107,13 @@ export class ImageUploader extends React.Component<ImageUploaderProps, ImageUplo
private modal() {
return (
- <Modal.Window className="c-image-viewer-modal" isOpen={this.state.showModal} center={false} size="fluid">
+ <Modal.Window
+ className="c-image-viewer-modal"
+ isOpen={this.state.showModal}
+ onClose={this.closeModal}
+ center={false}
+ size="fluid"
+ >
<Modal.Content>
{this.props.bkey ? <img src={uploadedImageURL(this.props.bkey)} /> : <img src={this.state.previewURL} />}
</Modal.Content>
diff --git a/public/components/common/form/ImageViewer.tsx b/public/components/common/form/ImageViewer.tsx
index f2ce16eff..1152ef4b2 100644
--- a/public/components/common/form/ImageViewer.tsx
+++ b/public/components/common/form/ImageViewer.tsx
@@ -45,7 +45,13 @@ export class ImageViewer extends React.Component<ImageViewerProps, ImageViewerSt
private modal() {
return (
- <Modal.Window className="c-image-viewer-modal" isOpen={this.state.showModal} center={false} size="fluid">
+ <Modal.Window
+ className="c-image-viewer-modal"
+ isOpen={this.state.showModal}
+ onClose={this.closeModal}
+ center={false}
+ size="fluid"
+ >
<Modal.Content>
{!this.state.loadedPreview && <Loader />}
<img onLoad={this.onPreviewLoad} src={uploadedImageURL(this.props.bkey, 1500)} />
diff --git a/public/pages/Administration/components/BillingPlanPanel.tsx b/public/pages/Administration/components/BillingPlanPanel.tsx
index 45507c3a2..0fcc1f5df 100644
--- a/public/pages/Administration/components/BillingPlanPanel.tsx
+++ b/public/pages/Administration/components/BillingPlanPanel.tsx
@@ -121,7 +121,7 @@ export class BillingPlanPanel extends React.Component<BillingPlanPanelProps, Bil
return (
<>
- <Modal.Window canClose={true} isOpen={!!this.state.action} center={false} onClose={this.closeModal}>
+ <Modal.Window isOpen={!!this.state.action} center={false} onClose={this.closeModal}>
{this.state.action === "subscribe" && <Modal.Header>Subscribe</Modal.Header>}
{this.state.action === "cancel" && <Modal.Header>Cancel Subscription</Modal.Header>}
<Modal.Content>
diff --git a/public/pages/Administration/components/PaymentInfoModal.tsx b/public/pages/Administration/components/PaymentInfoModal.tsx
index 4d9fff932..29d8f8652 100644
--- a/public/pages/Administration/components/PaymentInfoModal.tsx
+++ b/public/pages/Administration/components/PaymentInfoModal.tsx
@@ -172,7 +172,7 @@ class PaymentInfoModal extends React.Component<PaymentInfoModalProps, PaymentInf
public render() {
return (
- <Modal.Window isOpen={true} center={false} size="large" onClose={this.props.onClose}>
+ <Modal.Window isOpen={true} center={false} size="large" onClose={this.closeModal}>
<Modal.Content>
<Form className="c-payment-info-modal" error={this.state.error}>
<div className="row">
diff --git a/public/pages/CompleteSignInProfile/CompleteSignInProfile.page.tsx b/public/pages/CompleteSignInProfile/CompleteSignInProfile.page.tsx
index 48bc07323..4fb96b9e9 100644
--- a/public/pages/CompleteSignInProfile/CompleteSignInProfile.page.tsx
+++ b/public/pages/CompleteSignInProfile/CompleteSignInProfile.page.tsx
@@ -34,10 +34,14 @@ export default class CompleteSignInProfilePage extends React.Component<HomePageP
this.setState({ name });
};
+ private noop = () => {
+ // do nothing
+ };
+
public render() {
return (
<>
- <Modal.Window canClose={false} isOpen={true}>
+ <Modal.Window canClose={false} isOpen={true} onClose={this.noop}>
<Modal.Header>Complete your profile</Modal.Header>
<Modal.Content>
<p>Because this is your first sign in, please enter your name.</p>
diff --git a/public/pages/Home/components/PostInput.tsx b/public/pages/Home/components/PostInput.tsx
index d6cfdf0b5..9c57bf197 100644
--- a/public/pages/Home/components/PostInput.tsx
+++ b/public/pages/Home/components/PostInput.tsx
@@ -75,6 +75,10 @@ export class PostInput extends React.Component<PostInputProps, PostInputState> {
}
};
+ private hideModal = () => {
+ this.setState({ showSignIn: false });
+ };
+
private setInputRef = (e: HTMLInputElement) => {
this.title = e;
};
@@ -98,7 +102,7 @@ export class PostInput extends React.Component<PostInputProps, PostInputState> {
return (
<>
- <SignInModal isOpen={this.state.showSignIn} />
+ <SignInModal isOpen={this.state.showSignIn} onClose={this.hideModal} />
<Form error={this.state.error}>
<Input
field="title"
diff --git a/public/pages/MySettings/MySettings.page.tsx b/public/pages/MySettings/MySettings.page.tsx
index 409b89754..facd13283 100644
--- a/public/pages/MySettings/MySettings.page.tsx
+++ b/public/pages/MySettings/MySettings.page.tsx
@@ -113,7 +113,7 @@ export default class MySettingsPage extends React.Component<MySettingsPageProps,
return (
<div id="p-my-settings" className="page container">
- <Modal.Window isOpen={this.state.showModal} canClose={true} center={true}>
+ <Modal.Window isOpen={this.state.showModal} onClose={this.closeModal}>
<Modal.Header>Confirm your new email</Modal.Header>
<Modal.Content>
<div>
diff --git a/public/pages/MySettings/components/DangerZone.tsx b/public/pages/MySettings/components/DangerZone.tsx
index 24c3d4f46..bf2c07250 100644
--- a/public/pages/MySettings/components/DangerZone.tsx
+++ b/public/pages/MySettings/components/DangerZone.tsx
@@ -36,7 +36,7 @@ export class DangerZone extends React.Component<{}, DangerZoneState> {
public render() {
return (
<div className="l-danger-zone">
- <Modal.Window canClose={true} isOpen={this.state.clicked} center={false} onClose={this.onCancel}>
+ <Modal.Window isOpen={this.state.clicked} center={false} onClose={this.onCancel}>
<Modal.Header>Delete account</Modal.Header>
<Modal.Content>
<p>
diff --git a/public/pages/ShowPost/components/CommentInput.tsx b/public/pages/ShowPost/components/CommentInput.tsx
index 96185b67b..8b54e6272 100644
--- a/public/pages/ShowPost/components/CommentInput.tsx
+++ b/public/pages/ShowPost/components/CommentInput.tsx
@@ -45,6 +45,10 @@ export class CommentInput extends React.Component<CommentInputProps, CommentInpu
this.setState({ attachments });
};
+ private hideModal = () => {
+ this.setState({ showSignIn: false });
+ };
+
public submit = async () => {
this.setState({
error: undefined
@@ -75,7 +79,7 @@ export class CommentInput extends React.Component<CommentInputProps, CommentInpu
public render() {
return (
<>
- <SignInModal isOpen={this.state.showSignIn} />
+ <SignInModal isOpen={this.state.showSignIn} onClose={this.hideModal} />
<div className={`c-comment-input ${Fider.session.isAuthenticated && "m-authenticated"}`}>
{Fider.session.isAuthenticated && <Avatar user={Fider.session.user} />}
<Form error={this.state.error}>
diff --git a/public/pages/ShowPost/components/ModerationPanel.tsx b/public/pages/ShowPost/components/ModerationPanel.tsx
index a9456e5a5..e988d559e 100644
--- a/public/pages/ShowPost/components/ModerationPanel.tsx
+++ b/public/pages/ShowPost/components/ModerationPanel.tsx
@@ -25,14 +25,14 @@ export class ModerationPanel extends React.Component<ModerationPanelProps, Moder
private delete = async () => {
const response = await actions.deletePost(this.props.post.number, this.state.text);
if (response.ok) {
- await this.closeModal();
+ await this.hideModal();
navigator.goHome();
} else if (response.error) {
this.setState({ error: this.state.error });
}
};
- private closeModal = async () => {
+ private hideModal = async () => {
this.setState({ showConfirmation: false });
};
@@ -51,7 +51,7 @@ export class ModerationPanel extends React.Component<ModerationPanelProps, Moder
}
const modal = (
- <Modal.Window isOpen={this.state.showConfirmation} center={false} size="large">
+ <Modal.Window isOpen={this.state.showConfirmation} onClose={this.hideModal} center={false} size="large">
<Modal.Content>
<Form error={this.state.error}>
<TextArea
@@ -71,7 +71,7 @@ export class ModerationPanel extends React.Component<ModerationPanelProps, Moder
<Button color="danger" onClick={this.delete}>
Delete
</Button>
- <Button color="cancel" onClick={this.closeModal}>
+ <Button color="cancel" onClick={this.hideModal}>
Cancel
</Button>
</Modal.Footer>
diff --git a/public/pages/ShowPost/components/ResponseForm.tsx b/public/pages/ShowPost/components/ResponseForm.tsx
index e71a9f88d..074c3e27a 100644
--- a/public/pages/ShowPost/components/ResponseForm.tsx
+++ b/public/pages/ShowPost/components/ResponseForm.tsx
@@ -77,7 +77,7 @@ export class ResponseForm extends React.Component<ResponseFormProps, ResponseFor
}));
const modal = (
- <Modal.Window isOpen={this.state.showModal} center={false} size="large">
+ <Modal.Window isOpen={this.state.showModal} onClose={this.closeModal} center={false} size="large">
<Modal.Content>
<Form error={this.state.error} className="c-response-form">
<Select
diff --git a/public/pages/ShowPost/components/ShowComment.tsx b/public/pages/ShowPost/components/ShowComment.tsx
index be31b68f3..b4f6c116a 100644
--- a/public/pages/ShowPost/components/ShowComment.tsx
+++ b/public/pages/ShowPost/components/ShowComment.tsx
@@ -105,7 +105,7 @@ export class ShowComment extends React.Component<ShowCommentProps, ShowCommentSt
private modal() {
return (
- <Modal.Window isOpen={this.state.showDeleteConfirmation} center={false} size="small">
+ <Modal.Window isOpen={this.state.showDeleteConfirmation} onClose={this.closeModal} center={false} size="small">
<Modal.Header>Delete Comment</Modal.Header>
<Modal.Content>
<p>
diff --git a/public/pages/ShowPost/components/VotesModal.tsx b/public/pages/ShowPost/components/VotesModal.tsx
index 0d5278863..eaeb0a327 100644
--- a/public/pages/ShowPost/components/VotesModal.tsx
+++ b/public/pages/ShowPost/components/VotesModal.tsx
@@ -63,7 +63,7 @@ export class VotesModal extends React.Component<VotesModalProps, VotesModalState
public render() {
return (
- <Modal.Window className="c-votes-modal" isOpen={this.props.isOpen} center={false} onClose={this.props.onClose}>
+ <Modal.Window className="c-votes-modal" isOpen={this.props.isOpen} center={false} onClose={this.closeModal}>
<Modal.Content>
{this.state.isLoading && <Loader />}
{!this.state.isLoading && (
diff --git a/public/pages/SignUp/SignUp.page.tsx b/public/pages/SignUp/SignUp.page.tsx
index 0495cdedc..34e1d3ed9 100644
--- a/public/pages/SignUp/SignUp.page.tsx
+++ b/public/pages/SignUp/SignUp.page.tsx
@@ -123,9 +123,13 @@ export default class SignUpPage extends React.Component<{}, SignUpPageState> {
this.setState({ tenantName });
};
+ private noop = () => {
+ // do nothing
+ };
+
public render() {
const modal = (
- <Modal.Window canClose={false} isOpen={this.state.submitted}>
+ <Modal.Window canClose={false} isOpen={this.state.submitted} onClose={this.noop}>
<Modal.Header>Thank you for registering!</Modal.Header>
<Modal.Content>
<p>