fix: duplicate get schedule (#24471)

* fix: duplciate get schedule

* fix: duplciate get schedule

* chore: remvoe comment

* chore: update comment
This commit is contained in:
Udit Takkar
2025-10-15 10:45:50 +00:00
committed by GitHub
parent b312955838
commit b81a307bd9
3 changed files with 42 additions and 20 deletions
@@ -36,7 +36,8 @@ export const usePrefetch = ({ date, month, bookerLayout, bookerState }: UsePrefe
bookerLayout.layout,
bookerState,
monthAfterAdding1Month,
monthAfterAddingExtraDaysColumnView
monthAfterAddingExtraDaysColumnView,
prefetchNextMonth
);
return {
@@ -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);
});
});
@@ -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;
};