Skip to content

Commit

Permalink
refactor: remake handleFile util
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan-Zayne committed Nov 8, 2024
1 parent d4d1276 commit 9242b24
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 109 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-cheetahs-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zayne-labs/toolkit": patch
---

refactor: remake handleFile util
4 changes: 0 additions & 4 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"peerDependencies": {
"@types/react": "^18.0.0",
"react": "^18.0.0",
"sonner": "^1.5.0",
"zustand": "^5.0.0-rc.2"
},
"peerDependenciesMeta": {
Expand All @@ -59,9 +58,6 @@
"react": {
"optional": true
},
"sonner": {
"optional": true
},
"zustand": {
"optional": true
}
Expand Down
78 changes: 78 additions & 0 deletions packages/toolkit/src/core/handleFileValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
type ValidationRules = {
allowedFileTypes?: string[];
disallowDuplicates?: boolean;
fileLimit?: number;
maxFileSize?: number;
};

type ErrorContext = {
message: string;
};

type FileValidationOptions = {
existingFileArray?: File[];
newFileList: FileList;
onError?: (errorContext: ErrorContext) => void;
validationRules?: ValidationRules;
};

const toMegaByte = (size: number) => size * 1024 * 1024;

const handleFileValidation = (options: FileValidationOptions) => {
const { existingFileArray = [], newFileList, onError, validationRules } = options;

const { allowedFileTypes, disallowDuplicates, fileLimit, maxFileSize } = validationRules ?? {};

const validFilesArray: File[] = [];

const isFileUnique = (file: File) => {
return existingFileArray.every((fileItem) => fileItem.name !== file.name);
};

const isFileLimitReached = (limit: number) => {
return existingFileArray.length === limit || validFilesArray.length === limit;
};

// == Loop through fileList and validate each file
for (const file of newFileList) {
if (fileLimit && isFileLimitReached(fileLimit)) {
const message = `Maximum file limit of ${fileLimit} files has been reached`;

onError?.({ message });

break;
}

if (allowedFileTypes && !allowedFileTypes.includes(file.type)) {
const acceptedFilesString = allowedFileTypes.join(" | ");

const message = `File type must be of: ${acceptedFilesString}`;

onError?.({ message });

continue;
}

if (maxFileSize && file.size > toMegaByte(maxFileSize)) {
const message = `Cannot upload a file larger than ${maxFileSize}mb`;

onError?.({ message });

continue;
}

if (disallowDuplicates && !isFileUnique(file)) {
const message = `File: "${file.name}" has already been selected`;

onError?.({ message });

continue;
}

validFilesArray.push(file);
}

return validFilesArray;
};

export { handleFileValidation };
1 change: 1 addition & 0 deletions packages/toolkit/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export { PromiseWithResolvers } from "./promiseWithResolvers";
export { setAnimationInterval } from "./setAnimationInterval";
export { syncStateWithStorage } from "./syncStateWithStorage";
export { toArray } from "./toArray";
export { handleFileValidation } from "./handleFileValidation";
export * from "./throttle";
export * from "./wait";
72 changes: 0 additions & 72 deletions packages/toolkit/src/react/utils/handleFileValidation.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/toolkit/src/react/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from "./getSlotElement";
export { handleFileValidation } from "./handleFileValidation";
32 changes: 0 additions & 32 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9242b24

Please sign in to comment.