import React from "react"; // swr import useSWR from "swr"; // headless ui import { Listbox, Transition } from "@headlessui/react"; // react hook form import { useForm, Controller } from "react-hook-form"; // services import stateServices from "lib/services/state.services"; import issuesServices from "lib/services/issues.services"; import workspaceService from "lib/services/workspace.service"; // hooks import useUser from "lib/hooks/useUser"; // fetching keys import { PROJECT_ISSUES_LIST, STATE_LIST, WORKSPACE_MEMBERS, PROJECT_ISSUE_LABELS, } from "constants/fetch-keys"; // commons import { classNames } from "constants/common"; // ui import { Input, Button } from "ui"; // icons import { Bars3BottomRightIcon, PlusIcon, UserIcon, TagIcon } from "@heroicons/react/24/outline"; // types import type { Control } from "react-hook-form"; import type { IIssue, IIssueLabels, IssueResponse, IState, WorkspaceMember } from "types"; type Props = { control: Control; submitChanges: (formData: Partial) => void; }; const PRIORITIES = ["high", "medium", "low"]; const defaultValues: Partial = { name: "", }; const IssueDetailSidebar: React.FC = ({ control, submitChanges }) => { const { activeWorkspace, activeProject } = useUser(); const { data: states } = useSWR( activeWorkspace && activeProject ? STATE_LIST(activeProject.id) : null, activeWorkspace && activeProject ? () => stateServices.getStates(activeWorkspace.slug, activeProject.id) : null ); const { data: people } = useSWR( activeWorkspace ? WORKSPACE_MEMBERS : null, activeWorkspace ? () => workspaceService.workspaceMembers(activeWorkspace.slug) : null ); const { data: projectIssues } = useSWR( activeProject && activeWorkspace ? PROJECT_ISSUES_LIST(activeWorkspace.slug, activeProject.id) : null, activeProject && activeWorkspace ? () => issuesServices.getIssues(activeWorkspace.slug, activeProject.id) : null ); const { data: issueLabels, mutate: issueLabelMutate } = useSWR( activeProject && activeWorkspace ? PROJECT_ISSUE_LABELS(activeProject.id) : null, activeProject && activeWorkspace ? () => issuesServices.getIssueLabels(activeWorkspace.slug, activeProject.id) : null ); const { register, handleSubmit, formState: { isSubmitting }, reset, } = useForm({ defaultValues, }); const onSubmit = (formData: any) => { if (!activeWorkspace || !activeProject || isSubmitting) return; issuesServices .createIssueLabel(activeWorkspace.slug, activeProject.id, formData) .then((res) => { console.log(res); reset(defaultValues); issueLabelMutate((prevData) => [...(prevData ?? []), res], false); }); }; return (
{[ { label: "Priority", name: "priority", canSelectMultipleOptions: false, icon: Bars3BottomRightIcon, options: PRIORITIES.map((property) => ({ label: property, value: property, })), }, { label: "Status", name: "state", canSelectMultipleOptions: false, icon: Bars3BottomRightIcon, options: states?.map((state) => ({ label: state.name, value: state.id, })), }, { label: "Assignees", name: "assignees_list", canSelectMultipleOptions: true, icon: UserIcon, options: people?.map((person) => ({ label: person.member.first_name, value: person.member.id, })), }, { label: "Blocker", name: "blockers_list", canSelectMultipleOptions: true, icon: UserIcon, options: projectIssues?.results?.map((issue) => ({ label: issue.name, value: issue.id, })), }, { label: "Blocked", name: "blocked_list", canSelectMultipleOptions: true, icon: UserIcon, options: projectIssues?.results?.map((issue) => ({ label: issue.name, value: issue.id, })), }, ].map((item) => (

{item.label}

( submitChanges({ [item.name]: value })} className="flex-shrink-0" > {({ open }) => ( <> {item.label}
{item.options?.map((option) => ( classNames( active || selected ? "bg-indigo-50" : "bg-white", "relative cursor-default select-none py-2 px-3" ) } value={option.value} >
{option.label}
))}
)}
)} />
))}

Label

( submitChanges({ labels_list: value })} className="flex-shrink-0" > {({ open }) => ( <> Label
{issueLabels?.map((label: any) => ( classNames( active || selected ? "bg-indigo-50" : "bg-white", "relative cursor-default select-none py-2 px-3" ) } value={label.id} >
{label.name}
))}
)}
)} />
); }; export default IssueDetailSidebar;