Skip to content

Commit

Permalink
Added global unsaved status, implemented a popup before unsaved refre…
Browse files Browse the repository at this point in the history
…sh or tab close
  • Loading branch information
justinnas committed Sep 15, 2024
1 parent f492789 commit 45aa000
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const EditorView: React.FC = () => {

const { connected } = useSessionContext();
const { file, fileContent, filePagination, fileStateUpdate } = useWorkspaceContext();
const { blocked, blockedStateUpdate } = useStatusContext();
const { blocked, blockedStateUpdate, unsaved, unsavedStateUpdate } = useStatusContext();
const ref = useGridApiRef();

const handleSave = async () => {
Expand Down Expand Up @@ -125,6 +125,11 @@ export const EditorView: React.FC = () => {
}
};

const onCellEditStart = () => {
unsavedStateUpdate(true);
console.log(unsaved);
};

const getWorkspaceFile = useCallback(async () => {
if (!file.id) {
setFileContentResponse({ totalRows: 0, header: [], rows: [], page: 0 });
Expand Down Expand Up @@ -196,6 +201,19 @@ export const EditorView: React.FC = () => {
);
}, [fileContentResponse, fileContent.aggregations]);

// Browser tab close/refresh warning if there are unsaved changes effect
useEffect(() => {
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
if (unsaved) event.preventDefault();
};

window.addEventListener('beforeunload', handleBeforeUnload);

return () => {
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, [unsaved]);

return (
<DataGrid
sx={{ height: '100%', border: 'none' }}
Expand Down Expand Up @@ -229,6 +247,7 @@ export const EditorView: React.FC = () => {
toolbar: {},
}}
apiRef={ref}
onCellEditStart={onCellEditStart}
/>
);
};
12 changes: 12 additions & 0 deletions app/front-end/src/stores/statusContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import React, { createContext, useEffect, useState } from 'react';
export interface StatusContextProps {
blocked: boolean;
blockedStateUpdate: (blocked: boolean) => void;
unsaved: boolean;
unsavedStateUpdate: (isUnsaved: boolean) => void;
}

export const StatusContext = createContext<StatusContextProps>({
blocked: false,
blockedStateUpdate: () => {},
unsaved: false,
unsavedStateUpdate: () => {},
});

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

const [unsaved, setUnsaved] = useState<boolean>(false);

const unsavedStateUpdate = (isUnsaved: boolean) => {
setUnsaved(isUnsaved);
};

const { connected } = useSessionContext();

useEffect(() => {
Expand All @@ -36,6 +46,8 @@ export const StatusContextProvider: React.FC<Props> = ({ children }) => {
const StatusContextValue: StatusContextProps = {
blocked,
blockedStateUpdate,
unsaved,
unsavedStateUpdate,
};

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

0 comments on commit 45aa000

Please sign in to comment.