import React, { useState } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; // react hook form import { SubmitHandler, useForm } from "react-hook-form"; // headless ui import { Combobox, Dialog, Transition } from "@headlessui/react"; // services import issuesServices from "services/issues.service"; // hooks import useToast from "hooks/use-toast"; // ui import { Button } from "components/ui"; // icons import { MagnifyingGlassIcon } from "@heroicons/react/24/outline"; import { LayerDiagonalIcon } from "components/icons"; // types import { IIssue, IssueResponse } from "types"; // fetch keys import { PROJECT_ISSUES_LIST } from "constants/fetch-keys"; type FormInput = { delete_issue_ids: string[]; }; type Props = { isOpen: boolean; setIsOpen: React.Dispatch>; }; export const BulkDeleteIssuesModal: React.FC = ({ isOpen, setIsOpen }) => { const [query, setQuery] = useState(""); const router = useRouter(); const { query: { workspaceSlug, projectId }, } = router; const { data: issues } = useSWR( workspaceSlug && projectId ? PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string) : null, workspaceSlug && projectId ? () => issuesServices.getIssues(workspaceSlug as string, projectId as string) : null ); const { setToastAlert } = useToast(); const { handleSubmit, watch, reset, setValue, formState: { isSubmitting }, } = useForm({ defaultValues: { delete_issue_ids: [], }, }); const filteredIssues: IIssue[] = query === "" ? issues?.results ?? [] : issues?.results.filter( (issue) => issue.name.toLowerCase().includes(query.toLowerCase()) || `${issue.project_detail.identifier}-${issue.sequence_id}` .toLowerCase() .includes(query.toLowerCase()) ) ?? []; const handleClose = () => { setIsOpen(false); setQuery(""); reset(); }; const handleDelete: SubmitHandler = async (data) => { if (!data.delete_issue_ids || data.delete_issue_ids.length === 0) { setToastAlert({ title: "Error", type: "error", message: "Please select atleast one issue", }); return; } if (!Array.isArray(data.delete_issue_ids)) data.delete_issue_ids = [data.delete_issue_ids]; if (workspaceSlug && projectId) { await issuesServices .bulkDeleteIssues(workspaceSlug as string, projectId as string, { issue_ids: data.delete_issue_ids, }) .then((res) => { setToastAlert({ title: "Success", type: "success", message: res.message, }); mutate( PROJECT_ISSUES_LIST(workspaceSlug as string, projectId as string), (prevData) => ({ ...(prevData as IssueResponse), count: (prevData?.results ?? []).filter( (p) => !data.delete_issue_ids.some((id) => p.id === id) ).length, results: (prevData?.results ?? []).filter( (p) => !data.delete_issue_ids.some((id) => p.id === id) ), }), false ); handleClose(); }) .catch((e) => { console.log(e); }); } }; 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) => ( `flex cursor-pointer select-none items-center justify-between rounded-md px-3 py-2 ${ active ? "bg-gray-900 bg-opacity-5 text-gray-900" : "" }` } >
      {issue.project_detail.identifier}-{issue.sequence_id} {issue.name}
      ))}
  • ) : (

    No issues found. Create a new issue with{" "}
    C
    .

    )}
    {filteredIssues.length > 0 && (
    )}
    ); };