From 9f8250c40e8762ee03fa8e13a608b74063b0f744 Mon Sep 17 00:00:00 2001 From: Keshan Nageswaran Date: Tue, 10 Jul 2018 19:57:37 +0530 Subject: [PATCH] Implementing an alert before user tries to exit the tab/browser to avoid losing changes made in the editor --- components/Layout.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/components/Layout.js b/components/Layout.js index 06cbb25..451c5fd 100644 --- a/components/Layout.js +++ b/components/Layout.js @@ -81,14 +81,23 @@ export default class Layout extends PureComponent { this.setKey(this.props.pathname); } + componentWillUnmount() { + window.removeEventListener("beforeunload", this.alertExistHandler); + } + componentDidMount() { Layout.loadCarbonAds(); + window.addEventListener("beforeunload", this.alertExistHandler); } componentWillReceiveProps({ pathname }) { this.setKey(pathname); } + alertExistHandler(event) { + event.preventDefault(); + return (event.returnValue = "Are you sure you want to close?"); // currently newer version of chrome and other browsers deosn't support customized string so returning a string or empty string won't be of much impact + } static loadCarbonAds() { // const script = document.createElement("script"); //