Skip to content

Commit

Permalink
fix confirmAlert & confirmFileChanges
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmatthewnguyen105 committed Dec 13, 2023
1 parent cddd220 commit 6859afe
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
17 changes: 8 additions & 9 deletions src/_node/file/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RootNodeUid } from "@_constants/main";
import { FileChangeAlertMessage, RootNodeUid } from "@_constants/main";

import {
TFileHandlerInfoObj,
Expand Down Expand Up @@ -58,17 +58,16 @@ export const isUnsavedProject = (fileTree: TFileNodeTreeData) => {
return false;
};

export const triggerAlert = (msg: string) => {
export const confirmAlert = (msg: string): boolean => {
if (!window.confirm(msg)) {
return;
return false;
}
return true;
};
export const triggerFileChangeAlert = () => {
const message = `Your changes will be lost if you don't save them. Are you sure you want to continue without saving?`;
triggerAlert(message);
};
export const confirmFileChanges = (fileTree: TFileNodeTreeData) => {
isUnsavedProject(fileTree) && triggerFileChangeAlert();
export const confirmFileChanges = (fileTree: TFileNodeTreeData): boolean => {
return isUnsavedProject(fileTree)
? confirmAlert(FileChangeAlertMessage)
: true;
};

export const getFileNameAndExtensionFromFullname = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { useCallback, useContext } from "react";
import { TreeItem } from "react-complex-tree";
import { useDispatch } from "react-redux";

import { RednerableFileTypes, RootNodeUid, TmpNodeUid } from "@_constants/main";
import {
FileChangeAlertMessage,
RednerableFileTypes,
RootNodeUid,
TmpNodeUid,
} from "@_constants/main";
import {
_createIDBDirectory,
TFileNodeData,
triggerAlert,
triggerFileChangeAlert,
_writeIDBFile,
confirmAlert,
} from "@_node/file";
import { getValidNodeUids } from "@_node/helpers";
import { TNode, TNodeTreeData, TNodeUid } from "@_node/types";
Expand Down Expand Up @@ -264,7 +268,7 @@ export const useNodeActionsHandler = ({
const fileData = file.data;
if (file && fileData.changed) {
const message = `Your changes will be lost if you don't save them. Are you sure you want to continue without saving?`;
triggerAlert(message);
if (!confirmAlert(message)) return;
}

addTemporaryNodes(file.uid);
Expand Down Expand Up @@ -360,9 +364,7 @@ export const useNodeActionsHandler = ({
return _file && _fileData.changed;
});

if (hasChangedFile) {
triggerFileChangeAlert();
}
if (hasChangedFile && !confirmAlert(FileChangeAlertMessage)) return;

addRunningActions(["fileTreeView-move"]);

Expand Down
1 change: 1 addition & 0 deletions src/constants/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ export const ShortDelay = 50;

export const NodePathSplitter: string = "?";

export const FileChangeAlertMessage = `Your changes will be lost if you don't save them. Are you sure you want to continue without saving?`;
// ----------------------
export const TmpNodeUid = "tmp:node:uid";
8 changes: 6 additions & 2 deletions src/pages/main/MainPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,12 @@ export default function MainPage() {
recentProjectContexts[index];
const projectHandler =
recentProjectHandlers[index];
confirmFileChanges(fileTree);
importProject(projectContext, projectHandler);

confirmFileChanges(fileTree) &&
importProject(
projectContext,
projectHandler,
);
} else if (
(currentCmdkPage === "Add" &&
command.Group === "Recent") ||
Expand Down
4 changes: 2 additions & 2 deletions src/pages/main/hooks/useCmdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const useCmdk = ({ cmdkReferenceData, importProject }: IUseCmdk) => {
dispatch(setCmdkOpen(true));
}, [cmdkOpen]);
const onNew = useCallback(async () => {
confirmFileChanges(fileTree);
if (!confirmFileChanges(fileTree)) return;

dispatch(setDoingFileAction(true));
try {
Expand All @@ -96,7 +96,7 @@ export const useCmdk = ({ cmdkReferenceData, importProject }: IUseCmdk) => {
dispatch(setDoingFileAction(false));
}, [fileTree, importProject]);
const onOpen = useCallback(async () => {
confirmFileChanges(fileTree);
if (!confirmFileChanges(fileTree)) return;

dispatch(setDoingFileAction(true));
try {
Expand Down

0 comments on commit 6859afe

Please sign in to comment.