From b81a307bd98328289acf5bf5c5acdc97e554c7f4 Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Wed, 15 Oct 2025 16:15:50 +0530 Subject: [PATCH] fix: duplicate get schedule (#24471) * fix: duplciate get schedule * fix: duplciate get schedule * chore: remvoe comment * chore: update comment --- .../Booker/components/hooks/usePrefetch.ts | 3 +- .../utils/getPrefetchMonthCount.test.ts | 43 +++++++++++-------- .../Booker/utils/getPrefetchMonthCount.ts | 16 ++++++- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/packages/features/bookings/Booker/components/hooks/usePrefetch.ts b/packages/features/bookings/Booker/components/hooks/usePrefetch.ts index 9df53bd918..3f746aaf97 100644 --- a/packages/features/bookings/Booker/components/hooks/usePrefetch.ts +++ b/packages/features/bookings/Booker/components/hooks/usePrefetch.ts @@ -36,7 +36,8 @@ export const usePrefetch = ({ date, month, bookerLayout, bookerState }: UsePrefe bookerLayout.layout, bookerState, monthAfterAdding1Month, - monthAfterAddingExtraDaysColumnView + monthAfterAddingExtraDaysColumnView, + prefetchNextMonth ); return { diff --git a/packages/features/bookings/Booker/utils/getPrefetchMonthCount.test.ts b/packages/features/bookings/Booker/utils/getPrefetchMonthCount.test.ts index 03437be337..2db3076a28 100644 --- a/packages/features/bookings/Booker/utils/getPrefetchMonthCount.test.ts +++ b/packages/features/bookings/Booker/utils/getPrefetchMonthCount.test.ts @@ -7,87 +7,96 @@ import { getPrefetchMonthCount } from "./getPrefetchMonthCount"; describe("getPrefetchMonthCount", () => { describe("COLUMN_VIEW layout", () => { it("should return 2 when months are different", () => { - const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 1, 2); + const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 1, 2, false); expect(result).toBe(2); }); it("should return undefined when months are the same", () => { - const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, 5); + const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, 5, false); expect(result).toBe(undefined); }); it("should return 2 regardless of bookerState when months are different", () => { - expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", 3, 4)).toBe(2); - expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "booking", 7, 8)).toBe(2); + expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", 3, 4, false)).toBe(2); + expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "booking", 7, 8, false)).toBe(2); + }); + + it("should return 2 even when prefetchNextMonth is true", () => { + expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", 3, 4, true)).toBe(2); }); }); describe("WEEK_VIEW layout", () => { it("should return undefined when state is selecting_time", () => { - const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_time", 1, 2); + const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_time", 1, 2, false); expect(result).toBe(undefined); }); it("should return undefined when state is not selecting_time", () => { - const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_date", 1, 2); + const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_date", 1, 2, false); expect(result).toBe(undefined); }); it("should return undefined even with different months", () => { - const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "booking", 0, 11); + const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "booking", 0, 11, false); expect(result).toBe(undefined); }); }); describe("MONTH_VIEW layout with selecting_time state", () => { - it("should return 2 when months are different", () => { - const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 3, 4); + it("should return 2 when months are different and NOT prefetching", () => { + const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 3, 4, false); expect(result).toBe(2); }); + it("should return undefined when months are different but ALREADY prefetching", () => { + const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 3, 4, true); + expect(result).toBe(undefined); + }); + it("should return undefined when months are the same", () => { - const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 6, 6); + const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 6, 6, false); expect(result).toBe(undefined); }); }); describe("MONTH_VIEW layout with other states", () => { it("should return undefined when state is selecting_date", () => { - const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_date", 1, 2); + const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_date", 1, 2, false); expect(result).toBe(undefined); }); it("should return undefined when state is booking", () => { - const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "booking", 1, 2); + const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "booking", 1, 2, false); expect(result).toBe(undefined); }); }); describe("invalid month inputs", () => { it("should return undefined when first month is NaN", () => { - const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", NaN, 5); + const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", NaN, 5, false); expect(result).toBe(undefined); }); it("should return undefined when second month is NaN", () => { - const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, NaN); + const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, NaN, false); expect(result).toBe(undefined); }); it("should return undefined when both months are invalid", () => { - const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", Infinity, -Infinity); + const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", Infinity, -Infinity, false); expect(result).toBe(undefined); }); }); describe("edge cases", () => { it("should handle month transitions (11 to 0)", () => { - const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 11, 0); + const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 11, 0, false); expect(result).toBe(2); }); it("should handle negative month numbers if they are different", () => { - const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", -1, 5); + const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", -1, 5, false); expect(result).toBe(2); }); }); diff --git a/packages/features/bookings/Booker/utils/getPrefetchMonthCount.ts b/packages/features/bookings/Booker/utils/getPrefetchMonthCount.ts index e3d4723847..94be221b1f 100644 --- a/packages/features/bookings/Booker/utils/getPrefetchMonthCount.ts +++ b/packages/features/bookings/Booker/utils/getPrefetchMonthCount.ts @@ -7,7 +7,8 @@ export const getPrefetchMonthCount = ( bookerLayout: string, bookerState: BookerState, firstMonth: number, - secondMonth: number + secondMonth: number, + prefetchNextMonth: boolean ) => { const isDifferentMonth = areDifferentValidMonths(firstMonth, secondMonth); @@ -17,7 +18,18 @@ export const getPrefetchMonthCount = ( if (!isDifferentMonth) return undefined; - if (isColumnView || (!isWeekView && isSelectingTime)) return 2; + // Column view always needs 2 months because it displays multiple weeks side-by-side, + // regardless of user state or whether we're already prefetching + if (isColumnView) return 2; + + // Month view conditionally needs an extra month for performance optimization. + // Only return 2 when: + // 1. User is selecting time slots (improves UX by preloading more availability) + // 2. We're NOT already prefetching next month (prevents duplicate API calls) + // + // If prefetchNextMonth is already true (e.g., viewing dates after 15th of month), + // we don't need extra months since the next month is already being fetched via prefetchNextMonth + if (!isWeekView && isSelectingTime && !prefetchNextMonth) return 2; return undefined; };