import React, { useEffect } from "react"; import { useRouter } from "next/router"; import useSWR from "swr"; import { useForm, Controller, useFieldArray } from "react-hook-form"; import { Dialog, Transition } from "@headlessui/react"; // ui import { Button, CustomSelect, CustomSearchSelect } from "@plane/ui"; import { Avatar } from "components/ui"; //icons import { ChevronDown, Plus, X } from "lucide-react"; // services import { ProjectService } from "services/project"; import { WorkspaceService } from "services/workspace.service"; // contexts import { useProjectMyMembership } from "contexts/project-member.context"; // hooks import useToast from "hooks/use-toast"; // types import { IUser } from "types"; // fetch-keys import { WORKSPACE_MEMBERS } from "constants/fetch-keys"; // constants import { ROLE } from "constants/workspace"; type Props = { isOpen: boolean; setIsOpen: React.Dispatch>; members: any[]; user: IUser | undefined; onSuccess: () => void; }; type member = { role: 5 | 10 | 15 | 20; member_id: string; }; type FormValues = { members: member[]; }; const defaultValues: FormValues = { members: [ { role: 5, member_id: "", }, ], }; // services const projectService = new ProjectService(); const workspaceService = new WorkspaceService(); const SendProjectInvitationModal: React.FC = (props) => { const { isOpen, setIsOpen, members, user, onSuccess } = props; const router = useRouter(); const { workspaceSlug, projectId } = router.query; const { setToastAlert } = useToast(); const { memberDetails } = useProjectMyMembership(); const { data: people } = useSWR( workspaceSlug ? WORKSPACE_MEMBERS(workspaceSlug as string) : null, workspaceSlug ? () => workspaceService.workspaceMembers(workspaceSlug as string) : null ); const { formState: { errors, isSubmitting }, reset, handleSubmit, control, } = useForm(); const { fields, append, remove } = useFieldArray({ control, name: "members", }); const uninvitedPeople = people?.filter((person) => { const isInvited = members?.find((member) => member.memberId === person.member.id); return !isInvited; }); const onSubmit = async (formData: FormValues) => { if (!workspaceSlug || !projectId || isSubmitting) return; const payload = { ...formData }; await projectService .inviteProject(workspaceSlug as string, projectId as string, payload, user) .then(() => { setIsOpen(false); setToastAlert({ title: "Success", type: "success", message: "Member added successfully", }); onSuccess(); }) .catch((error) => { console.log(error); }) .finally(() => { reset(defaultValues); }); }; const handleClose = () => { setIsOpen(false); const timeout = setTimeout(() => { reset(defaultValues); clearTimeout(timeout); }, 500); }; const appendField = () => { append({ role: 5, member_id: "", }); }; useEffect(() => { if (fields.length === 0) { append([ { role: 5, member_id: "", }, ]); } }, [fields, append]); const options = uninvitedPeople?.map((person) => ({ value: person.member.id, query: person.member.display_name, content: (
{person.member.display_name} ({person.member.first_name + " " + person.member.last_name})
), })); return (
Invite Members

Invite members to work on your project.

{fields.map((field, index) => (
( {value && value !== "" ? (
p.member.id === value)?.member} /> {people?.find((p) => p.member.id === value)?.member.display_name}
) : (
Select co-worker
)}
( {field.value ? ROLE[field.value] : "Select role"}
} input width="w-full" > {Object.entries(ROLE).map(([key, label]) => { if (parseInt(key) > (memberDetails?.role ?? 5)) return null; return ( {label} ); })} )} /> {errors.members && errors.members[index]?.role && ( {errors.members[index]?.role?.message} )}
{fields.length > 1 && ( )}
))}
); }; export default SendProjectInvitationModal;