feat(reminders): validate frequency and time range
This commit is contained in:
54
components/modals/reminderValidation.ts
Normal file
54
components/modals/reminderValidation.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user