Skip to content

Commit

Permalink
chore: Simplify date range validation logic in DatePickerWidget2 by r…
Browse files Browse the repository at this point in the history
…emoving unused vars and consolidating comparison settings
  • Loading branch information
rahulbarwal committed Nov 5, 2024
1 parent d47896f commit 6da8ec5
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions app/client/src/widgets/DatePickerWidget2/widget/derived.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable @typescript-eslint/no-unused-vars*/
export default {
isValidDate: (props, moment, _) => {
isValidDate: (props, moment) => {
// Early return if no date is selected and it's not required
if (!props.selectedDate && !props.isRequired) {
return true;
Expand All @@ -18,45 +17,36 @@ export default {
return !props.isRequired;
}

// Get comparison settings based on time precision
const { granularity, inclusivity } = getDateComparisonSettings(
props.timePrecision,
);
// Set comparison settings based on time precision
let granularity, inclusivity;

switch (props.timePrecision) {
case "None":
granularity = "day";
inclusivity = "[]";
break;
case "second":
case "minute":
case "millisecond":
granularity = props.timePrecision;
inclusivity = "[]";
break;
}

// Check date range constraints
if (minDate && maxDate) {
return selectedDate.isBetween(minDate, maxDate, granularity, inclusivity);
}

if (minDate) {
return selectedDate.isAfter(minDate, granularity, inclusivity);
return selectedDate.isAfter(minDate);
}

if (maxDate) {
return selectedDate.isBefore(maxDate, granularity, inclusivity);
return selectedDate.isBefore(maxDate);
}

return true;
},
//
};

function getDateComparisonSettings(timePrecision) {
const settings = {
granularity: undefined,
inclusivity: "[]", // Include both bounds by default
};

switch (timePrecision) {
case "None":
settings.granularity = "day";
break;
case "second":
case "minute":
case "millisecond":
settings.granularity = timePrecision;
break;
}

return settings;
}

0 comments on commit 6da8ec5

Please sign in to comment.