import React, { useEffect } from "react"; import { useRouter } from "next/router"; import useSWR, { mutate } from "swr"; import { useForm, Controller, useFieldArray } from "react-hook-form"; import { Dialog, Transition } from "@headlessui/react"; // ui import { Avatar, CustomSearchSelect, CustomSelect, PrimaryButton, SecondaryButton, } from "components/ui"; //icons import { PlusIcon, XMarkIcon } from "@heroicons/react/24/outline"; import { ChevronDownIcon } from "@heroicons/react/20/solid"; // services import projectService from "services/project.service"; import workspaceService from "services/workspace.service"; // contexts import { useProjectMyMembership } from "contexts/project-member.context"; // hooks import useToast from "hooks/use-toast"; // types import { ICurrentUserResponse } from "types"; // fetch-keys import { PROJECT_MEMBERS, WORKSPACE_MEMBERS } from "constants/fetch-keys"; // constants import { ROLE } from "constants/workspace"; type Props = { isOpen: boolean; setIsOpen: React.Dispatch>; members: any[]; user: ICurrentUserResponse | undefined; }; type member = { role: 5 | 10 | 15 | 20; member_id: string; }; type FormValues = { members: member[]; }; const defaultValues: FormValues = { members: [ { role: 5, member_id: "", }, ], }; const SendProjectInvitationModal: React.FC = ({ isOpen, setIsOpen, members, user }) => { 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.email === person.member.email); 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); mutate(PROJECT_MEMBERS(projectId as string)); setToastAlert({ title: "Success", type: "success", message: "Member added successfully", }); }) .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.email, content: (
{person.member.email}
), })); 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.email}
) : (
Select co-worker’s email
)}
( {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 && ( )}
))}
Cancel {isSubmitting ? `${ fields && fields.length > 1 ? "Adding Members..." : "Adding Member..." }` : `${fields && fields.length > 1 ? "Add Members" : "Add Member"}`}
); }; export default SendProjectInvitationModal;