import React, { useCallback } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // react-beautiful-dnd import { Draggable, DraggableStateSnapshot, DraggingStyle, NotDraggingStyle, } from "react-beautiful-dnd"; // constants import { TrashIcon } from "@heroicons/react/24/outline"; // services import issuesService from "services/issues.service"; import stateService from "services/state.service"; // components import { AssigneeSelect, DueDateSelect, PrioritySelect, StateSelect } from "components/core/select"; // types import { CycleIssueResponse, IIssue, IProjectMember, IssueResponse, ModuleIssueResponse, NestedKeyOf, Properties, UserAuth, } from "types"; // fetch-keys import { STATE_LIST, CYCLE_ISSUES, MODULE_ISSUES, PROJECT_ISSUES_LIST } from "constants/fetch-keys"; type Props = { index: number; type?: string; issue: IIssue; properties: Properties; members: IProjectMember[] | undefined; handleDeleteIssue: (issue: IIssue) => void; orderBy: NestedKeyOf | "manual" | null; userAuth: UserAuth; }; export const SingleBoardIssue: React.FC = ({ index, type, issue, properties, members, handleDeleteIssue, orderBy, userAuth, }) => { const router = useRouter(); const { workspaceSlug, projectId, cycleId, moduleId } = router.query; const { data: states } = useSWR( workspaceSlug && projectId ? STATE_LIST(projectId as string) : null, workspaceSlug && projectId ? () => stateService.getStates(workspaceSlug as string, projectId as string) : null ); const partialUpdateIssue = useCallback( (formData: Partial) => { if (!workspaceSlug || !projectId) return; if (cycleId) mutate( CYCLE_ISSUES(cycleId as string), (prevData) => { const updatedIssues = (prevData ?? []).map((p) => { if (p.issue_detail.id === issue.id) { return { ...p, issue_detail: { ...p.issue_detail, ...formData, }, }; } return p; }); return [...updatedIssues]; }, false ); if (moduleId) mutate( MODULE_ISSUES(moduleId as string), (prevData) => { const updatedIssues = (prevData ?? []).map((p) => { if (p.issue_detail.id === issue.id) { return { ...p, issue_detail: { ...p.issue_detail, ...formData, }, }; } return p; }); return [...updatedIssues]; }, false ); mutate( PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string), (prevData) => ({ ...(prevData as IssueResponse), results: (prevData?.results ?? []).map((p) => { if (p.id === issue.id) return { ...p, ...formData }; return p; }), }), false ); issuesService .patchIssue(workspaceSlug as string, projectId as string, issue.id, formData) .then((res) => { mutate( cycleId ? CYCLE_ISSUES(cycleId as string) : CYCLE_ISSUES(issue?.issue_cycle?.cycle ?? "") ); mutate( moduleId ? MODULE_ISSUES(moduleId as string) : MODULE_ISSUES(issue?.issue_module?.module ?? "") ); mutate(PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string)); }) .catch((error) => { console.log(error); }); }, [workspaceSlug, projectId, cycleId, moduleId, issue] ); function getStyle( style: DraggingStyle | NotDraggingStyle | undefined, snapshot: DraggableStateSnapshot ) { if (orderBy === "manual") return style; if (!snapshot.isDragging) return {}; if (!snapshot.isDropAnimating) { return style; } return { ...style, transitionDuration: `0.001s`, }; } const isNotAllowed = userAuth.isGuest || userAuth.isViewer; return ( {(provided, snapshot) => (
{!isNotAllowed && (
)} {properties.key && (
{issue.project_detail.identifier}-{issue.sequence_id}
)}
{issue.name}
{properties.priority && ( )} {properties.state && ( )} {properties.due_date && ( )} {properties.sub_issue_count && (
{issue.sub_issues_count}{" "} {issue.sub_issues_count === 1 ? "sub-issue" : "sub-issues"}
)} {properties.assignee && ( )}
)}
); };