]> Untitled Git - lemmy-ui.git/blob - src/shared/components/common/navigation-prompt.tsx
Fix I18 next circular reference
[lemmy-ui.git] / src / shared / components / common / navigation-prompt.tsx
1 import { Component } from "inferno";
2 import { I18NextService } from "../../services";
3
4 export interface IPromptProps {
5   when: boolean;
6 }
7
8 export default class NavigationPrompt extends Component<IPromptProps, any> {
9   public unblock;
10
11   public enable() {
12     if (this.unblock) {
13       this.unblock();
14     }
15
16     this.unblock = this.context.router.history.block(tx => {
17       if (window.confirm(I18NextService.i18n.t("block_leaving") ?? undefined)) {
18         this.unblock();
19         tx.retry();
20       }
21     });
22   }
23
24   public disable() {
25     if (this.unblock) {
26       this.unblock();
27       this.unblock = null;
28     }
29   }
30   public componentWillMount() {
31     if (this.props.when) {
32       this.enable();
33     }
34   }
35
36   public componentWillReceiveProps(nextProps: IPromptProps) {
37     if (nextProps.when) {
38       if (!this.props.when) {
39         this.enable();
40       }
41     } else {
42       this.disable();
43     }
44   }
45
46   public componentWillUnmount() {
47     this.disable();
48   }
49
50   public render() {
51     return null;
52   }
53 }