Skip to content

Commit

Permalink
Feat: #115 병렬 처리된 파일의 성공과 실패 목록 메세지 노출
Browse files Browse the repository at this point in the history
  • Loading branch information
Seok93 committed Sep 27, 2024
1 parent 5a4d769 commit 613775b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/components/modal/task/CreateModalTask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { SubmitHandler } from 'react-hook-form';
import type { Task, TaskForm } from '@/types/TaskType';
import type { Project } from '@/types/ProjectType';
import type { ProjectStatus } from '@/types/ProjectStatusType';
import type { FileUploadFailureResult, FileUploadSuccessResult } from '@/types/FileType';

type CreateModalTaskProps = {
project: Project;
Expand All @@ -35,22 +36,33 @@ export default function CreateModalTask({ project, onClose: handleClose }: Creat
const taskFilesUpload = async (taskId: Task['taskId'], files: File[]) => {
const createFilePromises = files.map((file) =>
createTaskFileMutateAsync({ taskId, file }).then(
() => ({ status: 'fulfilled', file }),
(error) => ({ status: 'rejected', file, error }),
(): FileUploadSuccessResult => ({ status: 'fulfilled', file }),
(error): FileUploadFailureResult => ({ status: 'rejected', file, error }),
),
);

const results = await Promise.allSettled(createFilePromises);
const results = (await Promise.allSettled(createFilePromises)) as PromiseFulfilledResult<
FileUploadSuccessResult | FileUploadFailureResult
>[];
const queryKey = ['projects', project.projectId, 'tasks'];
queryClient.invalidateQueries({ queryKey });

const fulfilledFileList = results.filter((result) => result.status === 'fulfilled');
const fulfilledFilesName = fulfilledFileList.map((result) => result.value.file.name).join(', ');
toastSuccess(`${fulfilledFilesName} 파일 업로드에 성공했습니다.`);
const fulfilledFileList: FileUploadSuccessResult[] = [];
const rejectedFileList: FileUploadFailureResult[] = [];
results
.map((result) => result.value)
.forEach((result) => {
if (result.status === 'fulfilled') fulfilledFileList.push(result);
else rejectedFileList.push(result);
});

if (fulfilledFileList.length > 0) {
const fulfilledFilesName = fulfilledFileList.map((result) => result.file.name).join(', ');
toastSuccess(`${fulfilledFilesName} 파일 업로드에 성공했습니다.`);
}

const rejectedFileList = results.filter((result) => result.status === 'rejected');
if (rejectedFileList.length > 0) {
const rejectedFilesName = rejectedFileList.map((result) => result.reason.file.name).join(', ');
const rejectedFilesName = rejectedFileList.map((result) => result.file.name).join(', ');
toastError(`${rejectedFilesName} 파일 업로드에 실패했습니다.`);
}
};
Expand Down
10 changes: 10 additions & 0 deletions src/types/FileType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ export type TaskFile = {
};

export type FileInfo = TaskFile | CustomFile;

export type FileUploadSuccessResult = {
status: 'fulfilled';
file: File;
};
export type FileUploadFailureResult = {
status: 'rejected';
file: File;
error: unknown;
};

0 comments on commit 613775b

Please sign in to comment.