Skip to content

Commit b81a307

Browse files
authored
fix: duplicate get schedule (calcom#24471)
* fix: duplciate get schedule * fix: duplciate get schedule * chore: remvoe comment * chore: update comment
1 parent b312955 commit b81a307

3 files changed

Lines changed: 42 additions & 20 deletions

File tree

packages/features/bookings/Booker/components/hooks/usePrefetch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export const usePrefetch = ({ date, month, bookerLayout, bookerState }: UsePrefe
3636
bookerLayout.layout,
3737
bookerState,
3838
monthAfterAdding1Month,
39-
monthAfterAddingExtraDaysColumnView
39+
monthAfterAddingExtraDaysColumnView,
40+
prefetchNextMonth
4041
);
4142

4243
return {

packages/features/bookings/Booker/utils/getPrefetchMonthCount.test.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,96 @@ import { getPrefetchMonthCount } from "./getPrefetchMonthCount";
77
describe("getPrefetchMonthCount", () => {
88
describe("COLUMN_VIEW layout", () => {
99
it("should return 2 when months are different", () => {
10-
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 1, 2);
10+
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 1, 2, false);
1111
expect(result).toBe(2);
1212
});
1313

1414
it("should return undefined when months are the same", () => {
15-
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, 5);
15+
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, 5, false);
1616
expect(result).toBe(undefined);
1717
});
1818

1919
it("should return 2 regardless of bookerState when months are different", () => {
20-
expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", 3, 4)).toBe(2);
21-
expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "booking", 7, 8)).toBe(2);
20+
expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", 3, 4, false)).toBe(2);
21+
expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "booking", 7, 8, false)).toBe(2);
22+
});
23+
24+
it("should return 2 even when prefetchNextMonth is true", () => {
25+
expect(getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", 3, 4, true)).toBe(2);
2226
});
2327
});
2428

2529
describe("WEEK_VIEW layout", () => {
2630
it("should return undefined when state is selecting_time", () => {
27-
const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_time", 1, 2);
31+
const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_time", 1, 2, false);
2832
expect(result).toBe(undefined);
2933
});
3034

3135
it("should return undefined when state is not selecting_time", () => {
32-
const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_date", 1, 2);
36+
const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "selecting_date", 1, 2, false);
3337
expect(result).toBe(undefined);
3438
});
3539

3640
it("should return undefined even with different months", () => {
37-
const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "booking", 0, 11);
41+
const result = getPrefetchMonthCount(BookerLayouts.WEEK_VIEW, "booking", 0, 11, false);
3842
expect(result).toBe(undefined);
3943
});
4044
});
4145

4246
describe("MONTH_VIEW layout with selecting_time state", () => {
43-
it("should return 2 when months are different", () => {
44-
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 3, 4);
47+
it("should return 2 when months are different and NOT prefetching", () => {
48+
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 3, 4, false);
4549
expect(result).toBe(2);
4650
});
4751

52+
it("should return undefined when months are different but ALREADY prefetching", () => {
53+
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 3, 4, true);
54+
expect(result).toBe(undefined);
55+
});
56+
4857
it("should return undefined when months are the same", () => {
49-
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 6, 6);
58+
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_time", 6, 6, false);
5059
expect(result).toBe(undefined);
5160
});
5261
});
5362

5463
describe("MONTH_VIEW layout with other states", () => {
5564
it("should return undefined when state is selecting_date", () => {
56-
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_date", 1, 2);
65+
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "selecting_date", 1, 2, false);
5766
expect(result).toBe(undefined);
5867
});
5968

6069
it("should return undefined when state is booking", () => {
61-
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "booking", 1, 2);
70+
const result = getPrefetchMonthCount(BookerLayouts.MONTH_VIEW, "booking", 1, 2, false);
6271
expect(result).toBe(undefined);
6372
});
6473
});
6574

6675
describe("invalid month inputs", () => {
6776
it("should return undefined when first month is NaN", () => {
68-
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", NaN, 5);
77+
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", NaN, 5, false);
6978
expect(result).toBe(undefined);
7079
});
7180

7281
it("should return undefined when second month is NaN", () => {
73-
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, NaN);
82+
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 5, NaN, false);
7483
expect(result).toBe(undefined);
7584
});
7685

7786
it("should return undefined when both months are invalid", () => {
78-
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", Infinity, -Infinity);
87+
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_time", Infinity, -Infinity, false);
7988
expect(result).toBe(undefined);
8089
});
8190
});
8291

8392
describe("edge cases", () => {
8493
it("should handle month transitions (11 to 0)", () => {
85-
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 11, 0);
94+
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", 11, 0, false);
8695
expect(result).toBe(2);
8796
});
8897

8998
it("should handle negative month numbers if they are different", () => {
90-
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", -1, 5);
99+
const result = getPrefetchMonthCount(BookerLayouts.COLUMN_VIEW, "selecting_date", -1, 5, false);
91100
expect(result).toBe(2);
92101
});
93102
});

packages/features/bookings/Booker/utils/getPrefetchMonthCount.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export const getPrefetchMonthCount = (
77
bookerLayout: string,
88
bookerState: BookerState,
99
firstMonth: number,
10-
secondMonth: number
10+
secondMonth: number,
11+
prefetchNextMonth: boolean
1112
) => {
1213
const isDifferentMonth = areDifferentValidMonths(firstMonth, secondMonth);
1314

@@ -17,7 +18,18 @@ export const getPrefetchMonthCount = (
1718

1819
if (!isDifferentMonth) return undefined;
1920

20-
if (isColumnView || (!isWeekView && isSelectingTime)) return 2;
21+
// Column view always needs 2 months because it displays multiple weeks side-by-side,
22+
// regardless of user state or whether we're already prefetching
23+
if (isColumnView) return 2;
24+
25+
// Month view conditionally needs an extra month for performance optimization.
26+
// Only return 2 when:
27+
// 1. User is selecting time slots (improves UX by preloading more availability)
28+
// 2. We're NOT already prefetching next month (prevents duplicate API calls)
29+
//
30+
// If prefetchNextMonth is already true (e.g., viewing dates after 15th of month),
31+
// we don't need extra months since the next month is already being fetched via prefetchNextMonth
32+
if (!isWeekView && isSelectingTime && !prefetchNextMonth) return 2;
2133

2234
return undefined;
2335
};

0 commit comments

Comments
 (0)