Skip to content

Commit

Permalink
Fix: Error Toast Message for Issue Attachment (#5424)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharma01ketan authored Aug 26, 2024
1 parent 0dce67b commit 4689ebe
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 37 deletions.
57 changes: 40 additions & 17 deletions web/core/components/issues/attachment/attachment-item-list.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { FC, useCallback, useState } from "react";
import { observer } from "mobx-react";
import { useDropzone } from "react-dropzone";
import { FileRejection, useDropzone } from "react-dropzone";
import { UploadCloud } from "lucide-react";
// hooks
import {TOAST_TYPE, setToast } from "@plane/ui";
import { MAX_FILE_SIZE } from "@/constants/common";
import { generateFileName } from "@/helpers/attachment.helper";
import { useInstance, useIssueDetail } from "@/hooks/store";
Expand Down Expand Up @@ -36,24 +37,46 @@ export const IssueAttachmentItemList: FC<TIssueAttachmentItemList> = observer((p
const issueAttachments = getAttachmentsByIssueId(issueId);

const onDrop = useCallback(
(acceptedFiles: File[]) => {
const currentFile: File = acceptedFiles[0];
if (!currentFile || !workspaceSlug) return;
(acceptedFiles: File[], rejectedFiles:FileRejection[] ) => {
const totalAttachedFiles = acceptedFiles.length + rejectedFiles.length;

const uploadedFile: File = new File([currentFile], generateFileName(currentFile.name), {
type: currentFile.type,
});
const formData = new FormData();
formData.append("asset", uploadedFile);
formData.append(
"attributes",
JSON.stringify({
name: uploadedFile.name,
size: uploadedFile.size,
if(rejectedFiles.length===0){
const currentFile: File = acceptedFiles[0];
if (!currentFile || !workspaceSlug) return;

const uploadedFile: File = new File([currentFile], generateFileName(currentFile.name), {
type: currentFile.type,
});
const formData = new FormData();
formData.append("asset", uploadedFile);
formData.append(
"attributes",
JSON.stringify({
name: uploadedFile.name,
size: uploadedFile.size,
})
);
setIsLoading(true);
handleAttachmentOperations.create(formData)
.catch(()=>{
setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: "File could not be attached. Try uploading again.",
})
})
);
setIsLoading(true);
handleAttachmentOperations.create(formData).finally(() => setIsLoading(false));
.finally(() => setIsLoading(false));
return;
}

setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: (totalAttachedFiles>1)?
"Only one file can be uploaded at a time." :
"File must be 5MB or less.",
})
return;
},
[handleAttachmentOperations, workspaceSlug]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"use client";
import React, { FC, useCallback, useState } from "react";
import { observer } from "mobx-react";
import { useDropzone } from "react-dropzone";
import { FileRejection, useDropzone } from "react-dropzone";
import { Plus } from "lucide-react";
import {TOAST_TYPE, setToast } from "@plane/ui";
// constants
import { MAX_FILE_SIZE } from "@/constants/common";
// helper
Expand Down Expand Up @@ -33,31 +34,54 @@ export const IssueAttachmentActionButton: FC<Props> = observer((props) => {

// handlers
const onDrop = useCallback(
(acceptedFiles: File[]) => {
const currentFile: File = acceptedFiles[0];
if (!currentFile || !workspaceSlug) return;
(acceptedFiles: File[], rejectedFiles:FileRejection[] ) => {
const totalAttachedFiles = acceptedFiles.length + rejectedFiles.length;

const uploadedFile: File = new File([currentFile], generateFileName(currentFile.name), {
type: currentFile.type,
});
const formData = new FormData();
formData.append("asset", uploadedFile);
formData.append(
"attributes",
JSON.stringify({
name: uploadedFile.name,
size: uploadedFile.size,
if(rejectedFiles.length===0){
const currentFile: File = acceptedFiles[0];
if (!currentFile || !workspaceSlug) return;

const uploadedFile: File = new File([currentFile], generateFileName(currentFile.name), {
type: currentFile.type,
});
const formData = new FormData();
formData.append("asset", uploadedFile);
formData.append(
"attributes",
JSON.stringify({
name: uploadedFile.name,
size: uploadedFile.size,
})
);
setIsLoading(true);
handleAttachmentOperations.create(formData)
.catch(()=>{
setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: "File could not be attached. Try uploading again.",
})
})
);
setIsLoading(true);
handleAttachmentOperations.create(formData).finally(() => {
setLastWidgetAction("attachments");
setIsLoading(false);
.finally(() => {
setLastWidgetAction("attachments");
setIsLoading(false);
});
return;
}

setToast({
type: TOAST_TYPE.ERROR,
title: "Error!",
message: (totalAttachedFiles>1)?
"Only one file can be uploaded at a time." :
"File must be 5MB or less.",
})
return;
},
[handleAttachmentOperations, workspaceSlug]
);


const { getRootProps, getInputProps } = useDropzone({
onDrop,
maxSize: config?.file_size_limit ?? MAX_FILE_SIZE,
Expand All @@ -71,4 +95,4 @@ export const IssueAttachmentActionButton: FC<Props> = observer((props) => {
{customButton ? customButton : <Plus className="h-4 w-4" />}
</button>
);
});
});

0 comments on commit 4689ebe

Please sign in to comment.