Files
rxminder/components/modals/reminderValidation.ts
2025-09-23 10:53:12 -07:00

55 lines
1.4 KiB
TypeScript

export const MIN_REMINDER_FREQUENCY_MINUTES = 5;
export const MAX_REMINDER_FREQUENCY_MINUTES = 720;
export const parseTimeToMinutes = (time: string): number | null => {
const [hours, minutes] = time.split(':').map(Number);
if (
Number.isNaN(hours) ||
Number.isNaN(minutes) ||
hours < 0 ||
hours > 23 ||
minutes < 0 ||
minutes > 59
) {
return null;
}
return hours * 60 + minutes;
};
export interface ReminderValidationParams {
frequencyMinutes: number;
startTime: string;
endTime: string;
}
export const validateReminderInputs = ({
frequencyMinutes,
startTime,
endTime,
}: ReminderValidationParams): { frequency?: string; timeRange?: string } => {
const errors: { frequency?: string; timeRange?: string } = {};
const frequency = Number(frequencyMinutes);
if (
!Number.isInteger(frequency) ||
frequency < MIN_REMINDER_FREQUENCY_MINUTES ||
frequency > MAX_REMINDER_FREQUENCY_MINUTES
) {
errors.frequency = `Choose a value between ${MIN_REMINDER_FREQUENCY_MINUTES} and ${MAX_REMINDER_FREQUENCY_MINUTES} minutes.`;
}
const startMinutes = parseTimeToMinutes(startTime);
const endMinutes = parseTimeToMinutes(endTime);
if (
startMinutes === null ||
endMinutes === null ||
endMinutes <= startMinutes
) {
errors.timeRange = 'End time must be later than start time.';
}
return errors;
};