Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JTE/PKFE-37 #62

Merged
merged 6 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
mantvydasdeltuva marked this conversation as resolved.
Show resolved Hide resolved
};

const getWorkspaceFile = useCallback(async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also when moving to the next or previous page of the file, it should also prompt that changes may be lost.
The same principle should also be done with changing page size number.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved

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
Loading