add schedule form validation to ensure at least one occurrence

This commit is contained in:
Keith J. Grant
2022-09-07 10:19:25 -07:00
parent 0005d249c0
commit 078c3ae6d8
3 changed files with 79 additions and 33 deletions

View File

@@ -20,6 +20,7 @@ import ScheduleFormFields from './ScheduleFormFields';
import UnsupportedScheduleForm from './UnsupportedScheduleForm';
import parseRuleObj, { UnsupportedRRuleError } from './parseRuleObj';
import buildRuleObj from './buildRuleObj';
import buildRuleSet from './buildRuleSet';
const NUM_DAYS_PER_FREQUENCY = {
week: 7,
@@ -411,6 +412,26 @@ function ScheduleForm({
}
});
if (values.exceptionFrequency.length > 0) {
let rangeToCheck = 1;
values.frequency.forEach((freq) => {
if (NUM_DAYS_PER_FREQUENCY[freq] > rangeToCheck) {
rangeToCheck = NUM_DAYS_PER_FREQUENCY[freq];
}
});
const ruleSet = buildRuleSet(values, true);
const startDate = DateTime.fromISO(values.startDate);
const endDate = startDate.plus({ days: rangeToCheck });
const instances = ruleSet.between(
startDate.toJSDate(),
endDate.toJSDate(),
true
);
if (instances.length === 0) {
errors.exceptionFrequency = t`This schedule has no occurrences due to the selected exceptions.`;
}
}
return errors;
};