delete and update a attachment in task
This commit is contained in:
@@ -209,7 +209,7 @@ const TaskDetailModal: FC<Props> = ({ open, task, columns, projectId, onClose, o
|
|||||||
isSaving={updateTask.isPending}
|
isSaving={updateTask.isPending}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<AttachmentList attachments={taskDetail?.attachments} />
|
<AttachmentList taskId={task.id} attachments={taskDetail?.attachments} />
|
||||||
|
|
||||||
<CheckLists
|
<CheckLists
|
||||||
taskId={task.id}
|
taskId={task.id}
|
||||||
|
|||||||
@@ -73,13 +73,31 @@ const AttachmentItem: FC<Props> = ({ title, createdAt, type, url, onEdit, onDele
|
|||||||
</PopoverButton>
|
</PopoverButton>
|
||||||
|
|
||||||
<PopoverPanel anchor="bottom end" className="z-[80] mt-1 rounded-[10px] bg-white shadow-md text-right p-3">
|
<PopoverPanel anchor="bottom end" className="z-[80] mt-1 rounded-[10px] bg-white shadow-md text-right p-3">
|
||||||
<button type="button" onClick={onEdit} className="w-full text-sm text-right">
|
{({ close }) => (
|
||||||
|
<>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
onEdit();
|
||||||
|
close();
|
||||||
|
}}
|
||||||
|
className="w-full text-sm text-right"
|
||||||
|
>
|
||||||
ویرایش
|
ویرایش
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<button type="button" onClick={onDelete} className="w-full mt-3 text-sm text-right text-[#FF0000]">
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
onDelete();
|
||||||
|
close();
|
||||||
|
}}
|
||||||
|
className="w-full mt-3 text-sm text-right text-[#FF0000]"
|
||||||
|
>
|
||||||
پاک کردن
|
پاک کردن
|
||||||
</button>
|
</button>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</PopoverPanel>
|
</PopoverPanel>
|
||||||
</Popover>
|
</Popover>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,15 +1,28 @@
|
|||||||
import { type FC } from "react";
|
import { useState, type FC } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import AttachmentItem from "./AttachmentItem";
|
import DefaulModal from "../../../../../components/DefaulModal";
|
||||||
import type { TaskAttachment } from "./types";
|
import ModalConfrim from "../../../../../components/ModalConfrim";
|
||||||
|
import { toast } from "../../../../../components/Toast";
|
||||||
|
import { ErrorType } from "../../../../../helpers/types";
|
||||||
|
import { useSingleUpload } from "../../../../service/hooks/useServiceData";
|
||||||
|
import { useDeleteTaskAttachment, useUpdateTaskAttachment } from "../../../task/hooks/useTaskData";
|
||||||
import type { TaskAttachmentType } from "../../../task/types/TaskTypes";
|
import type { TaskAttachmentType } from "../../../task/types/TaskTypes";
|
||||||
|
import AttachmentItem from "./AttachmentItem";
|
||||||
|
import TaskDetailAttachmentForm from "./TaskDetailAttachmentForm";
|
||||||
|
import type { TaskAttachment } from "./types";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
taskId: string;
|
||||||
attachments?: TaskAttachmentType[];
|
attachments?: TaskAttachmentType[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const AttachmentList: FC<Props> = ({ attachments = [] }) => {
|
const AttachmentList: FC<Props> = ({ taskId, attachments = [] }) => {
|
||||||
const { t } = useTranslation("global");
|
const { t } = useTranslation("global");
|
||||||
|
const singleUpload = useSingleUpload();
|
||||||
|
const updateTaskAttachment = useUpdateTaskAttachment();
|
||||||
|
const deleteTaskAttachment = useDeleteTaskAttachment();
|
||||||
|
const [deleteItemId, setDeleteItemId] = useState<string | null>(null);
|
||||||
|
const [editItem, setEditItem] = useState<TaskAttachment | null>(null);
|
||||||
|
|
||||||
const mappedAttachments: TaskAttachment[] = attachments.map((item) => ({
|
const mappedAttachments: TaskAttachment[] = attachments.map((item) => ({
|
||||||
id: item.id,
|
id: item.id,
|
||||||
@@ -22,6 +35,61 @@ const AttachmentList: FC<Props> = ({ attachments = [] }) => {
|
|||||||
const files = mappedAttachments.filter((item) => item.type === "file");
|
const files = mappedAttachments.filter((item) => item.type === "file");
|
||||||
const links = mappedAttachments.filter((item) => item.type === "link");
|
const links = mappedAttachments.filter((item) => item.type === "link");
|
||||||
|
|
||||||
|
const handleDelete = () => {
|
||||||
|
if (!deleteItemId) return;
|
||||||
|
|
||||||
|
deleteTaskAttachment.mutate(
|
||||||
|
{ id: deleteItemId, taskId },
|
||||||
|
{
|
||||||
|
onSuccess: () => {
|
||||||
|
setDeleteItemId(null);
|
||||||
|
},
|
||||||
|
onError: (error: ErrorType) => {
|
||||||
|
toast(error.response?.data?.error.message[0], "error");
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUpdate = async (data: { file?: File; title: string; linkId: string }) => {
|
||||||
|
if (!editItem) return;
|
||||||
|
|
||||||
|
let fileUrl = data.linkId.trim() || editItem.file || "";
|
||||||
|
|
||||||
|
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 as ErrorType).response?.data?.error.message[0], "error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fileUrl) return;
|
||||||
|
|
||||||
|
const title = data.title.trim() || data.file?.name || editItem.title;
|
||||||
|
const type = data.file ? "file" : editItem.type;
|
||||||
|
|
||||||
|
updateTaskAttachment.mutate(
|
||||||
|
{ id: editItem.id, params: { file: fileUrl, title, type, taskId } },
|
||||||
|
{
|
||||||
|
onSuccess: () => {
|
||||||
|
setEditItem(null);
|
||||||
|
toast(t("success"), "success");
|
||||||
|
},
|
||||||
|
onError: (error: ErrorType) => {
|
||||||
|
toast(error.response?.data?.error.message[0], "error");
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const isUpdateSubmitting = singleUpload.isPending || updateTaskAttachment.isPending;
|
||||||
|
|
||||||
if (mappedAttachments.length === 0) return null;
|
if (mappedAttachments.length === 0) return null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -40,8 +108,8 @@ const AttachmentList: FC<Props> = ({ attachments = [] }) => {
|
|||||||
createdAt={item.createdAt}
|
createdAt={item.createdAt}
|
||||||
type="file"
|
type="file"
|
||||||
url={item.file}
|
url={item.file}
|
||||||
onEdit={() => {}}
|
onEdit={() => setEditItem(item)}
|
||||||
onDelete={() => {}}
|
onDelete={() => setDeleteItemId(item.id)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
@@ -60,13 +128,38 @@ const AttachmentList: FC<Props> = ({ attachments = [] }) => {
|
|||||||
createdAt={item.createdAt}
|
createdAt={item.createdAt}
|
||||||
type="link"
|
type="link"
|
||||||
url={item.file}
|
url={item.file}
|
||||||
onEdit={() => {}}
|
onEdit={() => setEditItem(item)}
|
||||||
onDelete={() => {}}
|
onDelete={() => setDeleteItemId(item.id)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
<ModalConfrim
|
||||||
|
isOpen={deleteItemId !== null}
|
||||||
|
close={() => setDeleteItemId(null)}
|
||||||
|
onConfrim={handleDelete}
|
||||||
|
isLoading={deleteTaskAttachment.isPending}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<DefaulModal
|
||||||
|
open={editItem !== null}
|
||||||
|
close={() => setEditItem(null)}
|
||||||
|
isHeader
|
||||||
|
title_header={t("edit")}
|
||||||
|
width={420}
|
||||||
|
>
|
||||||
|
{editItem ? (
|
||||||
|
<TaskDetailAttachmentForm
|
||||||
|
initialValues={{ title: editItem.title, fileUrl: editItem.file, type: editItem.type }}
|
||||||
|
onClose={() => setEditItem(null)}
|
||||||
|
onSubmit={handleUpdate}
|
||||||
|
isSubmitting={isUpdateSubmitting}
|
||||||
|
submitLabel={t("save")}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
</DefaulModal>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
+29
-6
@@ -4,22 +4,39 @@ import Button from "../../../../../components/Button";
|
|||||||
import Input from "../../../../../components/Input";
|
import Input from "../../../../../components/Input";
|
||||||
import Select from "../../../../../components/Select";
|
import Select from "../../../../../components/Select";
|
||||||
|
|
||||||
|
type InitialValues = {
|
||||||
|
title: string;
|
||||||
|
fileUrl?: string;
|
||||||
|
type: "file" | "link";
|
||||||
|
};
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
onSubmit: (data: { file?: File; title: string; linkId: string }) => void;
|
onSubmit: (data: { file?: File; title: string; linkId: string }) => void;
|
||||||
isSubmitting?: boolean;
|
isSubmitting?: boolean;
|
||||||
|
initialValues?: InitialValues;
|
||||||
|
submitLabel?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const TaskDetailAttachmentForm: FC<Props> = ({ onClose, onSubmit, isSubmitting = false }) => {
|
const TaskDetailAttachmentForm: FC<Props> = ({
|
||||||
|
onClose,
|
||||||
|
onSubmit,
|
||||||
|
isSubmitting = false,
|
||||||
|
initialValues,
|
||||||
|
submitLabel,
|
||||||
|
}) => {
|
||||||
const { t } = useTranslation("global");
|
const { t } = useTranslation("global");
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
const [file, setFile] = useState<File | null>(null);
|
const [file, setFile] = useState<File | null>(null);
|
||||||
const [title, setTitle] = useState("");
|
const [title, setTitle] = useState(initialValues?.title ?? "");
|
||||||
const [linkId, setLinkId] = useState("");
|
const [linkId, setLinkId] = useState(initialValues?.type === "link" ? (initialValues.fileUrl ?? "") : "");
|
||||||
|
const isEdit = Boolean(initialValues);
|
||||||
|
|
||||||
const linkItems = [{ value: "", label: t("taskmanager.task_detail.none") }];
|
const linkItems = [{ value: "", label: t("taskmanager.task_detail.none") }];
|
||||||
|
|
||||||
const canSubmit = Boolean(file) || Boolean(title.trim());
|
const canSubmit = isEdit
|
||||||
|
? Boolean(title.trim()) || Boolean(file)
|
||||||
|
: Boolean(file) || Boolean(title.trim());
|
||||||
|
|
||||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const selected = e.target.files?.[0];
|
const selected = e.target.files?.[0];
|
||||||
@@ -35,6 +52,12 @@ const TaskDetailAttachmentForm: FC<Props> = ({ onClose, onSubmit, isSubmitting =
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const currentFileLabel = file
|
||||||
|
? file.name
|
||||||
|
: initialValues?.fileUrl
|
||||||
|
? initialValues.fileUrl
|
||||||
|
: t("no_select_file");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-3">
|
<div className="flex flex-col gap-3">
|
||||||
<h3 className="text-xs font-bold text-center">{t("taskmanager.task_detail.attachment")}</h3>
|
<h3 className="text-xs font-bold text-center">{t("taskmanager.task_detail.attachment")}</h3>
|
||||||
@@ -43,7 +66,7 @@ const TaskDetailAttachmentForm: FC<Props> = ({ onClose, onSubmit, isSubmitting =
|
|||||||
<button type="button" onClick={() => fileInputRef.current?.click()} className="shrink-0 h-6 rounded-lg text-[11px] px-3 bg-secondary">
|
<button type="button" onClick={() => fileInputRef.current?.click()} className="shrink-0 h-6 rounded-lg text-[11px] px-3 bg-secondary">
|
||||||
{t("select_file")}
|
{t("select_file")}
|
||||||
</button>
|
</button>
|
||||||
<span className="text-[11px] text-description truncate">{file ? file.name : t("no_select_file")}</span>
|
<span className="text-[11px] text-description truncate">{currentFileLabel}</span>
|
||||||
<input ref={fileInputRef} type="file" className="hidden" onChange={handleFileChange} />
|
<input ref={fileInputRef} type="file" className="hidden" onChange={handleFileChange} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -69,7 +92,7 @@ const TaskDetailAttachmentForm: FC<Props> = ({ onClose, onSubmit, isSubmitting =
|
|||||||
{t("cancel")}
|
{t("cancel")}
|
||||||
</Button>
|
</Button>
|
||||||
<Button type="button" onClick={handleSubmit} disabled={!canSubmit || isSubmitting} isLoading={isSubmitting} className="h-8 bg-[#292D32] text-white rounded-xl text-xs disabled:opacity-50 w-[95px]">
|
<Button type="button" onClick={handleSubmit} disabled={!canSubmit || isSubmitting} isLoading={isSubmitting} className="h-8 bg-[#292D32] text-white rounded-xl text-xs disabled:opacity-50 w-[95px]">
|
||||||
{t("taskmanager.task_detail.insert")}
|
{submitLabel ?? t("taskmanager.task_detail.insert")}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
CreateTaskRemarkType,
|
CreateTaskRemarkType,
|
||||||
CreateTaskType,
|
CreateTaskType,
|
||||||
UpdateCheckListItemType,
|
UpdateCheckListItemType,
|
||||||
|
UpdateTaskAttachmentType,
|
||||||
UpdateTaskType,
|
UpdateTaskType,
|
||||||
} from "../types/TaskTypes";
|
} from "../types/TaskTypes";
|
||||||
|
|
||||||
@@ -169,3 +170,30 @@ export const useCreateTaskAttachment = () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useUpdateTaskAttachment = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: ({ id, params }: { id: string; params: UpdateTaskAttachmentType }) =>
|
||||||
|
api.updateTaskAttachment(id, params),
|
||||||
|
onSuccess: (_data, variables) => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["task-detail", variables.params.taskId] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["tasks-by-task-phase"] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["project"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useDeleteTaskAttachment = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: ({ id }: { id: string; taskId: string }) => api.deleteTaskAttachment(id),
|
||||||
|
onSuccess: (_data, variables) => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["task-detail", variables.taskId] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["tasks-by-task-phase"] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["project"] });
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
GetTasksByTaskPhaseResponse,
|
GetTasksByTaskPhaseResponse,
|
||||||
UpdateCheckListItemResponse,
|
UpdateCheckListItemResponse,
|
||||||
UpdateCheckListItemType,
|
UpdateCheckListItemType,
|
||||||
|
UpdateTaskAttachmentType,
|
||||||
UpdateTaskType,
|
UpdateTaskType,
|
||||||
} from "../types/TaskTypes";
|
} from "../types/TaskTypes";
|
||||||
|
|
||||||
@@ -85,3 +86,13 @@ export const createTaskAttachment = async (params: CreateTaskAttachmentType) =>
|
|||||||
const { data } = await axios.post(`/task-manager/attachments`, params);
|
const { data } = await axios.post(`/task-manager/attachments`, params);
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const updateTaskAttachment = async (id: string, params: UpdateTaskAttachmentType) => {
|
||||||
|
const { data } = await axios.patch(`/task-manager/attachments/${id}`, params);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const deleteTaskAttachment = async (id: string) => {
|
||||||
|
const { data } = await axios.delete(`/task-manager/attachments/${id}`);
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|||||||
@@ -111,6 +111,13 @@ export type CreateTaskAttachmentType = {
|
|||||||
taskId: string;
|
taskId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type UpdateTaskAttachmentType = {
|
||||||
|
file: string;
|
||||||
|
title: string;
|
||||||
|
type: "file" | "link";
|
||||||
|
taskId: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type TaskDetailType = {
|
export type TaskDetailType = {
|
||||||
id: string;
|
id: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user