-
Notifications
You must be signed in to change notification settings - Fork 11.1k
feat: remove seats from booking #25233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dhairyashiil
wants to merge
8
commits into
calcom:main
Choose a base branch
from
dhairyashiil:feat/remove-seats-from-booking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+868
−163
Draft
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e082146
feat: remove seats from booking
dhairyashiil 8ee47bb
refactor code
dhairyashiil b8eeca7
address cubics commets
dhairyashiil b84ee60
use some_attendees terminology instead of count
dhairyashiil facde41
Merge remote-tracking branch 'upstream/main' into feat/remove-seats-f…
dhairyashiil bdb9f73
Merge remote-tracking branch 'upstream/main' into feat/remove-seats-f…
dhairyashiil 2a11242
fix failing tests
dhairyashiil cec162e
address cubics comments
dhairyashiil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
204 changes: 204 additions & 0 deletions
204
apps/web/components/booking/RemoveBookingSeatsDialog.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| import { useState } from "react"; | ||
| import { useForm } from "react-hook-form"; | ||
|
|
||
| import { useLocale } from "@calcom/lib/hooks/useLocale"; | ||
| import { trpc } from "@calcom/trpc/react"; | ||
| import { Button } from "@calcom/ui/components/button"; | ||
| import { Dialog, DialogContent, DialogFooter, DialogClose } from "@calcom/ui/components/dialog"; | ||
| import { Form, TextAreaField, MultiSelectCheckbox } from "@calcom/ui/components/form"; | ||
| import type { MultiSelectCheckboxesOptionType as Option } from "@calcom/ui/components/form"; | ||
| import { showToast } from "@calcom/ui/components/toast"; | ||
|
|
||
| import type { BookingItemProps } from "./types"; | ||
|
|
||
| interface RemoveBookingSeatsDialogProps { | ||
| booking: BookingItemProps; | ||
| isOpen: boolean; | ||
| onClose: () => void; | ||
| onSuccess: () => void; | ||
| } | ||
|
|
||
| interface FormValues { | ||
| cancellationReason: string; | ||
| } | ||
|
|
||
| export function RemoveBookingSeatsDialog({ | ||
| booking, | ||
| isOpen, | ||
| onClose, | ||
| onSuccess, | ||
| }: RemoveBookingSeatsDialogProps) { | ||
| const { t } = useLocale(); | ||
| const utils = trpc.useUtils(); | ||
| const [selected, setSelected] = useState<Option[]>([]); | ||
| const [loading, setLoading] = useState(false); | ||
|
|
||
| const form = useForm<FormValues>({ | ||
| defaultValues: { | ||
| cancellationReason: "", | ||
| }, | ||
| }); | ||
|
|
||
| const userEmail = booking.loggedInUser?.userEmail; | ||
| const isUserOrganizer = userEmail === booking.user?.email; | ||
|
|
||
| const userSeat = userEmail | ||
| ? booking.seatsReferences?.find((seat) => seat.attendee?.email === userEmail) | ||
| : null; | ||
| const isJustAttendee = !!userSeat && !isUserOrganizer; | ||
|
|
||
| const allSeatOptions = (booking.seatsReferences || []) | ||
| .map((seatRef) => { | ||
| if (!seatRef?.referenceUid) return null; | ||
|
|
||
| const attendee = seatRef.attendee; | ||
| const attendeeName = attendee?.name; | ||
| const attendeeEmail = attendee?.email; | ||
|
|
||
| if (isJustAttendee) { | ||
| if (seatRef.referenceUid !== userSeat?.referenceUid) { | ||
| return null; | ||
| } | ||
| if (attendeeName && attendeeEmail) { | ||
| return { | ||
| value: seatRef.referenceUid, | ||
| label: `${attendeeName} (${attendeeEmail})`, | ||
| }; | ||
| } else if (attendeeEmail) { | ||
| return { | ||
| value: seatRef.referenceUid, | ||
| label: attendeeEmail, | ||
| }; | ||
| } else if (attendeeName) { | ||
| return { | ||
| value: seatRef.referenceUid, | ||
| label: attendeeName, | ||
| }; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| let label: string; | ||
| if (attendeeName && attendeeEmail) { | ||
| label = `${attendeeName} (${attendeeEmail})`; | ||
| } else if (attendeeEmail) { | ||
| label = attendeeEmail; | ||
| } else if (attendeeName) { | ||
| label = attendeeName; | ||
| } else { | ||
| label = `Seat ${seatRef.referenceUid.slice(0, 8)}...`; | ||
dhairyashiil marked this conversation as resolved.
Show resolved
Hide resolved
dhairyashiil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return { | ||
| value: seatRef.referenceUid, | ||
| label: label, | ||
| }; | ||
| }) | ||
| .filter(Boolean) as Option[]; | ||
|
|
||
| const seatOptions = allSeatOptions; | ||
|
|
||
| const onSubmit = async (data: FormValues) => { | ||
| if (selected.length === 0) { | ||
| showToast(t("please_select_at_least_one_seat"), "error"); | ||
| return; | ||
| } | ||
|
|
||
| setLoading(true); | ||
|
|
||
| try { | ||
| const seatReferenceUids = selected.map((option) => option.value); | ||
|
|
||
| const response = await fetch("/api/csrf?sameSite=none", { cache: "no-store" }); | ||
| const { csrfToken } = await response.json(); | ||
|
|
||
| const res = await fetch("/api/cancel", { | ||
| body: JSON.stringify({ | ||
| uid: booking.uid, | ||
| seatReferenceUids: seatReferenceUids, | ||
| cancellationReason: data.cancellationReason, | ||
| csrfToken, | ||
| }), | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| method: "POST", | ||
| }); | ||
|
|
||
| if (res.status >= 200 && res.status < 300) { | ||
| showToast(t("seats_removed_successfully"), "success"); | ||
| await utils.viewer.bookings.invalidate(); | ||
| onSuccess(); | ||
| onClose(); | ||
| form.reset(); | ||
| setSelected([]); | ||
| } else { | ||
| let errorMessage = t("error_removing_seats"); | ||
| try { | ||
| const responseText = await res.text(); | ||
| try { | ||
| const error = JSON.parse(responseText); | ||
| errorMessage = error.message || errorMessage; | ||
| } catch { | ||
| console.error("Failed to parse error response as JSON. Raw response:", responseText); | ||
| errorMessage = responseText.trim() || errorMessage; | ||
| } | ||
| } catch { | ||
| console.error("Failed to read error response. Status:", res.status); | ||
| } | ||
| showToast(errorMessage, "error"); | ||
| } | ||
| } catch { | ||
| showToast(t("error_removing_seats"), "error"); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog open={isOpen} onOpenChange={onClose}> | ||
| <DialogContent | ||
| title={t("remove_seats")} | ||
| description={t("remove_seats_description")} | ||
| type="creation" | ||
| className="max-w-lg"> | ||
| <Form form={form} handleSubmit={onSubmit}> | ||
| <div className="space-y-4"> | ||
| <div> | ||
| <label className="text-default mb-2 block text-sm font-medium"> | ||
| {t("select_seats_to_remove")} | ||
| </label> | ||
| {seatOptions.length === 0 ? ( | ||
| <div className="text-muted text-sm">{t("no_seats_available_to_remove")}</div> | ||
| ) : ( | ||
| <MultiSelectCheckbox | ||
| options={seatOptions} | ||
| selected={selected} | ||
| setSelected={setSelected} | ||
| setValue={(options) => { | ||
| setSelected(options); | ||
| }} | ||
| countText="count_selected" | ||
| className="w-full text-sm" | ||
| /> | ||
| )} | ||
| </div> | ||
|
|
||
| <TextAreaField | ||
| label={t("removal_reason_optional")} | ||
| placeholder={t("removal_reason_placeholder")} | ||
| {...form.register("cancellationReason")} | ||
| /> | ||
| </div> | ||
|
|
||
| <DialogFooter> | ||
| <DialogClose /> | ||
| <Button type="submit" loading={loading} disabled={loading || selected.length === 0}> | ||
| {t("remove_selected_seats")} | ||
| </Button> | ||
| </DialogFooter> | ||
| </Form> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.