attachment
This commit is contained in:
@@ -17,7 +17,8 @@ import TaskDetailMetadataPreview from "./TaskDetailMetadataPreview";
|
||||
import type { TaskDetailTab } from "./types";
|
||||
import TaskDetailUsersPopover from "./users/TaskDetailUsersPopover";
|
||||
import type { TaskDetailType } from "../../task/types/TaskTypes";
|
||||
import { useCreateCheckListItem, useCreateTaskRemark, useUpdateTask } from "../../task/hooks/useTaskData";
|
||||
import { useCreateCheckListItem, useCreateTaskAttachment, useCreateTaskRemark, useUpdateTask } from "../../task/hooks/useTaskData";
|
||||
import { useSingleUpload } from "../../../service/hooks/useServiceData";
|
||||
import { ErrorType } from "../../../../helpers/types";
|
||||
|
||||
type Props = {
|
||||
@@ -37,6 +38,8 @@ const TaskDetailToolbar: FC<Props> = ({ activeTab, onTabChange, taskDetail, task
|
||||
const { t } = useTranslation("global");
|
||||
const createTaskRemark = useCreateTaskRemark();
|
||||
const createCheckListItem = useCreateCheckListItem();
|
||||
const createTaskAttachment = useCreateTaskAttachment();
|
||||
const singleUpload = useSingleUpload();
|
||||
const updateTask = useUpdateTask();
|
||||
const getUsers = useGetUsers("");
|
||||
const [labelsView, setLabelsView] = useState<LabelsView>("list");
|
||||
@@ -204,10 +207,46 @@ const TaskDetailToolbar: FC<Props> = ({ activeTab, onTabChange, taskDetail, task
|
||||
if (isAttachmentOpen) onTabChange("attachment");
|
||||
};
|
||||
|
||||
const handleAttachmentCreate = (_data: { file?: File; title: string; linkId: string }) => {
|
||||
handleAttachmentClose();
|
||||
const handleAttachmentCreate = async (data: { file?: File; title: string; linkId: string }) => {
|
||||
const effectiveTaskId = taskId || taskDetail?.id;
|
||||
if (!effectiveTaskId) return;
|
||||
|
||||
let fileUrl = data.linkId.trim();
|
||||
|
||||
if (data.file) {
|
||||
const formData = new FormData();
|
||||
formData.append("file", data.file);
|
||||
|
||||
try {
|
||||
const uploadResult = await singleUpload.mutateAsync(formData);
|
||||
fileUrl = uploadResult?.data?.url ?? "";
|
||||
} catch (error) {
|
||||
toast.error((error as ErrorType).response?.data?.error.message[0]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!fileUrl) return;
|
||||
|
||||
const title = data.title.trim() || data.file?.name || "";
|
||||
const type = data.file ? "file" : "link";
|
||||
|
||||
createTaskAttachment.mutate(
|
||||
{ file: fileUrl, title, type, taskId: effectiveTaskId },
|
||||
{
|
||||
onSuccess: () => {
|
||||
handleAttachmentClose();
|
||||
toast.success(t("success"));
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast.error(error.response?.data?.error.message[0]);
|
||||
},
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
const isAttachmentSubmitting = singleUpload.isPending || createTaskAttachment.isPending;
|
||||
|
||||
const handleDateClose = () => {
|
||||
if (isDateOpen) onTabChange("date");
|
||||
};
|
||||
@@ -258,7 +297,11 @@ const TaskDetailToolbar: FC<Props> = ({ activeTab, onTabChange, taskDetail, task
|
||||
}
|
||||
attachmentPopover={
|
||||
isAttachmentOpen ? (
|
||||
<TaskDetailAttachmentPopover onClose={handleAttachmentClose} onSubmit={handleAttachmentCreate} />
|
||||
<TaskDetailAttachmentPopover
|
||||
onClose={handleAttachmentClose}
|
||||
onSubmit={handleAttachmentCreate}
|
||||
isSubmitting={isAttachmentSubmitting}
|
||||
/>
|
||||
) : null
|
||||
}
|
||||
datePopover={
|
||||
|
||||
Reference in New Issue
Block a user