import React, { useState } from "react"; import { observer } from "mobx-react"; import { useParams } from "next/navigation"; import { SubmitHandler, useForm } from "react-hook-form"; import useSWR from "swr"; import { Search } from "lucide-react"; import { Combobox, Dialog, Transition } from "@headlessui/react"; // services import { IUser, TIssue } from "@plane/types"; import { Button, TOAST_TYPE, setToast } from "@plane/ui"; import { EmptyState } from "@/components/empty-state"; import { EmptyStateType } from "@/constants/empty-state"; import { PROJECT_ISSUES_LIST } from "@/constants/fetch-keys"; import { EIssuesStoreType } from "@/constants/issue"; import { useIssues, useProject } from "@/hooks/store"; import { IssueService } from "@/services/issue"; // ui // icons // types // store hooks // components import { BulkDeleteIssuesModalItem } from "./bulk-delete-issues-modal-item"; // constants type FormInput = { delete_issue_ids: string[]; }; type Props = { isOpen: boolean; onClose: () => void; user: IUser | undefined; }; const issueService = new IssueService(); export const BulkDeleteIssuesModal: React.FC = observer((props) => { const { isOpen, onClose } = props; // router params const { workspaceSlug, projectId } = useParams(); // hooks const { getProjectById } = useProject(); const { issues: { removeBulkIssues }, } = useIssues(EIssuesStoreType.PROJECT); // states const [query, setQuery] = useState(""); // fetching project issues. const { data: issues } = useSWR( workspaceSlug && projectId && isOpen ? PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string) : null, workspaceSlug && projectId && isOpen ? () => issueService.getIssues(workspaceSlug as string, projectId as string) : null ); const { handleSubmit, watch, reset, setValue, formState: { isSubmitting }, } = useForm({ defaultValues: { delete_issue_ids: [], }, }); const handleClose = () => { setQuery(""); reset(); onClose(); }; const handleDelete: SubmitHandler = async (data) => { if (!workspaceSlug || !projectId) return; if (!data.delete_issue_ids || data.delete_issue_ids.length === 0) { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Please select at least one issue.", }); return; } if (!Array.isArray(data.delete_issue_ids)) data.delete_issue_ids = [data.delete_issue_ids]; await removeBulkIssues(workspaceSlug as string, projectId as string, data.delete_issue_ids) .then(() => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Success!", message: "Issues deleted successfully!", }); handleClose(); }) .catch(() => setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "Something went wrong. Please try again.", }) ); }; const projectDetails = getProjectById(projectId as string); const filteredIssues: TIssue[] = query === "" ? Object.values(issues ?? {}) : Object.values(issues ?? {})?.filter( (issue) => issue.name.toLowerCase().includes(query.toLowerCase()) || `${projectDetails?.identifier}-${issue.sequence_id}`.toLowerCase().includes(query.toLowerCase()) ) ?? []; return ( setQuery("")} appear>
{ const selectedIssues = watch("delete_issue_ids"); if (selectedIssues.includes(val)) setValue( "delete_issue_ids", selectedIssues.filter((i) => i !== val) ); else setValue("delete_issue_ids", [...selectedIssues, val]); }} >
{filteredIssues.length > 0 ? (
  • {query === "" && (

    Select issues to delete

    )}
      {filteredIssues.map((issue) => ( ))}
  • ) : (
    )}
    {filteredIssues.length > 0 && (
    )}
    ); });