fix(bookings): enable past date selection for cancelled bookings (#25644)
* refactor(data-table): clean up DateRangeFilter range options - Replace 'past' | 'custom' with 'past' | 'future' | 'any' | 'customOnly' - Add direction field to PresetOption for preset compatibility filtering - Derive presets visibility automatically based on compatible presets - Update bookings list to use new range values: - past -> 'past' - upcoming -> 'future' - unconfirmed/recurring/cancelled -> 'any' Co-Authored-By: [email protected] <[email protected]> * feat(playground): add DateRangeFilter playground page with E2E tests - Add playground page at /settings/admin/playground/date-range-filter - Demonstrate all 4 range options: past, future, any, customOnly - Add link to playground index page - Add E2E tests for presets visibility and date restrictions Co-Authored-By: [email protected] <[email protected]> * fix(playground): use correct meta.filter pattern for column filter config Co-Authored-By: [email protected] <[email protected]> * clean up the playground esign * add unit tests instead of e2e * fix the implementation --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
dd27d077ca
commit
aaaff0705b
9 files changed
+283
-30
No files matched your search
@@ -24,10 +24,10 @@ import {
|
||||
CUSTOM_PRESET,
|
||||
CUSTOM_PRESET_VALUE,
|
||||
DEFAULT_PRESET,
|
||||
PRESET_OPTIONS,
|
||||
getDefaultStartDate,
|
||||
getDefaultEndDate,
|
||||
getDateRangeFromPreset,
|
||||
getCompatiblePresets,
|
||||
type PresetOption,
|
||||
} from "../../lib/dateRange";
|
||||
import { preserveLocalTime } from "../../lib/preserveLocalTime";
|
||||
@@ -53,8 +53,10 @@ export const DateRangeFilter = ({
|
||||
const filterValue = useFilterValue(column.id, ZDateRangeFilterValue);
|
||||
const { updateFilter, removeFilter, timeZone: givenTimeZone } = useDataTable();
|
||||
const range = options?.range ?? "past";
|
||||
const forceCustom = range === "custom";
|
||||
const forcePast = range === "past";
|
||||
|
||||
const compatiblePresets = getCompatiblePresets(range);
|
||||
const showPresets = compatiblePresets.length > 1;
|
||||
const forceCustomOnly = range === "customOnly" || !showPresets;
|
||||
|
||||
const { t } = useLocale();
|
||||
const currentDate = dayjs();
|
||||
@@ -65,10 +67,10 @@ export const DateRangeFilter = ({
|
||||
filterValue?.data.endDate ? dayjs(filterValue.data.endDate) : undefined
|
||||
);
|
||||
const [selectedPreset, setSelectedPreset] = useState<PresetOption>(
|
||||
forceCustom
|
||||
forceCustomOnly
|
||||
? CUSTOM_PRESET
|
||||
: filterValue?.data.preset
|
||||
? PRESET_OPTIONS.find((o) => o.value === filterValue.data.preset) ?? DEFAULT_PRESET
|
||||
? compatiblePresets.find((o) => o.value === filterValue.data.preset) ?? DEFAULT_PRESET
|
||||
: DEFAULT_PRESET
|
||||
);
|
||||
|
||||
@@ -108,14 +110,14 @@ export const DateRangeFilter = ({
|
||||
useEffect(() => {
|
||||
// initially apply the default value
|
||||
// if the query param is not set yet
|
||||
if (!filterValue && !forceCustom) {
|
||||
if (!filterValue && !forceCustomOnly) {
|
||||
updateValues({
|
||||
preset: DEFAULT_PRESET,
|
||||
startDate: getDefaultStartDate(),
|
||||
endDate: getDefaultEndDate(),
|
||||
});
|
||||
}
|
||||
}, [filterValue, forceCustom, updateValues]);
|
||||
}, [filterValue, forceCustomOnly, updateValues]);
|
||||
|
||||
const updateDateRangeFromPreset = (val: string | null) => {
|
||||
if (val === CUSTOM_PRESET_VALUE) {
|
||||
@@ -195,13 +197,19 @@ export const DateRangeFilter = ({
|
||||
endDate: endDate?.toDate(),
|
||||
}}
|
||||
data-testid="date-range-calendar"
|
||||
minDate={forcePast ? currentDate.subtract(2, "year").toDate() : null}
|
||||
maxDate={forcePast ? currentDate.toDate() : undefined}
|
||||
minDate={
|
||||
range === "past"
|
||||
? currentDate.subtract(2, "year").toDate()
|
||||
: range === "future"
|
||||
? currentDate.toDate()
|
||||
: null
|
||||
}
|
||||
maxDate={range === "past" ? currentDate.toDate() : undefined}
|
||||
disabled={false}
|
||||
onDatesChange={updateDateRangeFromPicker}
|
||||
withoutPopover={true}
|
||||
/>
|
||||
{forceCustom && (
|
||||
{forceCustomOnly && (
|
||||
<div className="border-subtle border-t px-2 py-3">
|
||||
<Button
|
||||
color="secondary"
|
||||
@@ -213,10 +221,10 @@ export const DateRangeFilter = ({
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{!forceCustom && (
|
||||
{showPresets && (
|
||||
<Command className={classNames("w-40", isCustomPreset && "rounded-b-none")}>
|
||||
<CommandList>
|
||||
{PRESET_OPTIONS.map((option) => (
|
||||
{compatiblePresets.map((option) => (
|
||||
<CommandItem
|
||||
key={option.value}
|
||||
data-testid={`date-range-options-${option.value}`}
|
||||
|
||||
@@ -5,6 +5,7 @@ import dayjs from "@calcom/dayjs";
|
||||
import {
|
||||
getDateRangeFromPreset,
|
||||
recalculateDateRange,
|
||||
getCompatiblePresets,
|
||||
PRESET_OPTIONS,
|
||||
CUSTOM_PRESET,
|
||||
DEFAULT_PRESET,
|
||||
@@ -111,4 +112,55 @@ describe("PRESET_OPTIONS", () => {
|
||||
expect(CUSTOM_PRESET.value).toBe(CUSTOM_PRESET_VALUE);
|
||||
expect(CUSTOM_PRESET.labelKey).toBe("custom_range");
|
||||
});
|
||||
|
||||
it("should have correct direction for each preset", () => {
|
||||
expect(PRESET_OPTIONS[0].direction).toBe("past"); // Today
|
||||
expect(PRESET_OPTIONS[1].direction).toBe("past"); // Last 7 days
|
||||
expect(PRESET_OPTIONS[2].direction).toBe("past"); // Last 30 days
|
||||
expect(PRESET_OPTIONS[3].direction).toBe("past"); // Month to date
|
||||
expect(PRESET_OPTIONS[4].direction).toBe("past"); // Year to date
|
||||
expect(PRESET_OPTIONS[5].direction).toBe("any"); // Custom
|
||||
});
|
||||
});
|
||||
|
||||
describe("getCompatiblePresets", () => {
|
||||
it("should return empty array for customOnly range", () => {
|
||||
const result = getCompatiblePresets("customOnly");
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it("should return all presets for any range", () => {
|
||||
const result = getCompatiblePresets("any");
|
||||
expect(result.length).toBe(6); // All 6 presets
|
||||
expect(result.map((p) => p.value)).toEqual(["tdy", "w", "t", "m", "y", "c"]);
|
||||
});
|
||||
|
||||
it("should return all presets for past range (including direction:any)", () => {
|
||||
const result = getCompatiblePresets("past");
|
||||
expect(result.length).toBe(6); // All 6 presets (5 past + 1 any)
|
||||
// Should include both "past" direction and "any" direction presets
|
||||
const directions = result.map((p) => p.direction);
|
||||
expect(directions).toContain("past");
|
||||
expect(directions).toContain("any");
|
||||
expect(result.map((p) => p.value)).toEqual(["tdy", "w", "t", "m", "y", "c"]);
|
||||
});
|
||||
|
||||
it("should return only any-direction presets for future range", () => {
|
||||
const result = getCompatiblePresets("future");
|
||||
expect(result.length).toBe(1); // Only Custom (Today is now past-direction)
|
||||
expect(result.map((p) => p.value)).toEqual(["c"]);
|
||||
// All returned presets should have direction "any"
|
||||
expect(result.every((p) => p.direction === "any")).toBe(true);
|
||||
});
|
||||
|
||||
it("should not return past-only presets for future range", () => {
|
||||
const result = getCompatiblePresets("future");
|
||||
const values = result.map((p) => p.value);
|
||||
// Should NOT include past-only presets (including Today)
|
||||
expect(values).not.toContain("tdy"); // Today (now past-direction)
|
||||
expect(values).not.toContain("w"); // Last 7 days
|
||||
expect(values).not.toContain("t"); // Last 30 days
|
||||
expect(values).not.toContain("m"); // Month to date
|
||||
expect(values).not.toContain("y"); // Year to date
|
||||
});
|
||||
});
|
||||
@@ -4,10 +4,13 @@ import { ColumnFilterType, type DateRangeFilterValue } from "./types";
|
||||
|
||||
export type PresetOptionValue = "c" | "w" | "m" | "y" | "t" | "tdy";
|
||||
|
||||
export type PresetDirection = "past" | "future" | "any";
|
||||
|
||||
export type PresetOption = {
|
||||
labelKey: string;
|
||||
i18nOptions?: Record<string, string | number>;
|
||||
value: PresetOptionValue;
|
||||
direction: PresetDirection;
|
||||
};
|
||||
|
||||
export const CUSTOM_PRESET_VALUE = "c" as const;
|
||||
@@ -16,18 +19,34 @@ export const DEFAULT_PRESET: PresetOption = {
|
||||
labelKey: "last_number_of_days",
|
||||
i18nOptions: { count: 7 },
|
||||
value: "w",
|
||||
direction: "past",
|
||||
};
|
||||
export const CUSTOM_PRESET: PresetOption = {
|
||||
labelKey: "custom_range",
|
||||
value: CUSTOM_PRESET_VALUE,
|
||||
direction: "any",
|
||||
};
|
||||
export const CUSTOM_PRESET: PresetOption = { labelKey: "custom_range", value: CUSTOM_PRESET_VALUE };
|
||||
|
||||
export const PRESET_OPTIONS: PresetOption[] = [
|
||||
{ labelKey: "today", value: "tdy" },
|
||||
{ labelKey: "today", value: "tdy", direction: "past" },
|
||||
DEFAULT_PRESET,
|
||||
{ labelKey: "last_number_of_days", i18nOptions: { count: 30 }, value: "t" },
|
||||
{ labelKey: "month_to_date", value: "m" },
|
||||
{ labelKey: "year_to_date", value: "y" },
|
||||
{ labelKey: "last_number_of_days", i18nOptions: { count: 30 }, value: "t", direction: "past" },
|
||||
{ labelKey: "month_to_date", value: "m", direction: "past" },
|
||||
{ labelKey: "year_to_date", value: "y", direction: "past" },
|
||||
CUSTOM_PRESET,
|
||||
];
|
||||
|
||||
export const getCompatiblePresets = (range: "past" | "future" | "any" | "customOnly"): PresetOption[] => {
|
||||
if (range === "customOnly") {
|
||||
return [];
|
||||
}
|
||||
return PRESET_OPTIONS.filter((preset) => {
|
||||
if (preset.direction === "any") return true;
|
||||
if (range === "any") return true;
|
||||
return preset.direction === range;
|
||||
});
|
||||
};
|
||||
|
||||
export const getDefaultStartDate = () => dayjs().subtract(6, "day").startOf("day");
|
||||
|
||||
export const getDefaultEndDate = () => dayjs().endOf("day");
|
||||
|
||||
@@ -118,7 +118,7 @@ export const ZFilterValue = z.union([
|
||||
]);
|
||||
|
||||
export type DateRangeFilterOptions = {
|
||||
range?: "past" | "custom";
|
||||
range?: "past" | "future" | "any" | "customOnly";
|
||||
convertToTimeZone?: boolean;
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user