import React from "react"; // swr import useSWR, { mutate } from "swr"; // react hook form import { useForm, Controller } from "react-hook-form"; // headless import { Dialog, Transition, Listbox } from "@headlessui/react"; // hooks import useUser from "lib/hooks/useUser"; import useToast from "lib/hooks/useToast"; // services import projectService from "lib/services/project.service"; import workspaceService from "lib/services/workspace.service"; // constants import { ROLE } from "constants/"; import { PROJECT_INVITATIONS, WORKSPACE_MEMBERS } from "constants/fetch-keys"; // ui import { Button, Select, TextArea } from "ui"; // icons import { ChevronDownIcon, CheckIcon } from "@heroicons/react/20/solid"; // types import { IProjectMemberInvitation } from "types"; type Props = { isOpen: boolean; setIsOpen: React.Dispatch>; members: any[]; }; type ProjectMember = IProjectMemberInvitation & { member_id: string; user_id: string; }; const defaultValues: Partial = { email: "", message: "", role: 5, member_id: "", user_id: "", }; const SendProjectInvitationModal: React.FC = ({ isOpen, setIsOpen, members }) => { const handleClose = () => { setIsOpen(false); const timeout = setTimeout(() => { reset(defaultValues); clearTimeout(timeout); }, 500); }; const { activeWorkspace, activeProject } = useUser(); const { setToastAlert } = useToast(); const { data: people } = useSWR( activeWorkspace ? WORKSPACE_MEMBERS(activeWorkspace.slug) : null, activeWorkspace ? () => workspaceService.workspaceMembers(activeWorkspace.slug) : null, { onErrorRetry(err, _, __, revalidate, revalidateOpts) { if (err?.status === 403) return; setTimeout(() => revalidate(revalidateOpts), 5000); }, } ); const { register, formState: { errors, isSubmitting }, handleSubmit, reset, setError, setValue, control, } = useForm({ defaultValues, }); const onSubmit = async (formData: ProjectMember) => { if (!activeWorkspace || !activeProject || isSubmitting) return; await projectService .inviteProject(activeWorkspace.slug, activeProject.id, formData) .then((response) => { console.log(response); setIsOpen(false); mutate( PROJECT_INVITATIONS, (prevData: any[]) => { return [{ ...formData, ...response }, ...(prevData ?? [])]; }, false ); setToastAlert({ title: "Success", type: "success", message: "Member added successfully", }); }) .catch((error) => { console.log(error); }); }; return (
Invite Members

Invite members to work on your project.

( { onChange(data.id); setValue("member_id", data.id); setValue("email", data.email); }} > {({ open }) => ( <> Email
{value && value !== "" ? people?.find((p) => p.member.id === value)?.member.email : "Select email"} {people?.map( (person) => !members.some( (m: any) => m.email === person.member.email ) && ( `${ active ? "text-white bg-theme" : "text-gray-900" } cursor-default select-none relative py-2 pl-3 pr-9 text-left` } value={{ id: person.member.id, email: person.member.email, }} > {({ selected, active }) => ( <> {person.member.email} {selected ? ( ) : null} )} ) )}

{errors.user_id && errors.user_id.message}

)}
)} >