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 | 46x | import React from "react";
import axios from "axios";
import { customInstance } from "@services/api";
import { debounce } from "lodash";
import {
TFileAttachmentType,
UploadProgressParams,
} from "@components/UploadFile/hooks/useUploadFiles";
type handleUploadProps = {
list: { file: File; id: string }[];
onUploadProgress: (params: UploadProgressParams) => void;
onUploadFinish(params: { identifiers: (number | string)[] }): void;
onUploadFailed(params: { fileIds: string[] }): void;
attachmentType?: TFileAttachmentType;
};
export type HandlePresignedUploadReturnType = string[] | "upload_failed";
export const useUploadPresignedDocument = () => {
const [isLoading, setIsLoading] = React.useState(false);
const handleUpload = ({
list,
onUploadProgress,
onUploadFailed,
onUploadFinish,
attachmentType = "bank_account",
}: handleUploadProps) => {
const customDocumentList: string[] = [];
setIsLoading(true);
const isConversation = attachmentType === "conversation_message";
return new Promise<HandlePresignedUploadReturnType>(function (
resolve,
reject,
) {
if (list.length === 0) return resolve([]);
Promise.all(
list.map((document) =>
customInstance({
url: `s3/presign-url`,
method: "POST",
data: {
attachmentType: attachmentType,
fileName: document.file.name,
},
}),
),
)
.then((res) => {
return Promise.all(
res.map((item, index) => {
customDocumentList.push(
isConversation ? item.presignedURL : item.URL,
);
return axios.put(item.presignedURL, list[index].file, {
onUploadProgress: debounce((progressEvent: ProgressEvent) => {
onUploadProgress({
identifier: isConversation ? item.presignedURL : item.URL,
fileId: list[index].id,
progress:
(progressEvent.loaded * 100) / progressEvent.total,
});
}, 100) as any,
});
}),
);
})
.then(() => {
onUploadFinish({ identifiers: customDocumentList });
resolve(customDocumentList);
})
.catch(() => {
onUploadFailed({ fileIds: list.map((file) => file.id) });
resolve("upload_failed");
})
.finally(() => {
setIsLoading(false);
});
});
};
return { handleUpload, isLoading };
};
|