forked from github/plane
feat: bulk invite for project (#1466)
* feat: bulk invite for project * feat: members dropdown updated * fix: error message added ,style: ui improvement * feat: added add members button for scenarios with multiple members * chore: updated watch to fields
This commit is contained in:
parent
49f37e0346
commit
353c85120f
@ -1,14 +1,23 @@
|
|||||||
import React from "react";
|
import React, { useEffect } from "react";
|
||||||
|
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
|
|
||||||
import useSWR, { mutate } from "swr";
|
import useSWR, { mutate } from "swr";
|
||||||
|
|
||||||
import { useForm, Controller } from "react-hook-form";
|
import { useForm, Controller, useFieldArray } from "react-hook-form";
|
||||||
|
|
||||||
import { Dialog, Transition } from "@headlessui/react";
|
import { Dialog, Transition } from "@headlessui/react";
|
||||||
// ui
|
// ui
|
||||||
import { CustomSelect, PrimaryButton, SecondaryButton, TextArea } from "components/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
|
// services
|
||||||
import projectService from "services/project.service";
|
import projectService from "services/project.service";
|
||||||
import workspaceService from "services/workspace.service";
|
import workspaceService from "services/workspace.service";
|
||||||
@ -17,9 +26,9 @@ import { useProjectMyMembership } from "contexts/project-member.context";
|
|||||||
// hooks
|
// hooks
|
||||||
import useToast from "hooks/use-toast";
|
import useToast from "hooks/use-toast";
|
||||||
// types
|
// types
|
||||||
import { ICurrentUserResponse, IProjectMemberInvitation } from "types";
|
import { ICurrentUserResponse } from "types";
|
||||||
// fetch-keys
|
// fetch-keys
|
||||||
import { PROJECT_INVITATIONS, WORKSPACE_MEMBERS } from "constants/fetch-keys";
|
import { PROJECT_MEMBERS, WORKSPACE_MEMBERS } from "constants/fetch-keys";
|
||||||
// constants
|
// constants
|
||||||
import { ROLE } from "constants/workspace";
|
import { ROLE } from "constants/workspace";
|
||||||
|
|
||||||
@ -30,17 +39,22 @@ type Props = {
|
|||||||
user: ICurrentUserResponse | undefined;
|
user: ICurrentUserResponse | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ProjectMember = IProjectMemberInvitation & {
|
type member = {
|
||||||
|
role: 5 | 10 | 15 | 20;
|
||||||
member_id: string;
|
member_id: string;
|
||||||
user_id: string;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultValues: Partial<ProjectMember> = {
|
type FormValues = {
|
||||||
email: "",
|
members: member[];
|
||||||
message: "",
|
};
|
||||||
role: 5,
|
|
||||||
member_id: "",
|
const defaultValues: FormValues = {
|
||||||
user_id: "",
|
members: [
|
||||||
|
{
|
||||||
|
role: 5,
|
||||||
|
member_id: "",
|
||||||
|
},
|
||||||
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
const SendProjectInvitationModal: React.FC<Props> = ({ isOpen, setIsOpen, members, user }) => {
|
const SendProjectInvitationModal: React.FC<Props> = ({ isOpen, setIsOpen, members, user }) => {
|
||||||
@ -56,14 +70,16 @@ const SendProjectInvitationModal: React.FC<Props> = ({ isOpen, setIsOpen, member
|
|||||||
);
|
);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
register,
|
|
||||||
formState: { errors, isSubmitting },
|
formState: { errors, isSubmitting },
|
||||||
handleSubmit,
|
|
||||||
reset,
|
reset,
|
||||||
setValue,
|
handleSubmit,
|
||||||
control,
|
control,
|
||||||
} = useForm<ProjectMember>({
|
} = useForm<FormValues>();
|
||||||
defaultValues,
|
|
||||||
|
const { fields, append, remove } = useFieldArray({
|
||||||
|
control,
|
||||||
|
name: "members",
|
||||||
});
|
});
|
||||||
|
|
||||||
const uninvitedPeople = people?.filter((person) => {
|
const uninvitedPeople = people?.filter((person) => {
|
||||||
@ -71,20 +87,14 @@ const SendProjectInvitationModal: React.FC<Props> = ({ isOpen, setIsOpen, member
|
|||||||
return !isInvited;
|
return !isInvited;
|
||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit = async (formData: ProjectMember) => {
|
const onSubmit = async (formData: FormValues) => {
|
||||||
if (!workspaceSlug || !projectId || isSubmitting) return;
|
if (!workspaceSlug || !projectId || isSubmitting) return;
|
||||||
|
const payload = { ...formData };
|
||||||
await projectService
|
await projectService
|
||||||
.inviteProject(workspaceSlug as string, projectId as string, formData, user)
|
.inviteProject(workspaceSlug as string, projectId as string, payload, user)
|
||||||
.then((response) => {
|
.then(() => {
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
mutate<any[]>(
|
mutate(PROJECT_MEMBERS(projectId as string));
|
||||||
PROJECT_INVITATIONS,
|
|
||||||
(prevData) => {
|
|
||||||
if (!prevData) return prevData;
|
|
||||||
return [{ ...formData, ...response }, ...(prevData ?? [])];
|
|
||||||
},
|
|
||||||
false
|
|
||||||
);
|
|
||||||
setToastAlert({
|
setToastAlert({
|
||||||
title: "Success",
|
title: "Success",
|
||||||
type: "success",
|
type: "success",
|
||||||
@ -93,6 +103,9 @@ const SendProjectInvitationModal: React.FC<Props> = ({ isOpen, setIsOpen, member
|
|||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
reset(defaultValues);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -104,6 +117,35 @@ const SendProjectInvitationModal: React.FC<Props> = ({ isOpen, setIsOpen, member
|
|||||||
}, 500);
|
}, 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: (
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<Avatar user={person.member} />
|
||||||
|
{person.member.email}
|
||||||
|
</div>
|
||||||
|
),
|
||||||
|
}));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Transition.Root show={isOpen} as={React.Fragment}>
|
<Transition.Root show={isOpen} as={React.Fragment}>
|
||||||
<Dialog as="div" className="relative z-20" onClose={handleClose}>
|
<Dialog as="div" className="relative z-20" onClose={handleClose}>
|
||||||
@ -116,11 +158,11 @@ const SendProjectInvitationModal: React.FC<Props> = ({ isOpen, setIsOpen, member
|
|||||||
leaveFrom="opacity-100"
|
leaveFrom="opacity-100"
|
||||||
leaveTo="opacity-0"
|
leaveTo="opacity-0"
|
||||||
>
|
>
|
||||||
<div className="fixed inset-0 bg-[#131313] bg-opacity-50 transition-opacity" />
|
<div className="fixed inset-0 bg-brand-backdrop bg-opacity-50 transition-opacity" />
|
||||||
</Transition.Child>
|
</Transition.Child>
|
||||||
|
|
||||||
<div className="fixed inset-0 z-20 overflow-y-auto">
|
<div className="fixed inset-0 z-20 overflow-y-auto">
|
||||||
<div className="flex min-h-full items-center justify-center p-4 text-center sm:p-0">
|
<div className="flex items-center justify-center min-h-full p-4 text-center">
|
||||||
<Transition.Child
|
<Transition.Child
|
||||||
as={React.Fragment}
|
as={React.Fragment}
|
||||||
enter="ease-out duration-300"
|
enter="ease-out duration-300"
|
||||||
@ -130,111 +172,138 @@ const SendProjectInvitationModal: React.FC<Props> = ({ isOpen, setIsOpen, member
|
|||||||
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
|
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
|
||||||
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
|
||||||
>
|
>
|
||||||
<Dialog.Panel className="relative transform overflow-hidden rounded-lg bg-brand-surface-2 p-5 text-left shadow-xl transition-all sm:w-full sm:max-w-2xl">
|
<Dialog.Panel className="relative transform rounded-lg border border-brand-base bg-brand-base p-5 text-left shadow-xl transition-all sm:w-full sm:max-w-2xl">
|
||||||
<form onSubmit={handleSubmit(onSubmit)}>
|
<form onSubmit={handleSubmit(onSubmit)}>
|
||||||
<div className="space-y-5">
|
<div className="space-y-5 mb-5">
|
||||||
<Dialog.Title as="h3" className="text-lg font-medium leading-6 text-brand-base">
|
<Dialog.Title as="h3" className="text-lg font-medium leading-6 text-brand-base">
|
||||||
Invite Members
|
Invite Members
|
||||||
</Dialog.Title>
|
</Dialog.Title>
|
||||||
<div className="mt-2">
|
</div>
|
||||||
<p className="text-sm text-brand-secondary">
|
|
||||||
Invite members to work on your project.
|
<div className="text-sm">
|
||||||
</p>
|
<div className="grid grid-cols-12 gap-x-4 mb-3 text-sm">
|
||||||
|
<h6 className="col-span-7 px-1">Email</h6>
|
||||||
|
<h6 className="col-span-4 px-1">Role</h6>
|
||||||
</div>
|
</div>
|
||||||
<div className="space-y-3">
|
|
||||||
<div>
|
|
||||||
<Controller
|
|
||||||
control={control}
|
|
||||||
name="user_id"
|
|
||||||
rules={{ required: "Please select a member" }}
|
|
||||||
render={({ field: { value, onChange } }) => (
|
|
||||||
<CustomSelect
|
|
||||||
value={value}
|
|
||||||
label={
|
|
||||||
<div
|
|
||||||
className={`${errors.user_id ? "border-red-500 bg-red-50" : ""}`}
|
|
||||||
>
|
|
||||||
{value && value !== ""
|
|
||||||
? people?.find((p) => p.member.id === value)?.member.email
|
|
||||||
: "Select email"}
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
onChange={(val: string) => {
|
|
||||||
onChange(val);
|
|
||||||
const person = uninvitedPeople?.find((p) => p.member.id === val);
|
|
||||||
|
|
||||||
setValue("member_id", val);
|
<div className="space-y-4 mb-3">
|
||||||
setValue("email", person?.member.email ?? "");
|
{fields.map((field, index) => (
|
||||||
}}
|
<div
|
||||||
input
|
key={field.id}
|
||||||
width="w-full"
|
className="group grid grid-cols-12 gap-x-4 mb-1 text-sm items-start"
|
||||||
>
|
>
|
||||||
{uninvitedPeople && uninvitedPeople.length > 0 ? (
|
<div className="flex flex-col gap-1 col-span-7">
|
||||||
<>
|
<Controller
|
||||||
{uninvitedPeople?.map((person) => (
|
control={control}
|
||||||
<CustomSelect.Option
|
name={`members.${index}.member_id`}
|
||||||
key={person.member.id}
|
rules={{ required: "Please select a member" }}
|
||||||
value={person.member.id}
|
render={({ field: { value, onChange } }) => (
|
||||||
>
|
<CustomSearchSelect
|
||||||
{person.member.email}
|
value={value}
|
||||||
</CustomSelect.Option>
|
customButton={
|
||||||
))}
|
<button className="flex w-full items-center justify-between gap-1 rounded-md border border-brand-base shadow-sm duration-300 text-brand-secondary hover:text-brand-base hover:bg-brand-surface-2 focus:outline-none px-3 py-2 text-sm text-left">
|
||||||
</>
|
{value && value !== "" ? (
|
||||||
) : (
|
<div className="flex items-center gap-2">
|
||||||
<div className="text-center text-sm py-5">
|
<Avatar
|
||||||
Invite members to workspace before adding them to a project.
|
user={
|
||||||
</div>
|
people?.find((p) => p.member.id === value)?.member
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
{people?.find((p) => p.member.id === value)?.member.email}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div>Select co-worker’s email</div>
|
||||||
|
)}
|
||||||
|
<ChevronDownIcon className="h-3 w-3" aria-hidden="true" />
|
||||||
|
</button>
|
||||||
|
}
|
||||||
|
onChange={(val: string) => {
|
||||||
|
onChange(val);
|
||||||
|
}}
|
||||||
|
options={options}
|
||||||
|
position="left"
|
||||||
|
dropdownWidth="w-full min-w-[12rem]"
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</CustomSelect>
|
/>
|
||||||
)}
|
{errors.members && errors.members[index]?.member_id && (
|
||||||
/>
|
<span className="text-sm px-1 text-red-500">
|
||||||
</div>
|
{errors.members[index]?.member_id?.message}
|
||||||
<div>
|
</span>
|
||||||
<h6 className="text-brand-secondary">Role</h6>
|
)}
|
||||||
<Controller
|
</div>
|
||||||
name="role"
|
|
||||||
control={control}
|
|
||||||
render={({ field }) => (
|
|
||||||
<CustomSelect
|
|
||||||
{...field}
|
|
||||||
label={
|
|
||||||
<span className="capitalize">
|
|
||||||
{field.value ? ROLE[field.value] : "Select role"}
|
|
||||||
</span>
|
|
||||||
}
|
|
||||||
input
|
|
||||||
width="w-full"
|
|
||||||
>
|
|
||||||
{Object.entries(ROLE).map(([key, label]) => {
|
|
||||||
if (parseInt(key) > (memberDetails?.role ?? 5)) return null;
|
|
||||||
|
|
||||||
return (
|
<div className="flex items-center justify-between gap-2 col-span-5">
|
||||||
<CustomSelect.Option key={key} value={key}>
|
<div className="flex flex-col gap-1 w-full">
|
||||||
{label}
|
<Controller
|
||||||
</CustomSelect.Option>
|
name={`members.${index}.role`}
|
||||||
);
|
control={control}
|
||||||
})}
|
rules={{ required: "Select Role" }}
|
||||||
</CustomSelect>
|
render={({ field }) => (
|
||||||
)}
|
<CustomSelect
|
||||||
/>
|
{...field}
|
||||||
</div>
|
label={
|
||||||
<div>
|
<span className="capitalize">
|
||||||
<TextArea
|
{field.value ? ROLE[field.value] : "Select role"}
|
||||||
id="message"
|
</span>
|
||||||
name="message"
|
}
|
||||||
label="Message"
|
input
|
||||||
placeholder="Enter message"
|
width="w-full"
|
||||||
error={errors.message}
|
>
|
||||||
register={register}
|
{Object.entries(ROLE).map(([key, label]) => {
|
||||||
/>
|
if (parseInt(key) > (memberDetails?.role ?? 5)) return null;
|
||||||
</div>
|
|
||||||
|
return (
|
||||||
|
<CustomSelect.Option key={key} value={key}>
|
||||||
|
{label}
|
||||||
|
</CustomSelect.Option>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</CustomSelect>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
{errors.members && errors.members[index]?.role && (
|
||||||
|
<span className="text-sm px-1 text-red-500">
|
||||||
|
{errors.members[index]?.role?.message}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-item w-6">
|
||||||
|
{fields.length > 1 && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="self-center place-items-center rounded"
|
||||||
|
onClick={() => remove(index)}
|
||||||
|
>
|
||||||
|
<XMarkIcon className="h-4 w-4 text-brand-secondary" />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-5 flex justify-end gap-2">
|
|
||||||
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
<div className="mt-5 flex items-center justify-between gap-2">
|
||||||
<PrimaryButton type="submit" loading={isSubmitting}>
|
<button
|
||||||
{isSubmitting ? "Sending Invitation..." : "Send Invitation"}
|
type="button"
|
||||||
</PrimaryButton>
|
className="flex items-center gap-2 outline-brand-accent bg-transparent text-brand-accent text-sm font-medium py-2 pr-3"
|
||||||
|
onClick={appendField}
|
||||||
|
>
|
||||||
|
<PlusIcon className="h-4 w-4" />
|
||||||
|
Add more
|
||||||
|
</button>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<SecondaryButton onClick={handleClose}>Cancel</SecondaryButton>
|
||||||
|
<PrimaryButton type="submit" loading={isSubmitting}>
|
||||||
|
{isSubmitting
|
||||||
|
? `${
|
||||||
|
fields && fields.length > 1 ? "Adding Members..." : "Adding Member..."
|
||||||
|
}`
|
||||||
|
: `${fields && fields.length > 1 ? "Add Members" : "Add Member"}`}
|
||||||
|
</PrimaryButton>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</Dialog.Panel>
|
</Dialog.Panel>
|
||||||
|
@ -8,6 +8,7 @@ import type {
|
|||||||
ICurrentUserResponse,
|
ICurrentUserResponse,
|
||||||
IFavoriteProject,
|
IFavoriteProject,
|
||||||
IProject,
|
IProject,
|
||||||
|
IProjectBulkInviteFormData,
|
||||||
IProjectMember,
|
IProjectMember,
|
||||||
IProjectMemberInvitation,
|
IProjectMemberInvitation,
|
||||||
ISearchIssueResponse,
|
ISearchIssueResponse,
|
||||||
@ -102,7 +103,7 @@ class ProjectServices extends APIService {
|
|||||||
async inviteProject(
|
async inviteProject(
|
||||||
workspaceSlug: string,
|
workspaceSlug: string,
|
||||||
projectId: string,
|
projectId: string,
|
||||||
data: any,
|
data: IProjectBulkInviteFormData,
|
||||||
user: ICurrentUserResponse | undefined
|
user: ICurrentUserResponse | undefined
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
return this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/members/add/`, data)
|
return this.post(`/api/workspaces/${workspaceSlug}/projects/${projectId}/members/add/`, data)
|
||||||
|
4
apps/app/types/projects.d.ts
vendored
4
apps/app/types/projects.d.ts
vendored
@ -113,6 +113,10 @@ export interface IProjectMemberInvitation {
|
|||||||
updated_by: string;
|
updated_by: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IProjectBulkInviteFormData {
|
||||||
|
members: { role: 5 | 10 | 15 | 20; member_id: string }[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface IGithubRepository {
|
export interface IGithubRepository {
|
||||||
id: string;
|
id: string;
|
||||||
full_name: string;
|
full_name: string;
|
||||||
|
Loading…
Reference in New Issue
Block a user