// react import React, { useState } from "react"; // next import { useRouter } from "next/router"; // swr import useSWR, { mutate } from "swr"; // react hook form import { SubmitHandler, useForm } from "react-hook-form"; // services import { Combobox, Dialog, Transition } from "@headlessui/react"; import { FolderIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline"; import issuesServices from "services/issues.service"; import projectService from "services/project.service"; // hooks import useToast from "hooks/use-toast"; // headless ui // ui import { Button } from "components/ui"; // icons import { LayerDiagonalIcon } from "components/icons"; // types import { IIssue, IssueResponse } from "types"; // fetch keys import { PROJECT_ISSUES_LIST, PROJECT_DETAILS } from "constants/fetch-keys"; type FormInput = { delete_issue_ids: string[]; }; type Props = { isOpen: boolean; setIsOpen: React.Dispatch>; }; 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 { data: projectDetails } = useSWR( workspaceSlug && projectId ? PROJECT_DETAILS(projectId as string) : null, workspaceSlug && projectId ? () => projectService.getProject(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 { const newToDelete = selectedIssues; newToDelete.push(val); setValue("delete_issue_ids", newToDelete); } }} >
{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" : "" }` } >
      {projectDetails?.identifier}-{issue.sequence_id} {issue.name}
      ))}
  • ) : (

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

    )}
    {filteredIssues.length > 0 && (
    )}
    ); }; export default BulkDeleteIssuesModal;