Skip to content

Commit 85e1ed3

Browse files
deepanshurajput0Deepanshu Verma
andauthored
refactor: move cancel logic from inline onClick handler to dedicated function (calcom#27972)
* refactor: move cancel booking logic into handleCancel function * chore: remove unused comments per review feedback --------- Co-authored-by: Deepanshu Verma <deepanshuverma186@email.com>
1 parent 4647333 commit 85e1ed3

1 file changed

Lines changed: 59 additions & 56 deletions

File tree

apps/web/components/booking/CancelBooking.tsx

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const InternalNotePresetsSelect = ({
4646
}
4747
};
4848

49+
4950
return (
5051
<div className="mb-4 flex flex-col">
5152
<Label>{t("internal_booking_note")}</Label>
@@ -177,6 +178,63 @@ export default function CancelBooking(props: Props) {
177178

178179
const isRenderedAsCancelDialog = props.renderContext === "dialog";
179180

181+
const handleCancel =async()=>{
182+
try{
183+
setLoading(true);
184+
185+
const response = await fetch("/api/csrf?sameSite=none", { cache: "no-store" });
186+
const { csrfToken } = await response.json();
187+
188+
const res = await fetch("/api/cancel", {
189+
body: JSON.stringify({
190+
uid: booking?.uid,
191+
cancellationReason: cancellationReason,
192+
allRemainingBookings,
193+
// @NOTE: very important this shouldn't cancel with number ID use uid instead
194+
seatReferenceUid,
195+
cancelledBy: currentUserEmail,
196+
internalNote: internalNote,
197+
csrfToken,
198+
}),
199+
headers: {
200+
"Content-Type": "application/json",
201+
},
202+
method: "POST",
203+
});
204+
205+
const bookingWithCancellationReason = {
206+
...(bookingCancelledEventProps.booking as object),
207+
cancellationReason,
208+
} as unknown;
209+
210+
if (res.status >= 200 && res.status < 300) {
211+
sdkActionManager?.fire("bookingCancelled", {
212+
...bookingCancelledEventProps,
213+
booking: bookingWithCancellationReason,
214+
});
215+
refreshData();
216+
if (props.onCanceled) {
217+
props.onCanceled();
218+
}
219+
} else {
220+
const data = await res.json();
221+
const errorMessage =
222+
data.message ||
223+
`${t("error_with_status_code_occured", { status: res.status })} ${t(
224+
"please_try_again"
225+
)}`;
226+
227+
if (props.showErrorAsToast) {
228+
showToast(errorMessage, "error");
229+
} else {
230+
setError(errorMessage);
231+
}
232+
}
233+
} finally{
234+
setLoading(false);
235+
}
236+
}
237+
180238
return (
181239
<>
182240
{error && !props.showErrorAsToast && (
@@ -268,62 +326,7 @@ export default function CancelBooking(props: Props) {
268326
<Button
269327
data-testid="confirm_cancel"
270328
disabled={hostMissingCancellationReason || cancellationNoShowFeeNotAcknowledged}
271-
onClick={async () => {
272-
setLoading(true);
273-
274-
// telemetry.event(telemetryEventTypes.bookingCancelled, collectPageParameters());
275-
276-
const response = await fetch("/api/csrf?sameSite=none", { cache: "no-store" });
277-
const { csrfToken } = await response.json();
278-
279-
const res = await fetch("/api/cancel", {
280-
body: JSON.stringify({
281-
uid: booking?.uid,
282-
cancellationReason: cancellationReason,
283-
allRemainingBookings,
284-
// @NOTE: very important this shouldn't cancel with number ID use uid instead
285-
seatReferenceUid,
286-
cancelledBy: currentUserEmail,
287-
internalNote: internalNote,
288-
csrfToken,
289-
}),
290-
headers: {
291-
"Content-Type": "application/json",
292-
},
293-
method: "POST",
294-
});
295-
296-
const bookingWithCancellationReason = {
297-
...(bookingCancelledEventProps.booking as object),
298-
cancellationReason,
299-
} as unknown;
300-
301-
if (res.status >= 200 && res.status < 300) {
302-
// tested by apps/web/playwright/booking-pages.e2e.ts
303-
sdkActionManager?.fire("bookingCancelled", {
304-
...bookingCancelledEventProps,
305-
booking: bookingWithCancellationReason,
306-
});
307-
refreshData();
308-
if (props.onCanceled) {
309-
props.onCanceled();
310-
}
311-
} else {
312-
const data = await res.json();
313-
setLoading(false);
314-
const errorMessage =
315-
data.message ||
316-
`${t("error_with_status_code_occured", { status: res.status })} ${t(
317-
"please_try_again"
318-
)}`;
319-
320-
if (props.showErrorAsToast) {
321-
showToast(errorMessage, "error");
322-
} else {
323-
setError(errorMessage);
324-
}
325-
}
326-
}}
329+
onClick={handleCancel}
327330
loading={loading}>
328331
{props.allRemainingBookings ? t("cancel_all_remaining") : t("cancel_event")}
329332
</Button>

0 commit comments

Comments
 (0)