edit check list item
This commit is contained in:
@@ -1,17 +1,83 @@
|
||||
import { Popover, PopoverButton, PopoverPanel } from "@headlessui/react";
|
||||
import { More } from "iconsax-react";
|
||||
import { type FC } from "react";
|
||||
import { type FC, useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import TaskDetailLabelCheckbox from "../labels/TaskDetailLabelCheckbox";
|
||||
|
||||
type Props = {
|
||||
title: string;
|
||||
checked: boolean;
|
||||
onToggle: () => void;
|
||||
onEdit: () => void;
|
||||
onSave: (title: string) => void;
|
||||
onDelete: () => void;
|
||||
isSaving?: boolean;
|
||||
};
|
||||
|
||||
const ChecklistItem: FC<Props> = ({ title, checked, onToggle, onEdit, onDelete }) => {
|
||||
const ChecklistItem: FC<Props> = ({ title, checked, onToggle, onSave, onDelete, isSaving = false }) => {
|
||||
const { t } = useTranslation("global");
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
const [editTitle, setEditTitle] = useState(title);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isEditing) {
|
||||
setEditTitle(title);
|
||||
}
|
||||
}, [title, isEditing]);
|
||||
|
||||
const handleSave = () => {
|
||||
const trimmed = editTitle.trim();
|
||||
if (!trimmed || trimmed === title) {
|
||||
setIsEditing(false);
|
||||
setEditTitle(title);
|
||||
return;
|
||||
}
|
||||
onSave(trimmed);
|
||||
setIsEditing(false);
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
setIsEditing(false);
|
||||
setEditTitle(title);
|
||||
};
|
||||
|
||||
if (isEditing) {
|
||||
return (
|
||||
<div className="flex gap-1 items-center">
|
||||
<TaskDetailLabelCheckbox checked={checked} onToggle={onToggle} />
|
||||
|
||||
<div className="flex-1 flex gap-2 items-center">
|
||||
<input
|
||||
value={editTitle}
|
||||
onChange={(e) => setEditTitle(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === "Enter") handleSave();
|
||||
if (e.key === "Escape") handleCancel();
|
||||
}}
|
||||
autoFocus
|
||||
disabled={isSaving}
|
||||
className="flex-1 h-[27px] px-2 text-xs bg-white/50 rounded outline-none"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSave}
|
||||
disabled={!editTitle.trim() || isSaving}
|
||||
className="h-7 px-3 rounded-lg bg-[#292D32] text-white text-[11px] disabled:opacity-50"
|
||||
>
|
||||
{t("save")}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleCancel}
|
||||
disabled={isSaving}
|
||||
className="h-7 px-3 rounded-lg bg-white/50 text-[#292D32] text-[11px] border border-white disabled:opacity-50"
|
||||
>
|
||||
{t("cancel")}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex gap-1 h-[27px] items-center">
|
||||
<TaskDetailLabelCheckbox checked={checked} onToggle={onToggle} />
|
||||
@@ -25,13 +91,31 @@ const ChecklistItem: FC<Props> = ({ title, checked, onToggle, onEdit, onDelete }
|
||||
</PopoverButton>
|
||||
|
||||
<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={() => {
|
||||
close();
|
||||
setIsEditing(true);
|
||||
}}
|
||||
className="w-full text-sm text-right"
|
||||
>
|
||||
ویرایش
|
||||
</button>
|
||||
|
||||
<button type="button" onClick={onDelete} className="w-full mt-3 text-sm text-right text-[#FF0000]">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
close();
|
||||
onDelete();
|
||||
}}
|
||||
className="w-full mt-3 text-sm text-right text-[#FF0000]"
|
||||
>
|
||||
پاک کردن
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</PopoverPanel>
|
||||
</Popover>
|
||||
</div>
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Edit } from "iconsax-react";
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import ModalConfrim from "../../../../../components/ModalConfrim";
|
||||
import { toast } from '../../../../../components/Toast';
|
||||
import { toast } from "../../../../../components/Toast";
|
||||
import ProgressBar from "../../../../../components/ProgressBar";
|
||||
import { ErrorType } from "../../../../../helpers/types";
|
||||
import { useDeleteCheckListItem, useUpdateCheckListItem } from "../../../task/hooks/useTaskData";
|
||||
@@ -37,12 +37,23 @@ const CheckLists = ({
|
||||
const doneCount = completedCheckListItemCount ?? items.filter((item) => item.checked).length;
|
||||
const progress = totalCount > 0 ? Math.round((doneCount / totalCount) * 100) : 0;
|
||||
|
||||
const handleToggle = (id: string, checked: boolean) => {
|
||||
const handleToggle = (id: string, title: string, checked: boolean) => {
|
||||
updateCheckListItem.mutate(
|
||||
{ id, params: { isDone: !checked }, taskId },
|
||||
{ id, params: { title, isDone: !checked, taskId }, taskId },
|
||||
{
|
||||
onError: (error: ErrorType) => {
|
||||
toast(error.response?.data?.error.message[0], 'error');
|
||||
toast(error.response?.data?.error.message[0], "error");
|
||||
},
|
||||
},
|
||||
);
|
||||
};
|
||||
|
||||
const handleSave = (id: string, title: string, checked: boolean) => {
|
||||
updateCheckListItem.mutate(
|
||||
{ id, params: { title, isDone: checked, taskId }, taskId },
|
||||
{
|
||||
onError: (error: ErrorType) => {
|
||||
toast(error.response?.data?.error.message[0], "error");
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -58,7 +69,7 @@ const CheckLists = ({
|
||||
setDeleteItemId(null);
|
||||
},
|
||||
onError: (error: ErrorType) => {
|
||||
toast(error.response?.data?.error.message[0], 'error');
|
||||
toast(error.response?.data?.error.message[0], "error");
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -84,9 +95,10 @@ const CheckLists = ({
|
||||
key={item.id}
|
||||
title={item.title}
|
||||
checked={item.checked}
|
||||
onToggle={() => handleToggle(item.id, item.checked)}
|
||||
onEdit={() => {}}
|
||||
onToggle={() => handleToggle(item.id, item.title, item.checked)}
|
||||
onSave={(title) => handleSave(item.id, title, item.checked)}
|
||||
onDelete={() => setDeleteItemId(item.id)}
|
||||
isSaving={updateCheckListItem.isPending}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -159,7 +159,13 @@ export const useUpdateCheckListItem = () => {
|
||||
if (!current?.data?.checkListItems) return current;
|
||||
|
||||
const checkListItems = current.data.checkListItems.map((item) =>
|
||||
item.id === id ? { ...item, isDone: params.isDone } : item,
|
||||
item.id === id
|
||||
? {
|
||||
...item,
|
||||
...(params.isDone !== undefined ? { isDone: params.isDone } : {}),
|
||||
...(params.title !== undefined ? { title: params.title } : {}),
|
||||
}
|
||||
: item,
|
||||
);
|
||||
const completedCheckListItemCount = checkListItems.filter((item) => item.isDone).length;
|
||||
|
||||
|
||||
@@ -83,7 +83,9 @@ export interface CreateCheckListItemResponse extends IResponse<{ checkListItem:
|
||||
}
|
||||
|
||||
export type UpdateCheckListItemType = {
|
||||
isDone: boolean;
|
||||
title?: string;
|
||||
isDone?: boolean;
|
||||
taskId?: string;
|
||||
};
|
||||
|
||||
export interface UpdateCheckListItemResponse extends IResponse<{ checkListItem: TaskChecklistItemType }> {
|
||||
|
||||
Reference in New Issue
Block a user