|
| 1 | +import React, { useState } from "react"; |
| 2 | +// swr |
| 3 | +import { mutate } from "swr"; |
| 4 | +// react hook form |
| 5 | +import { useForm } from "react-hook-form"; |
| 6 | +// headless ui |
| 7 | +import { Combobox, Dialog, Transition } from "@headlessui/react"; |
| 8 | +// hooks |
| 9 | +import useUser from "lib/hooks/useUser"; |
| 10 | +// icons |
| 11 | +import { MagnifyingGlassIcon } from "@heroicons/react/20/solid"; |
| 12 | +import { FolderIcon } from "@heroicons/react/24/outline"; |
| 13 | +// commons |
| 14 | +import { classNames } from "constants/common"; |
| 15 | +// types |
| 16 | +import { IIssue, IssueResponse } from "types"; |
| 17 | +import { Button } from "ui"; |
| 18 | +import { PROJECT_ISSUES_DETAILS, PROJECT_ISSUES_LIST } from "constants/fetch-keys"; |
| 19 | +import issuesServices from "lib/services/issues.services"; |
| 20 | + |
| 21 | +type Props = { |
| 22 | + isOpen: boolean; |
| 23 | + setIsOpen: React.Dispatch<React.SetStateAction<boolean>>; |
| 24 | + parentId: string; |
| 25 | +}; |
| 26 | + |
| 27 | +type FormInput = { |
| 28 | + issue_ids: string[]; |
| 29 | + cycleId: string; |
| 30 | +}; |
| 31 | + |
| 32 | +const AddAsSubIssue: React.FC<Props> = ({ isOpen, setIsOpen, parentId }) => { |
| 33 | + const [query, setQuery] = useState(""); |
| 34 | + |
| 35 | + const { activeWorkspace, activeProject, issues } = useUser(); |
| 36 | + |
| 37 | + const filteredIssues: IIssue[] = |
| 38 | + query === "" |
| 39 | + ? issues?.results ?? [] |
| 40 | + : issues?.results.filter((issue) => issue.name.toLowerCase().includes(query.toLowerCase())) ?? |
| 41 | + []; |
| 42 | + |
| 43 | + const { |
| 44 | + register, |
| 45 | + formState: { errors, isSubmitting }, |
| 46 | + handleSubmit, |
| 47 | + control, |
| 48 | + reset, |
| 49 | + setError, |
| 50 | + } = useForm<FormInput>(); |
| 51 | + |
| 52 | + const handleCommandPaletteClose = () => { |
| 53 | + setIsOpen(false); |
| 54 | + setQuery(""); |
| 55 | + reset(); |
| 56 | + }; |
| 57 | + |
| 58 | + const addAsSubIssue = (issueId: string) => { |
| 59 | + if (activeWorkspace && activeProject) { |
| 60 | + issuesServices |
| 61 | + .patchIssue(activeWorkspace.slug, activeProject.id, issueId, { parent: parentId }) |
| 62 | + .then((res) => { |
| 63 | + mutate<IssueResponse>( |
| 64 | + PROJECT_ISSUES_LIST(activeWorkspace.slug, activeProject.id), |
| 65 | + (prevData) => ({ |
| 66 | + ...(prevData as IssueResponse), |
| 67 | + results: (prevData?.results ?? []).map((p) => |
| 68 | + p.id === issueId ? { ...p, ...res } : p |
| 69 | + ), |
| 70 | + }), |
| 71 | + false |
| 72 | + ); |
| 73 | + }) |
| 74 | + .catch((e) => { |
| 75 | + console.log(e); |
| 76 | + }); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <Transition.Root show={isOpen} as={React.Fragment} afterLeave={() => setQuery("")} appear> |
| 83 | + <Dialog as="div" className="relative z-10" onClose={handleCommandPaletteClose}> |
| 84 | + <Transition.Child |
| 85 | + as={React.Fragment} |
| 86 | + enter="ease-out duration-300" |
| 87 | + enterFrom="opacity-0" |
| 88 | + enterTo="opacity-100" |
| 89 | + leave="ease-in duration-200" |
| 90 | + leaveFrom="opacity-100" |
| 91 | + leaveTo="opacity-0" |
| 92 | + > |
| 93 | + <div className="fixed inset-0 bg-gray-500 bg-opacity-25 transition-opacity" /> |
| 94 | + </Transition.Child> |
| 95 | + |
| 96 | + <div className="fixed inset-0 z-10 overflow-y-auto p-4 sm:p-6 md:p-20"> |
| 97 | + <Transition.Child |
| 98 | + as={React.Fragment} |
| 99 | + enter="ease-out duration-300" |
| 100 | + enterFrom="opacity-0 scale-95" |
| 101 | + enterTo="opacity-100 scale-100" |
| 102 | + leave="ease-in duration-200" |
| 103 | + leaveFrom="opacity-100 scale-100" |
| 104 | + leaveTo="opacity-0 scale-95" |
| 105 | + > |
| 106 | + <Dialog.Panel className="relative mx-auto max-w-2xl transform divide-y divide-gray-500 divide-opacity-10 rounded-xl bg-white bg-opacity-80 shadow-2xl ring-1 ring-black ring-opacity-5 backdrop-blur backdrop-filter transition-all"> |
| 107 | + <Combobox |
| 108 | + // onChange={(item: ItemType) => { |
| 109 | + // const { url, onClick } = item; |
| 110 | + // if (url) router.push(url); |
| 111 | + // else if (onClick) onClick(); |
| 112 | + // handleCommandPaletteClose(); |
| 113 | + // }} |
| 114 | + > |
| 115 | + <div className="relative m-1"> |
| 116 | + <MagnifyingGlassIcon |
| 117 | + className="pointer-events-none absolute top-3.5 left-4 h-5 w-5 text-gray-900 text-opacity-40" |
| 118 | + aria-hidden="true" |
| 119 | + /> |
| 120 | + <Combobox.Input |
| 121 | + className="h-12 w-full border-0 bg-transparent pl-11 pr-4 text-gray-900 placeholder-gray-500 focus:ring-0 sm:text-sm outline-none" |
| 122 | + placeholder="Search..." |
| 123 | + onChange={(e) => setQuery(e.target.value)} |
| 124 | + /> |
| 125 | + </div> |
| 126 | + |
| 127 | + <Combobox.Options |
| 128 | + static |
| 129 | + className="max-h-80 scroll-py-2 divide-y divide-gray-500 divide-opacity-10 overflow-y-auto" |
| 130 | + > |
| 131 | + {filteredIssues.length > 0 && ( |
| 132 | + <> |
| 133 | + <li className="p-2"> |
| 134 | + {query === "" && ( |
| 135 | + <h2 className="mt-4 mb-2 px-3 text-xs font-semibold text-gray-900"> |
| 136 | + Issues |
| 137 | + </h2> |
| 138 | + )} |
| 139 | + <ul className="text-sm text-gray-700"> |
| 140 | + {filteredIssues.map((issue) => { |
| 141 | + if (issue.parent === "" || issue.parent === null) |
| 142 | + return ( |
| 143 | + <Combobox.Option |
| 144 | + key={issue.id} |
| 145 | + value={{ |
| 146 | + name: issue.name, |
| 147 | + }} |
| 148 | + className={({ active }) => |
| 149 | + classNames( |
| 150 | + "flex items-center gap-2 cursor-pointer select-none rounded-md px-3 py-2", |
| 151 | + active ? "bg-gray-900 bg-opacity-5 text-gray-900" : "" |
| 152 | + ) |
| 153 | + } |
| 154 | + onClick={() => { |
| 155 | + addAsSubIssue(issue.id); |
| 156 | + setIsOpen(false); |
| 157 | + }} |
| 158 | + > |
| 159 | + <span |
| 160 | + className={`h-1.5 w-1.5 block rounded-full`} |
| 161 | + style={{ |
| 162 | + backgroundColor: issue.state_detail.color, |
| 163 | + }} |
| 164 | + /> |
| 165 | + {issue.name} |
| 166 | + </Combobox.Option> |
| 167 | + ); |
| 168 | + })} |
| 169 | + </ul> |
| 170 | + </li> |
| 171 | + </> |
| 172 | + )} |
| 173 | + </Combobox.Options> |
| 174 | + |
| 175 | + {query !== "" && filteredIssues.length === 0 && ( |
| 176 | + <div className="py-14 px-6 text-center sm:px-14"> |
| 177 | + <FolderIcon |
| 178 | + className="mx-auto h-6 w-6 text-gray-900 text-opacity-40" |
| 179 | + aria-hidden="true" |
| 180 | + /> |
| 181 | + <p className="mt-4 text-sm text-gray-900"> |
| 182 | + We couldn{"'"}t find any issue with that term. Please try again. |
| 183 | + </p> |
| 184 | + </div> |
| 185 | + )} |
| 186 | + </Combobox> |
| 187 | + </Dialog.Panel> |
| 188 | + </Transition.Child> |
| 189 | + </div> |
| 190 | + </Dialog> |
| 191 | + </Transition.Root> |
| 192 | + </> |
| 193 | + ); |
| 194 | +}; |
| 195 | + |
| 196 | +export default AddAsSubIssue; |
0 commit comments