View update when upload attachment files

This commit is contained in:
Takeshi Kimata
2024-03-20 15:56:31 +09:00
parent 1634cfc252
commit 8a5ae80351
2 changed files with 36 additions and 5 deletions

View File

@@ -15,7 +15,7 @@ import { Save, Plus, ArrowLeft, ArrowUpFromLine } from "lucide-react";
import { priorities, testTypes, templates } from "@/config/selection"; import { priorities, testTypes, templates } from "@/config/selection";
import CaseStepsEditor from "./case-steps-editor"; import CaseStepsEditor from "./case-steps-editor";
import CaseAttachmentsEditor from "./case-attachments-editor"; import CaseAttachmentsEditor from "./case-attachments-editor";
import { CaseType } from "./caseTypes"; import { CaseType, AttachmentType } from "./caseTypes";
import { import {
fetchCase, fetchCase,
fetchCreateStep, fetchCreateStep,
@@ -105,17 +105,48 @@ export default function CaseEditor({
}); });
}; };
const handleDrop = (event) => { const handleDrop = async (event) => {
event.preventDefault(); event.preventDefault();
fetchCreateAttachments(params.caseId, event.dataTransfer.files); handleFetchCreateAttachments(params.caseId, event.dataTransfer.files);
}; };
const handleInput = (event) => { const handleInput = (event) => {
fetchCreateAttachments(params.caseId, event.target.files); handleFetchCreateAttachments(params.caseId, event.target.files);
};
const handleFetchCreateAttachments = async (
caseId: number,
files: File[]
) => {
const newAttachments = await fetchCreateAttachments(caseId, files);
if (newAttachments) {
const newAttachmentsWithJoinTable = [];
newAttachments.forEach((attachment: AttachmentType) => {
attachment.caseAttachments = { AttachmentId: attachment.id };
newAttachmentsWithJoinTable.push(attachment);
});
const updatedAttachments = testCase.Attachments;
updatedAttachments.push(...newAttachments);
setTestCase({
...testCase,
Attachments: updatedAttachments,
});
}
}; };
const onAttachmentDelete = async (attachmentId: number) => { const onAttachmentDelete = async (attachmentId: number) => {
await fetchDeleteAttachment(attachmentId); await fetchDeleteAttachment(attachmentId);
const filteredAttachments = testCase.Attachments.filter(
(attachment) => attachment.id !== attachmentId
);
setTestCase({
...testCase,
Attachments: filteredAttachments,
});
}; };
useEffect(() => { useEffect(() => {

View File

@@ -44,7 +44,7 @@ type AttachmentType = {
path: string; path: string;
createdAt: Date; createdAt: Date;
updatedAt: Date; updatedAt: Date;
caseSteps: CaseAttachmentType; caseAttachments: CaseAttachmentType;
}; };
export { CaseType, StepType, AttachmentType }; export { CaseType, StepType, AttachmentType };