mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-12 15:22:45 +00:00
Merge pull request #4527 from MiniX16/master
Add unsaved-change warning to Home page editor
This commit is contained in:
@@ -53,8 +53,9 @@ const HomePage =(props)=>{
|
|||||||
const [isSaving , setIsSaving] = useState(false);
|
const [isSaving , setIsSaving] = useState(false);
|
||||||
const [autoSaveEnabled , setAutoSaveEnable] = useState(false);
|
const [autoSaveEnabled , setAutoSaveEnable] = useState(false);
|
||||||
|
|
||||||
const editorRef = useRef(null);
|
const editorRef = useRef(null);
|
||||||
const lastSavedBrew = useRef(_.cloneDeep(props.brew));
|
const lastSavedBrew = useRef(_.cloneDeep(props.brew));
|
||||||
|
const unsavedChangesRef = useRef(unsavedChanges);
|
||||||
|
|
||||||
useEffect(()=>{
|
useEffect(()=>{
|
||||||
fetchThemeBundle(setError, setThemeBundle, currentBrew.renderer, currentBrew.theme);
|
fetchThemeBundle(setError, setThemeBundle, currentBrew.renderer, currentBrew.theme);
|
||||||
@@ -70,12 +71,20 @@ const HomePage =(props)=>{
|
|||||||
};
|
};
|
||||||
|
|
||||||
document.addEventListener('keydown', handleControlKeys);
|
document.addEventListener('keydown', handleControlKeys);
|
||||||
|
window.onbeforeunload = ()=>{
|
||||||
|
if(unsavedChangesRef.current)
|
||||||
|
return 'You have unsaved changes!';
|
||||||
|
};
|
||||||
return ()=>{
|
return ()=>{
|
||||||
document.removeEventListener('keydown', handleControlKeys);
|
document.removeEventListener('keydown', handleControlKeys);
|
||||||
|
window.onbeforeunload = null;
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(()=>{
|
||||||
|
unsavedChangesRef.current = unsavedChanges;
|
||||||
|
}, [unsavedChanges]);
|
||||||
|
|
||||||
const save = ()=>{
|
const save = ()=>{
|
||||||
request.post('/api')
|
request.post('/api')
|
||||||
.send(currentBrew)
|
.send(currentBrew)
|
||||||
|
|||||||
Reference in New Issue
Block a user