From 63797ecf83e16a4b9e5123b306359f0a4a4017d7 Mon Sep 17 00:00:00 2001 From: Wahyu Ramadhan Date: Wed, 9 Aug 2023 15:05:34 +0700 Subject: [PATCH] Prevent moving page --- src/views/SimproV2/Gantt/GanttFrame.js | 28 +++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/views/SimproV2/Gantt/GanttFrame.js b/src/views/SimproV2/Gantt/GanttFrame.js index 0ddb2b9..f175154 100644 --- a/src/views/SimproV2/Gantt/GanttFrame.js +++ b/src/views/SimproV2/Gantt/GanttFrame.js @@ -1,10 +1,36 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; +import { useHistory } from 'react-router-dom'; import { BASE_SIMPRO_LUMEN } from '../../../const/ApiConst'; const GanttFrame = React.memo((props) => { + const history = useHistory(); const { versionGanttId, idProject, token, ro, timestamp } = props; const iframeSrc = `https://adw-gantt.ospro.id/edit-mode/index.html?base_url=${BASE_SIMPRO_LUMEN}&gantt_id=${versionGanttId}&proyek_id=${idProject}&token=${token}&ro=${ro}×tamp=${timestamp}`; + + + const [batchEntityData, setBatchEntityData] = useState(null); + + useEffect(() => { + const handleMessage = (event) => { + if (event.data && event.data.batchEntity) { + setBatchEntityData(event.data.batchEntity); + } + }; + + window.addEventListener('message', handleMessage); + }, []); + + useEffect(()=>{ + const handleNavigation = (event) => { + if (batchEntityData && batchEntityData !== '') { + const message = 'You have unsaved changes. Are you sure you want to leave this page?'; + return message; + } + }; + history.block(handleNavigation); + }, [batchEntityData]) + return (