Skip to content

Commit 45aa000

Browse files
committed
Added global unsaved status, implemented a popup before unsaved refresh or tab close
1 parent f492789 commit 45aa000

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

app/front-end/src/features/editor/components/editorView/editorView.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const EditorView: React.FC = () => {
5757

5858
const { connected } = useSessionContext();
5959
const { file, fileContent, filePagination, fileStateUpdate } = useWorkspaceContext();
60-
const { blocked, blockedStateUpdate } = useStatusContext();
60+
const { blocked, blockedStateUpdate, unsaved, unsavedStateUpdate } = useStatusContext();
6161
const ref = useGridApiRef();
6262

6363
const handleSave = async () => {
@@ -125,6 +125,11 @@ export const EditorView: React.FC = () => {
125125
}
126126
};
127127

128+
const onCellEditStart = () => {
129+
unsavedStateUpdate(true);
130+
console.log(unsaved);
131+
};
132+
128133
const getWorkspaceFile = useCallback(async () => {
129134
if (!file.id) {
130135
setFileContentResponse({ totalRows: 0, header: [], rows: [], page: 0 });
@@ -196,6 +201,19 @@ export const EditorView: React.FC = () => {
196201
);
197202
}, [fileContentResponse, fileContent.aggregations]);
198203

204+
// Browser tab close/refresh warning if there are unsaved changes effect
205+
useEffect(() => {
206+
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
207+
if (unsaved) event.preventDefault();
208+
};
209+
210+
window.addEventListener('beforeunload', handleBeforeUnload);
211+
212+
return () => {
213+
window.removeEventListener('beforeunload', handleBeforeUnload);
214+
};
215+
}, [unsaved]);
216+
199217
return (
200218
<DataGrid
201219
sx={{ height: '100%', border: 'none' }}
@@ -229,6 +247,7 @@ export const EditorView: React.FC = () => {
229247
toolbar: {},
230248
}}
231249
apiRef={ref}
250+
onCellEditStart={onCellEditStart}
232251
/>
233252
);
234253
};

app/front-end/src/stores/statusContextProvider.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import React, { createContext, useEffect, useState } from 'react';
44
export interface StatusContextProps {
55
blocked: boolean;
66
blockedStateUpdate: (blocked: boolean) => void;
7+
unsaved: boolean;
8+
unsavedStateUpdate: (isUnsaved: boolean) => void;
79
}
810

911
export const StatusContext = createContext<StatusContextProps>({
1012
blocked: false,
1113
blockedStateUpdate: () => {},
14+
unsaved: false,
15+
unsavedStateUpdate: () => {},
1216
});
1317

1418
interface Props {
@@ -22,6 +26,12 @@ export const StatusContextProvider: React.FC<Props> = ({ children }) => {
2226
setBlocked(blocked);
2327
};
2428

29+
const [unsaved, setUnsaved] = useState<boolean>(false);
30+
31+
const unsavedStateUpdate = (isUnsaved: boolean) => {
32+
setUnsaved(isUnsaved);
33+
};
34+
2535
const { connected } = useSessionContext();
2636

2737
useEffect(() => {
@@ -36,6 +46,8 @@ export const StatusContextProvider: React.FC<Props> = ({ children }) => {
3646
const StatusContextValue: StatusContextProps = {
3747
blocked,
3848
blockedStateUpdate,
49+
unsaved,
50+
unsavedStateUpdate,
3951
};
4052

4153
return <StatusContext.Provider value={StatusContextValue}>{children}</StatusContext.Provider>;

0 commit comments

Comments
 (0)