Skip to content

Commit a870aea

Browse files
Merge pull request #96 from Saifullah-dev/95-issue-optional-callback-props-error
fix(Callback Props): Update error message to instruct user to pass missing required callback props
2 parents 107d917 + 8c133ce commit a870aea

File tree

8 files changed

+30
-11
lines changed

8 files changed

+30
-11
lines changed

frontend/src/FileManager/Actions/CreateFolder/CreateFolder.action.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import NameInput from "../../../components/NameInput/NameInput";
55
import ErrorTooltip from "../../../components/ErrorTooltip/ErrorTooltip";
66
import { useFileNavigation } from "../../../contexts/FileNavigationContext";
77
import { useLayout } from "../../../contexts/LayoutContext";
8+
import { validateApiCallback } from "../../../utils/validateApiCallback";
89

910
const maxNameLength = 220;
1011

@@ -84,7 +85,7 @@ const CreateFolderAction = ({ filesViewRef, file, onCreateFolder, triggerAction
8485
newFolderName = duplicateNameHandler("New Folder", true, syncedCurrPathFiles);
8586
}
8687

87-
onCreateFolder(newFolderName, currentFolder);
88+
validateApiCallback(onCreateFolder, "onCreateFolder", newFolderName, currentFolder);
8889
setCurrentPathFiles((prev) => prev.filter((f) => f.key !== file.key));
8990
triggerAction.close();
9091
}

frontend/src/FileManager/Actions/Rename/Rename.action.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import NameInput from "../../../components/NameInput/NameInput";
88
import ErrorTooltip from "../../../components/ErrorTooltip/ErrorTooltip";
99
import { useFileNavigation } from "../../../contexts/FileNavigationContext";
1010
import { useLayout } from "../../../contexts/LayoutContext";
11+
import { validateApiCallback } from "../../../utils/validateApiCallback";
1112

1213
const maxNameLength = 220;
1314

@@ -88,7 +89,7 @@ const RenameAction = ({ filesViewRef, file, onRename, triggerAction }) => {
8889
}
8990
}
9091
setFileRenameError(false);
91-
onRename(file, renameFile);
92+
validateApiCallback(onRename, "onRename", file, renameFile);
9293
setCurrentPathFiles((prev) => prev.filter((f) => f.key !== file.key)); // Todo: Should only filter on success API call
9394
triggerAction.close();
9495
}

frontend/src/FileManager/FileManager.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ const FileManager = ({
2020
fileUploadConfig,
2121
isLoading,
2222
onCreateFolder,
23-
onFileUploading,
24-
onFileUploaded,
23+
onFileUploading = () => {},
24+
onFileUploaded = () => {},
2525
onPaste,
2626
onRename,
2727
onDownload,
2828
onDelete = () => null,
29-
onLayoutChange,
29+
onLayoutChange = () => {},
3030
onRefresh,
31-
onFileOpen,
32-
onError,
31+
onFileOpen = () => {},
32+
onError = () => {},
3333
layout = "grid",
3434
enableFilePreview = true,
3535
maxFileSize,

frontend/src/FileManager/Toolbar/Toolbar.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { useFileNavigation } from "../../contexts/FileNavigationContext";
1414
import { useSelection } from "../../contexts/SelectionContext";
1515
import { useClipBoard } from "../../contexts/ClipboardContext";
1616
import { useLayout } from "../../contexts/LayoutContext";
17+
import { validateApiCallback } from "../../utils/validateApiCallback";
1718
import "./Toolbar.scss";
1819

1920
const Toolbar = ({
@@ -61,7 +62,7 @@ const Toolbar = ({
6162
icon: <FiRefreshCw size={16} />,
6263
title: "Refresh",
6364
onClick: () => {
64-
onRefresh();
65+
validateApiCallback(onRefresh, "onRefresh");
6566
setClipBoard(null);
6667
},
6768
},

frontend/src/contexts/ClipboardContext.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createContext, useContext, useState } from "react";
22
import { useSelection } from "./SelectionContext";
3+
import { validateApiCallback } from "../utils/validateApiCallback";
34

45
const ClipBoardContext = createContext();
56

@@ -21,7 +22,7 @@ export const ClipBoardProvider = ({ children, onPaste }) => {
2122
const copiedFiles = clipBoard.files;
2223
const operationType = clipBoard.isMoving ? "move" : "copy";
2324

24-
onPaste(copiedFiles, destinationFolder, operationType);
25+
validateApiCallback(onPaste, "onPaste", copiedFiles, destinationFolder, operationType);
2526

2627
clipBoard.isMoving && setClipBoard(null);
2728
setSelectedFiles([]);

frontend/src/contexts/SelectionContext.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { createContext, useContext, useState } from "react";
2+
import { validateApiCallback } from "../utils/validateApiCallback";
23

34
const SelectionContext = createContext();
45

56
export const SelectionProvider = ({ children, onDownload }) => {
67
const [selectedFiles, setSelectedFiles] = useState([]);
78

89
const handleDownload = () => {
9-
onDownload(selectedFiles);
10+
validateApiCallback(onDownload, "onDownload", selectedFiles);
1011
};
1112

1213
return (

frontend/src/hooks/useShortcutHandler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useClipBoard } from "../contexts/ClipboardContext";
44
import { useFileNavigation } from "../contexts/FileNavigationContext";
55
import { useSelection } from "../contexts/SelectionContext";
66
import { useLayout } from "../contexts/LayoutContext";
7+
import { validateApiCallback } from "../utils/validateApiCallback";
78

89
export const useShortcutHandler = (triggerAction, onRefresh) => {
910
const { setClipBoard, handleCutCopy, handlePasting } = useClipBoard();
@@ -64,7 +65,7 @@ export const useShortcutHandler = (triggerAction, onRefresh) => {
6465
};
6566

6667
const triggerRefresh = () => {
67-
onRefresh();
68+
validateApiCallback(onRefresh, "onRefresh");
6869
setClipBoard(null);
6970
};
7071

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const validateApiCallback = (callback, callbackName, ...args) => {
2+
try {
3+
if (typeof callback === "function") {
4+
callback(...args);
5+
} else {
6+
throw new Error(
7+
`<FileManager /> Missing prop: Callback function "${callbackName}" is required.`
8+
);
9+
}
10+
} catch (error) {
11+
console.error(error.message);
12+
}
13+
};

0 commit comments

Comments
 (0)