2023-06-13 10:33:27 +00:00
|
|
|
import { Component } from "inferno";
|
2023-06-22 00:54:35 +00:00
|
|
|
import { I18NextService } from "../../services";
|
2023-06-13 10:33:27 +00:00
|
|
|
|
|
|
|
export interface IPromptProps {
|
|
|
|
when: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class NavigationPrompt extends Component<IPromptProps, any> {
|
|
|
|
public unblock;
|
|
|
|
|
|
|
|
public enable() {
|
|
|
|
if (this.unblock) {
|
|
|
|
this.unblock();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.unblock = this.context.router.history.block(tx => {
|
2023-06-22 00:54:35 +00:00
|
|
|
if (window.confirm(I18NextService.i18n.t("block_leaving") ?? undefined)) {
|
2023-06-13 10:33:27 +00:00
|
|
|
this.unblock();
|
|
|
|
tx.retry();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public disable() {
|
|
|
|
if (this.unblock) {
|
|
|
|
this.unblock();
|
|
|
|
this.unblock = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public componentWillMount() {
|
|
|
|
if (this.props.when) {
|
|
|
|
this.enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillReceiveProps(nextProps: IPromptProps) {
|
|
|
|
if (nextProps.when) {
|
|
|
|
if (!this.props.when) {
|
|
|
|
this.enable();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
this.disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|