import { formatDate, formatDuration, promisifyMutation } from "@probo/helpers"; import { usePageTitle } from "@probo/hooks"; import { useTranslate } from "@probo/i18n"; import { ActionDropdown, Card, DropdownItem, IconArrowCornerDownLeft, IconPencil, IconTrashCan, PriorityLevel, Spinner, TabBadge, TabItem, Tabs, TaskStateIcon, useConfirm, useDialogRef, } from "@probo/ui"; import { Fragment } from "react"; import { graphql, useFragment, useMutation, useRelayEnvironment, } from "react-relay"; import { Link, useLocation, useParams } from "react-router"; import type { MeasureTasksTabQuery$data } from "#/__generated__/core/MeasureTasksTabQuery.graphql"; import type { TaskFormDialogFragment$key } from "#/__generated__/core/TaskFormDialogFragment.graphql"; import type { TaskFormDialogUpdateMutation } from "#/__generated__/core/TaskFormDialogUpdateMutation.graphql"; import type { TasksCard_TaskRowFragment$key } from "#/__generated__/core/TasksCard_TaskRowFragment.graphql"; import type { TasksCardDeleteMutation } from "#/__generated__/core/TasksCardDeleteMutation.graphql"; import type { TasksPageFragment$data } from "#/__generated__/core/TasksPageFragment.graphql"; import TaskFormDialog, { taskUpdateMutation, } from "#/components/tasks/TaskFormDialog"; import { updateStoreCounter } from "#/hooks/useMutationWithIncrement"; import { useOrganizationId } from "#/hooks/useOrganizationId"; type Props = { tasks: | TasksPageFragment$data["tasks"]["edges"] | Extract< MeasureTasksTabQuery$data["node"], { __typename: "Measure" } >["tasks"]["edges"]; connectionId: string; }; export function TasksCard({ tasks, connectionId }: Props) { const { __ } = useTranslate(); const hash = useLocation().hash.replace("#", ""); const hashes = [ { hash: "", label: __("To do"), state: "TODO" }, { hash: "done", label: __("Done"), state: "DONE" }, { hash: "all", label: __("All"), state: null }, ] as const; const tasksPerHash = new Map([ ["", tasks?.filter(({ node }) => node.state === "TODO")], ["done", tasks?.filter(({ node }) => node.state === "DONE")], ["all", tasks], ]); const filteredTasks = tasksPerHash.get(hash) ?? []; usePageTitle(__("Tasks")); return (
{tasks.length === 0 ? (

{__("No tasks")}

) : ( {hashes.map(h => ( {h.label} {tasksPerHash.get(h.hash)?.length} ))}
{hash === "all" // All tabs group the todo using the state ? hashes .slice(0, 2) .filter(h => tasksPerHash.get(h.hash)?.length) .map(h => (

{h.label}

{tasksPerHash.get(h.hash)?.map(({ node: task }) => ( ))}
)) // Todo and Done tab simply list todos : filteredTasks?.map(({ node: task }) => ( ))}
)}
); } type TaskRowProps = { fKey: TasksCard_TaskRowFragment$key | TaskFormDialogFragment$key; connectionId: string; }; const fragment = graphql` fragment TasksCard_TaskRowFragment on Task { id name state description timeEstimate deadline canUpdate: permission(action: "core:task:update") canDelete: permission(action: "core:task:delete") assignedTo { id fullName } measure { id name } } `; const deleteMutation = graphql` mutation TasksCardDeleteMutation( $input: DeleteTaskInput! $connections: [ID!]! ) { deleteTask(input: $input) { deletedTaskId @deleteEdge(connections: $connections) } } `; function TaskRow(props: TaskRowProps) { const organizationId = useOrganizationId(); const dialogRef = useDialogRef(); const { __ } = useTranslate(); const confirm = useConfirm(); const [deleteTask] = useMutation(deleteMutation); const params = useParams<{ measureId?: string }>(); const relayEnv = useRelayEnvironment(); const { canUpdate, canDelete, ...task } = useFragment( fragment, props.fKey as TasksCard_TaskRowFragment$key, ); const [updateTask, isUpdating] = useMutation(taskUpdateMutation); const onToggle = async () => { await promisifyMutation(updateTask)({ variables: { input: { taskId: task.id, state: task.state === "TODO" ? "DONE" : "TODO", }, }, }); }; const onDelete = () => { confirm( () => promisifyMutation(deleteTask)({ variables: { input: { taskId: task.id }, connections: [props.connectionId], }, onCompleted: (_response, errors) => { if (!errors && params.measureId) { updateStoreCounter( relayEnv, params.measureId, "tasks(first:0)", -1, ); } }, }), { message: "Are you sure you want to delete this task?", }, ); }; return ( <>

{task.name}

{task.description && (

{task.description}

)}
{task.measure && ( {task.measure?.name} )} {task.timeEstimate && ( {formatDuration(task.timeEstimate, __)} )} {task.deadline && ( )}
{task.assignedTo?.fullName && (
{task.assignedTo.fullName}
)}
{isUpdating && } {(canUpdate || canDelete) && ( {canUpdate && ( dialogRef.current?.open()} > {__("Edit")} )} {canDelete && ( {__("Delete")} )} )}
); }