Files
calendar/packages/features/users/components/UserTable/BulkActions/TeamList.tsx
T
+28 10ffd9bacd feat: Org user table - bulk actions (#10504)
Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
Co-authored-by: Leo Giovanetti <hello@leog.me>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
Co-authored-by: CarinaWolli <wollencarina@gmail.com>
Co-authored-by: zomars <zomars@me.com>
Co-authored-by: Peer Richelsen <peeroke@gmail.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
Co-authored-by: Udit Takkar <udit.07814802719@cse.mait.ac.in>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Peer Richelsen <peer@cal.com>
Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>
Co-authored-by: gitstart-calcom <gitstart@users.noreply.github.com>
Co-authored-by: Shivam Kalra <shivamkalra98@gmail.com>
Co-authored-by: cherish2003 <saicherissh90@gmail.com>
Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com>
Co-authored-by: rkreddy99 <rreddy@e2clouds.com>
Co-authored-by: varun thummar <Varun>
Co-authored-by: Crowdin Bot <support+bot@crowdin.com>
Co-authored-by: Pradumn Kumar <47187878+Pradumn27@users.noreply.github.com>
Co-authored-by: Richard Poelderl <richard.poelderl@gmail.com>
Co-authored-by: mohammed hussam <hussamkhatib20@gmail.com>
Co-authored-by: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com>
Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
Co-authored-by: nicktrn <55853254+nicktrn@users.noreply.github.com>
Co-authored-by: sydwardrae <94979838+sydwardrae@users.noreply.github.com>
Co-authored-by: Janakiram Yellapu <jyellapu@vmware.com>
Co-authored-by: GitStart-Cal.com <121884634+gitstart-calcom@users.noreply.github.com>
Co-authored-by: sajanlamsal <saznlamsal@gmail.com>
Co-authored-by: Cherish <88829894+cherish2003@users.noreply.github.com>
Co-authored-by: Danila <daniil.demidovich@gmail.com>
Co-authored-by: Neel Patel <29038590+N-NeelPatel@users.noreply.github.com>
Co-authored-by: Rama Krishna Reddy <49095575+rkreddy99@users.noreply.github.com>
Co-authored-by: Varun Thummar <110765105+VARUN949@users.noreply.github.com>
Co-authored-by: Bhargav <bhargavtenali@gmail.com>
Co-authored-by: Pratik Kumar <kpratik1929@gmail.com>
Co-authored-by: Ritesh Patil <riteshsp2000@gmail.com>
2023-08-15 14:07:38 -07:00

127 lines
4.1 KiB
TypeScript

import type { Table } from "@tanstack/react-table";
import { Users, Check } from "lucide-react";
import { useState } from "react";
import classNames from "@calcom/lib/classNames";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { trpc } from "@calcom/trpc";
import {
Command,
CommandInput,
CommandList,
CommandItem,
CommandEmpty,
CommandGroup,
Button,
Popover,
PopoverContent,
PopoverTrigger,
showToast,
} from "@calcom/ui";
import type { User } from "../UserListTable";
interface Props {
table: Table<User>;
}
export function TeamListBulkAction({ table }: Props) {
const { data: teams } = trpc.viewer.organizations.getTeams.useQuery();
const [selectedValues, setSelectedValues] = useState<Set<number>>(new Set());
const utils = trpc.useContext();
const mutation = trpc.viewer.organizations.bulkAddToTeams.useMutation({
onError: (error) => {
showToast(error.message, "error");
},
onSuccess: (res) => {
showToast(
`${res.invitedTotalUsers} Users invited to ${Array.from(selectedValues).length} teams`,
"success"
);
// Optimistically update the data from query trpc cache listMembers
// We may need to set this data instread of invalidating. Will see how performance handles it
utils.viewer.organizations.listMembers.invalidate();
// Clear the selected values
setSelectedValues(new Set());
table.toggleAllRowsSelected(false);
},
});
const { t } = useLocale();
// Add a value to the set
const addValue = (value: number) => {
const updatedSet = new Set(selectedValues);
updatedSet.add(value);
setSelectedValues(updatedSet);
};
// Remove a value from the set
const removeValue = (value: number) => {
const updatedSet = new Set(selectedValues);
updatedSet.delete(value);
setSelectedValues(updatedSet);
};
return (
<>
<Popover>
<PopoverTrigger asChild>
<Button StartIcon={Users}>{t("add_to_team")}</Button>
</PopoverTrigger>
{/* We dont really use shadows much - but its needed here */}
<PopoverContent className="w-[200px] p-0 shadow-md" align="start" sideOffset={12}>
<Command>
<CommandInput placeholder={t("search")} />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
<CommandGroup>
{teams &&
teams.map((option) => {
const isSelected = selectedValues.has(option.id);
return (
<CommandItem
key={option.id}
onSelect={() => {
if (!isSelected) {
addValue(option.id);
} else {
removeValue(option.id);
}
}}>
<span>{option.name}</span>
<div
className={classNames(
"border-subtle ml-auto flex h-4 w-4 items-center justify-center rounded-sm border",
isSelected ? "text-emphasis" : "opacity-50 [&_svg]:invisible"
)}>
<Check className={classNames("h-4 w-4")} />
</div>
</CommandItem>
);
})}
</CommandGroup>
</CommandList>
</Command>
<div className="my-1.5 flex w-full">
<Button
loading={mutation.isLoading}
className="ml-auto mr-1.5 rounded-md"
size="sm"
onClick={async () => {
const selectedRows = table.getSelectedRowModel().flatRows.map((row) => row.original);
mutation.mutateAsync({
userIds: selectedRows.map((row) => row.id),
teamIds: Array.from(selectedValues),
});
}}>
{t("apply")}
</Button>
</div>
</PopoverContent>
</Popover>
</>
);
}