import { DEFAULT_ERROR_TEXT } from '../ui-kit/Input/constants'; import { noop } from '../utils/noop'; import { API_BASE_URI } from './apiBaseUrl'; const SEND_FILE_URL = 'uploadFile'; export const SUCCESS_STATUS = 'SUCCESS'; export const ERROR_STATUS = 'ERROR'; export type StatusType = typeof SUCCESS_STATUS | typeof ERROR_STATUS; type ErrorCode = '0' | '1'; export interface CheckFileProps { file: File; onProgress?: (value: number) => void; } export type ResponseData = { status?: StatusType; fileId?: string; errorDesc?: string; errorCode?: ErrorCode; }; export async function sendFile({ file, onProgress = noop }: CheckFileProps): Promise { return new Promise((resolve, reject) => { const formData = new FormData(); formData.append('file', file); let processingAnimation: number | undefined = undefined; const xhr = new XMLHttpRequest(); let lastProgress: number; xhr.upload.onprogress = (event) => { if (event.lengthComputable) { const percentComplete = (event.loaded / event.total) * 50; onProgress(percentComplete); } }; xhr.upload.onload = () => { let current = 50; onProgress(current); processingAnimation = window.setInterval(() => { current += 0.2; lastProgress = current; if (current < 90) { onProgress(current); } else { onProgress(90); window.clearInterval(processingAnimation); } }, 100); }; xhr.onload = async () => { try { const response: ResponseData = JSON.parse(xhr.responseText); if (response.status === 'SUCCESS') { window.clearInterval(processingAnimation); await animateToFull(lastProgress, onProgress); resolve(response); } else { reject(new Error(response.errorDesc ?? DEFAULT_ERROR_TEXT)); } } catch (e) { reject(new Error(DEFAULT_ERROR_TEXT)); } }; xhr.onerror = () => { reject(new Error(DEFAULT_ERROR_TEXT)); }; xhr.open('POST', `${API_BASE_URI}/${SEND_FILE_URL}`); xhr.send(formData); }); } const animateToFull = (current: number, onProgress: (value: number) => void): Promise => new Promise((resolve) => { let value = current; const increment = (100 - current) / 30; const interval = 16; const step = () => { value += increment; if (value >= 100) { onProgress(100); resolve(); } else { onProgress(value); setTimeout(step, interval); } }; step(); });