refactor: v2 calendars busy-times (#18244)

* refactor: make dateFrom and dateTo required

* refactor: make dateFrom and dateTo required

* refactor: useCalendarsBusyTimes enabled only if dateFrom & dateTo passed
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2024-12-18 13:36:26 +01:00
1 parent 8bf46ab8b6
commit ad5d92dff0
6 files changed
+57 -42

No files matched your search

@@ -8,7 +8,10 @@ import http from "../lib/http";
export const QUERY_KEY = "get-calendars-busy-times";
type UseCalendarsBusyTimesProps = CalendarBusyTimesInput & { onError?: () => void; enabled: boolean };
type UseCalendarsBusyTimesProps = Omit<CalendarBusyTimesInput, "dateFrom" | "dateTo"> & {
dateFrom: string | null;
dateTo: string | null;
} & { onError?: () => void; enabled: boolean };
export const useCalendarsBusyTimes = ({ onError, enabled, ...rest }: UseCalendarsBusyTimesProps) => {
const availableSlots = useQuery({
@@ -32,7 +35,7 @@ export const useCalendarsBusyTimes = ({ onError, enabled, ...rest }: UseCalendar
throw new Error(res.data.error.message);
});
},
enabled,
enabled: enabled && !!rest.dateFrom && !!rest.dateTo,
});
return availableSlots;
};
+5 -11
View File
@@ -1,9 +1,7 @@
import { ApiProperty } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { Transform } from "class-transformer";
import { IsNumber, IsString, IsOptional, IsArray, ValidateNested, Validate } from "class-validator";
import { IsYearMonthDays } from "./validators/isYearMonthDays";
import { IsNumber, IsString, IsArray, ValidateNested, IsDateString } from "class-validator";
export class Calendar {
@Transform(({ value }: { value: string }) => value && parseInt(value))
@@ -29,23 +27,19 @@ export class CalendarBusyTimesInput {
required: false,
description: "The starting date for the busy times query",
example: "2023-10-01",
nullable: true,
})
@IsString()
@IsOptional()
@Validate(IsYearMonthDays)
dateFrom?: string | null;
@IsDateString()
dateFrom!: string;
@ApiProperty({
required: false,
description: "The ending date for the busy times query",
example: "2023-10-31",
nullable: true,
})
@IsString()
@IsOptional()
@Validate(IsYearMonthDays)
dateTo?: string | null;
@IsDateString()
dateTo!: string;
@ApiProperty({
type: [Calendar],
@@ -1,22 +0,0 @@
import type { ValidatorConstraintInterface } from "class-validator";
import { ValidatorConstraint } from "class-validator";
import { DateTime } from "luxon";
@ValidatorConstraint({ name: "IsYearMonthDays", async: false })
export class IsYearMonthDays implements ValidatorConstraintInterface {
validate(dateString: string): boolean {
try {
// Attempt to parse the date string
const dateTime = DateTime.fromFormat(dateString, "yyyy-MM-dd");
// Check if the parsed date is valid
return dateTime.isValid;
} catch (error) {
return false;
}
}
defaultMessage() {
return "time format must be YEAR-MONTH-DAYS (e.g. 2022-01-01)";
}
}