From 90bf05b67dedb9b295abf555e555c270bc1e8478 Mon Sep 17 00:00:00 2001 From: abias Date: Mon, 12 Jun 2023 22:00:36 -0400 Subject: [PATCH] Completely fix prompt component --- .../components/common/markdown-textarea.tsx | 1 - .../components/common/navigation-prompt.tsx | 64 +++++++++++-------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/shared/components/common/markdown-textarea.tsx b/src/shared/components/common/markdown-textarea.tsx index e88f4706..b9597fed 100644 --- a/src/shared/components/common/markdown-textarea.tsx +++ b/src/shared/components/common/markdown-textarea.tsx @@ -137,7 +137,6 @@ export class MarkdownTextArea extends Component<
diff --git a/src/shared/components/common/navigation-prompt.tsx b/src/shared/components/common/navigation-prompt.tsx index e77466d4..f76e6957 100644 --- a/src/shared/components/common/navigation-prompt.tsx +++ b/src/shared/components/common/navigation-prompt.tsx @@ -1,45 +1,53 @@ import { Component } from "inferno"; -import { i18n } from "../../i18next"; -import { HistoryService } from "../../services/HistoryService"; +import { i18n } from "../../../shared/i18next"; -interface NavigationPromptProps { +export interface IPromptProps { when: boolean; - suppress?: boolean; } -interface NavigationPromptState { - promptState: "hidden" | "show" | "approved"; -} +export default class NavigationPrompt extends Component { + public unblock; -export default class NavigationPrompt extends Component< - NavigationPromptProps, - NavigationPromptState -> { - state: NavigationPromptState = { - promptState: "hidden", - }; + public enable() { + if (this.unblock) { + this.unblock(); + } - constructor(props: NavigationPromptProps, context: any) { - super(props, context); + this.unblock = this.context.router.history.block(tx => { + if (window.confirm(i18n.t("block_leaving"))) { + this.unblock(); + tx.retry(); + } + }); } - componentDidMount(): void { - if (!this.props.suppress) { - const unblock = HistoryService.history.block(tx => { - if (!this.props.when || window.confirm(i18n.t("block_leaving"))) { - unblock(); - - tx.retry(); - } - }); + public disable() { + if (this.unblock) { + this.unblock(); + this.unblock = null; + } + } + public componentWillMount() { + if (this.props.when) { + this.enable(); } } - componentWillUnmount(): void { - console.log("unmounted"); + public componentWillReceiveProps(nextProps: IPromptProps) { + if (nextProps.when) { + if (!this.props.when) { + this.enable(); + } + } else { + this.disable(); + } } - render() { + public componentWillUnmount() { + this.disable(); + } + + public render() { return null; } }