Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | 46x 46x 46x 46x 46x 46x 925x | import { useGetUploadFilePermission } from "@hooks/merchant-api/manage-money/useUploadBankAccountDocument";
import React, { useCallback, useMemo, useRef } from "react";
import { IFileWithMeta } from "react-dropzone-uploader";
import {
TFileAttachmentType,
TUploadFilePayload,
TUploadedFile,
UploadProgressParams,
useUploadFiles,
} from "./hooks/useUploadFiles";
import { FileUploadStatus, SnackbarFile, TSnackbarFiles } from "./types";
import { QueryKey, useQueryClient } from "react-query";
import { customInstance } from "@services/api";
import { useUploadPresignedDocument } from "@hooks/enterprise-api/merchants/useUploadPresignedDocument";
import { isNumber } from "lodash";
import { mime } from "./Rebranded/UploadFile";
import { useUploadCustomerFile } from "./hooks/useUploadCustomerFile";
import { useUploadSignature } from "./hooks/useUploadSignature";
type TPopulateSnackbarReturnedType =
| string[]
| undefined
| "upload_failed"
| TUploadedFile[];
type TPopulateSnackbarFiles = (
payload: TSnackbarFiles,
accept?: string,
challengeSlugParam?: string,
) => Promise<TPopulateSnackbarReturnedType> | null;
type ContextType = {
snackbarFiles: SnackbarFile[];
setSnackbarFiles: React.Dispatch<React.SetStateAction<SnackbarFile[]>>;
failedFiles: { current: IFileWithMeta[] };
populateSnackbarFiles: TPopulateSnackbarFiles;
isLoading: boolean;
isUploadAllowed: boolean;
isEveryFileUploaded: boolean;
clearSnackbarFiles: () => void;
updateDroppedFilesCount: (payload: number | null) => void;
onDeleteFile: (payload: {
identifier: number | string;
accountToRequest: number;
}) => void;
onUploadProgress: ({
fileId,
progress,
identifier,
}: UploadProgressParams) => void;
};
const REJECTED_FILE_STATUSES = [
FileUploadStatus.FILE_TOO_LARGE,
FileUploadStatus.FILE_NOT_SUPPORTED,
];
export const FileUploadContext = React.createContext<ContextType>({
snackbarFiles: [],
setSnackbarFiles: () => null,
failedFiles: { current: [] },
populateSnackbarFiles: () => null,
isLoading: false,
isUploadAllowed: true,
isEveryFileUploaded: false,
clearSnackbarFiles: () => null,
updateDroppedFilesCount: () => null,
onDeleteFile: () => null,
onUploadProgress: () => null,
});
// If files are too large or are not accepted types, we give them status accordingly, otherwise we consider them as in progress / ready to be uploaded
const getFileStatus = (
fileStatus: string,
type: string,
supportedFiles: string,
) => {
let status = "";
if (fileStatus === "error_file_size") {
status = FileUploadStatus.FILE_TOO_LARGE;
} else {
status = FileUploadStatus.IN_PROGRESS;
}
if (!type || !supportedFiles.includes(type))
status = FileUploadStatus.FILE_NOT_SUPPORTED;
return status;
};
const defaultSupportedFiles = mime.image + "," + mime.applications;
export const FileUploadProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
const queryClient = useQueryClient();
const [snackbarFiles, setSnackbarFiles] = React.useState<SnackbarFile[]>([]);
const failedFiles = React.useRef<IFileWithMeta[]>([]);
const { upload, isLoading } = useUpload();
const isUploadAllowed = useGetUploadFilePermission();
const droppedFilesCountRef = useRef<number | null>(null);
const updateDroppedFilesCount = useCallback(
(numberOfDroppedFiles: number | null) => {
droppedFilesCountRef.current = numberOfDroppedFiles;
},
[],
);
const clearSnackbarFiles = () => {
setSnackbarFiles([]);
};
const isEveryFileUploaded = useMemo(
() => snackbarFiles.every((item) => item.status !== "Uploading..."),
[snackbarFiles],
);
const onUploadProgress = useCallback(
({ fileId, progress, identifier }: UploadProgressParams) => {
setSnackbarFiles((prevState) => {
const indexToUpdate = prevState.findIndex((file) => file.id === fileId);
const newState = [...prevState];
if (indexToUpdate === -1) return newState;
newState[indexToUpdate].uploadProgress = progress;
newState[indexToUpdate].identifier = identifier;
return newState;
});
},
[],
);
const onDeleteFile = useCallback(
async ({
identifier,
accountToRequest,
}: {
identifier: string | number;
accountToRequest: number;
}) => {
if (isNumber(identifier)) {
await customInstance({
url: `/accounts/${accountToRequest}/files/${identifier}`,
method: "DELETE",
});
let refetcherKey: QueryKey | undefined;
setSnackbarFiles((prevState) => {
const newState = prevState.map((file) => {
const toDelete = file.identifier === identifier;
if (toDelete) {
refetcherKey = file.refetcherKeyOnDelete;
}
return {
...file,
status: toDelete ? FileUploadStatus.DELETED : file.status,
};
});
if (refetcherKey) {
queryClient.invalidateQueries(refetcherKey);
}
return newState;
});
}
},
[],
);
const onUploadFinish = useCallback(
({ identifiers }: { identifiers: (number | string)[] }) => {
setSnackbarFiles((prevState) => {
const newState = prevState.map((file: SnackbarFile) => {
const status =
file.identifier && identifiers.includes(file.identifier)
? FileUploadStatus.UPLOADED
: file.status;
return {
...file,
status,
};
});
return newState;
});
},
[],
);
const onUploadFailed = useCallback(({ fileIds }: { fileIds: string[] }) => {
setSnackbarFiles((prevState) => {
const newState = prevState.map((file: SnackbarFile) => ({
...file,
status: fileIds.includes(file.id)
? FileUploadStatus.FAILED
: file.status,
}));
return newState;
});
}, []);
const populateSnackbarFiles: TPopulateSnackbarFiles = useCallback(
async (
payload: TSnackbarFiles,
supportedFiles = defaultSupportedFiles,
challengeSlugParam?: string,
) => {
const {
allFiles: oldAllFiles,
isDeleteAllowed = true,
...uploadProps
} = payload;
const allFiles = [...oldAllFiles];
const isUnderwritingProfileFiles =
uploadProps.attachmentType === "underwriting_profile";
/* The reason for this check is that, when we drop multiple big files, they upload slowly and this function is called multiple times.
Also in case of large files during first calls of this function, allFiles variable doesn't contain all the dropped files and for this reason multiple requests are sent.
*/
if (
droppedFilesCountRef.current &&
droppedFilesCountRef.current !==
allFiles.length + failedFiles.current.length
)
return undefined;
// We need to include files that are too big along with processed files, so we can display them in stack with the necessary message
const areAllFilesProcessed = allFiles.every(({ meta }) =>
["done", "error_file_size"].includes(meta?.status),
);
if (!areAllFilesProcessed) return undefined;
const files = [...allFiles];
const snackbarData = files.map(({ meta }) => {
const { id, name, size, status: fileStatus, type } = meta;
const status = getFileStatus(fileStatus, type, supportedFiles);
return {
id,
name,
size,
status,
/* Initially all the files have progress of 0, until their upload process is started.
This variable will be updated according to the progress value returned by axios request.
*/
uploadProgress: 0,
// storing account id to use it when item is deleted from snackbar
accountToRequest: uploadProps.merchantId,
canBeDeleted: isUnderwritingProfileFiles && isDeleteAllowed,
refetcherKeyOnDelete: uploadProps.refetcherKeyOnDelete,
};
});
const rejectedSnackbarItems = snackbarData.filter((file) =>
REJECTED_FILE_STATUSES.includes(file.status as FileUploadStatus),
);
setSnackbarFiles((prevState) => {
if (!isUnderwritingProfileFiles && rejectedSnackbarItems.length > 0)
return rejectedSnackbarItems;
// take only the new files from allFiles that's not already present in snackbar list
const newData = snackbarData.filter(
(newItem) =>
!prevState.some((snackbarItem) => snackbarItem.id === newItem.id),
);
const newSnackbarData = [...prevState, ...newData];
return newSnackbarData;
});
// // We need to filter files here, to make sure that only valid files are uploaded
const filesToUpload = files
.filter(({ meta }) => {
return (
meta.status === "done" &&
meta.type &&
supportedFiles.includes(meta.type)
);
})
.map((f) => ({
file: f.file,
id: f.meta.id,
}));
// Remove all files from dropzone in documents drop areas
if (
uploadProps.attachmentType === "underwriting_profile" ||
uploadProps.attachmentType === "conversation_message"
) {
updateDroppedFilesCount(null);
allFiles.forEach((f: IFileWithMeta) => {
f.remove && f.remove();
});
}
// return and dont call api there are rejected file types and uploading for bank account/pah
if (!isUnderwritingProfileFiles && rejectedSnackbarItems.length > 0)
return "upload_failed";
// If after filtering array contains items, we proceed with uploading, otherwise we hide snackbar after 3 seconds
const uploadedFiles =
(await upload(
{
...uploadProps,
list: filesToUpload,
onUploadProgress,
onUploadFinish: payload.customFinish || onUploadFinish,
onUploadFailed: payload.customFailed || onUploadFailed,
isCustomerUpload: payload?.isCustomerUpload,
isSignatureUpload: payload?.isSignatureUpload,
},
challengeSlugParam,
)) || [];
return uploadedFiles;
},
[],
);
return (
<FileUploadContext.Provider
value={{
snackbarFiles,
setSnackbarFiles,
failedFiles: failedFiles,
populateSnackbarFiles,
isLoading,
isUploadAllowed,
clearSnackbarFiles,
updateDroppedFilesCount,
onDeleteFile,
isEveryFileUploaded,
onUploadProgress,
}}
>
{children}
</FileUploadContext.Provider>
);
};
type TUploadArgs = Omit<TUploadFilePayload, "merchantId" | "resourceID"> & {
merchantId?: number;
resourceID?: number;
attachmentType?: TFileAttachmentType;
};
const useUpload = () => {
const { handleUpload, isLoading } = useUploadFiles();
const { handlePostImage } = useUploadCustomerFile();
const { handleUpload: uploadPresigned, isLoading: presignedLoading } =
useUploadPresignedDocument();
const { handleUploadSignature } = useUploadSignature();
const upload = async (args: TUploadArgs, challengeSlugParam?: string) => {
// when we have a merchant
if (
args.list.length > 0 &&
args.merchantId &&
args.resourceID &&
!args?.isCustomerUpload &&
!args?.isSignatureUpload
) {
const uploadedFiles = await handleUpload(
{
...args,
merchantId: args.merchantId,
resourceID: args.resourceID,
},
challengeSlugParam,
);
return uploadedFiles;
}
// when we create merchant / provider
else if (
args.list.length > 0 &&
!args.merchantId &&
!args?.isCustomerUpload &&
!args?.isSignatureUpload
) {
const uploadedFiles = await uploadPresigned({
list: args.list,
onUploadProgress: args.onUploadProgress,
onUploadFinish: args.onUploadFinish,
onUploadFailed: args.onUploadFailed,
attachmentType: args.attachmentType,
});
return uploadedFiles;
} else if (args?.isCustomerUpload) {
const uploadedFile = await handlePostImage({
list: args.list,
onUploadProgress: args.onUploadProgress,
onUploadFinish: args.onUploadFinish,
onUploadFailed: args.onUploadFailed,
});
return [uploadedFile];
} else if (args?.isSignatureUpload) {
const uploadedSignature = await handleUploadSignature({
list: args.list,
onUploadProgress: args.onUploadProgress,
onUploadFinish: args.onUploadFinish,
onUploadFailed: args.onUploadFailed,
});
return [uploadedSignature];
} else {
setTimeout(() => {
if (args.clearSnackbarFiles) args.clearSnackbarFiles();
}, 3000);
}
};
return { upload, isLoading: isLoading || presignedLoading };
};
export const useFileUploadContext = () => React.useContext(FileUploadContext);
|