|
| 1 | +import type { Dispatch, SetStateAction } from "react"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { z } from "zod"; |
| 4 | + |
| 5 | +import { useLocale } from "@calcom/lib/hooks/useLocale"; |
| 6 | +import { trpc } from "@calcom/trpc/react"; |
| 7 | +import { |
| 8 | + Button, |
| 9 | + Dialog, |
| 10 | + DialogContent, |
| 11 | + DialogFooter, |
| 12 | + DialogHeader, |
| 13 | + MultiEmail, |
| 14 | + Icon, |
| 15 | + showToast, |
| 16 | +} from "@calcom/ui"; |
| 17 | + |
| 18 | +interface IAddGuestsDialog { |
| 19 | + isOpenDialog: boolean; |
| 20 | + setIsOpenDialog: Dispatch<SetStateAction<boolean>>; |
| 21 | + bookingId: number; |
| 22 | +} |
| 23 | + |
| 24 | +export const AddGuestsDialog = (props: IAddGuestsDialog) => { |
| 25 | + const { t } = useLocale(); |
| 26 | + const ZAddGuestsInputSchema = z.array(z.string().email()).refine((emails) => { |
| 27 | + const uniqueEmails = new Set(emails); |
| 28 | + return uniqueEmails.size === emails.length; |
| 29 | + }); |
| 30 | + const { isOpenDialog, setIsOpenDialog, bookingId } = props; |
| 31 | + const utils = trpc.useUtils(); |
| 32 | + const [multiEmailValue, setMultiEmailValue] = useState<string[]>([""]); |
| 33 | + const [isInvalidEmail, setIsInvalidEmail] = useState(false); |
| 34 | + |
| 35 | + const addGuestsMutation = trpc.viewer.bookings.addGuests.useMutation({ |
| 36 | + onSuccess: async () => { |
| 37 | + showToast(t("guests_added"), "success"); |
| 38 | + setIsOpenDialog(false); |
| 39 | + setMultiEmailValue([""]); |
| 40 | + utils.viewer.bookings.invalidate(); |
| 41 | + }, |
| 42 | + onError: (err) => { |
| 43 | + const message = `${err.data?.code}: ${t(err.message)}`; |
| 44 | + showToast(message || t("unable_to_add_guests"), "error"); |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + const handleAdd = () => { |
| 49 | + if (multiEmailValue.length === 0) { |
| 50 | + return; |
| 51 | + } |
| 52 | + const validationResult = ZAddGuestsInputSchema.safeParse(multiEmailValue); |
| 53 | + if (validationResult.success) { |
| 54 | + addGuestsMutation.mutate({ bookingId, guests: multiEmailValue }); |
| 55 | + } else { |
| 56 | + setIsInvalidEmail(true); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + return ( |
| 61 | + <Dialog open={isOpenDialog} onOpenChange={setIsOpenDialog}> |
| 62 | + <DialogContent enableOverflow> |
| 63 | + <div className="flex flex-row space-x-3"> |
| 64 | + <div className="bg-subtle flex h-10 w-10 flex-shrink-0 justify-center rounded-full "> |
| 65 | + <Icon name="user-plus" className="m-auto h-6 w-6" /> |
| 66 | + </div> |
| 67 | + <div className="w-full pt-1"> |
| 68 | + <DialogHeader title={t("additional_guests")} /> |
| 69 | + <MultiEmail |
| 70 | + label={t("add_emails")} |
| 71 | + value={multiEmailValue} |
| 72 | + readOnly={false} |
| 73 | + setValue={setMultiEmailValue} |
| 74 | + /> |
| 75 | + |
| 76 | + {isInvalidEmail && ( |
| 77 | + <div className="my-4 flex text-sm text-red-700"> |
| 78 | + <div className="flex-shrink-0"> |
| 79 | + <Icon name="triangle-alert" className="h-5 w-5" /> |
| 80 | + </div> |
| 81 | + <div className="ml-3"> |
| 82 | + <p className="font-medium">{t("emails_must_be_unique_valid")}</p> |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + )} |
| 86 | + |
| 87 | + <DialogFooter> |
| 88 | + <Button |
| 89 | + onClick={() => { |
| 90 | + setMultiEmailValue([""]); |
| 91 | + setIsInvalidEmail(false); |
| 92 | + setIsOpenDialog(false); |
| 93 | + }} |
| 94 | + type="button" |
| 95 | + color="secondary"> |
| 96 | + {t("cancel")} |
| 97 | + </Button> |
| 98 | + <Button data-testid="add_members" loading={addGuestsMutation.isPending} onClick={handleAdd}> |
| 99 | + {t("add")} |
| 100 | + </Button> |
| 101 | + </DialogFooter> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + </DialogContent> |
| 105 | + </Dialog> |
| 106 | + ); |
| 107 | +}; |
0 commit comments