Skip to content

Commit

Permalink
date-range-fix (sartography#1673)
Browse files Browse the repository at this point in the history
* add debounce to date input fields in date range picker to properly format dates w/ burnettk

* use descriptive ruff ids for pre-commit w/ burnettk

---------

Co-authored-by: jasquat <[email protected]>
  • Loading branch information
jasquat and jasquat authored Jun 4, 2024
1 parent 5cc86a8 commit fe2a8da
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repos:
language: system
types: [text]
stages: [commit, push, manual]
- id: ruff
- id: ruff-check
args: [check, --fix]
files: ^spiffworkflow-backend/
name: ruff
Expand All @@ -36,7 +36,7 @@ repos:
# this is also specified in spiffworkflow-backend/pyproject.toml but we run pre-commit
# with all-files which ignores that
exclude: "/migrations/"
- id: ruff
- id: ruff-format
args: [format]
files: ^spiffworkflow-backend/
name: ruff
Expand Down
6 changes: 3 additions & 3 deletions spiffworkflow-frontend/src/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const supportedDateFormatTypes = {
dd: '01',
};
const unsupportedFormatTypes = splitDateFormat.filter(
(x) => !Object.keys(supportedDateFormatTypes).includes(x)
(x) => !Object.keys(supportedDateFormatTypes).includes(x),
);
const formattedSupportedDateTypes: string[] = [];
Object.entries(supportedDateFormatTypes).forEach(([key, value]) => {
Expand All @@ -107,8 +107,8 @@ Object.entries(supportedDateFormatTypes).forEach(([key, value]) => {
if (unsupportedFormatTypes.length > 0) {
throw new Error(
`Given SPIFFWORKFLOW_FRONTEND_RUNTIME_CONFIG_DATE_FORMAT is not supported. Given: ${generalDateFormat} with invalid options: ${unsupportedFormatTypes.join(
', '
)}. Valid options are: ${formattedSupportedDateTypes.join(', ')}`
', ',
)}. Valid options are: ${formattedSupportedDateTypes.join(', ')}`,
);
}
const carbonDateFormat = generalDateFormat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../../../config';
import { getCommonAttributes } from '../../helpers';
import DateAndTimeService from '../../../services/DateAndTimeService';
import { useDebouncedCallback } from 'use-debounce';

interface widgetArgs {
id: string;
Expand Down Expand Up @@ -42,20 +43,20 @@ export default function DateRangePickerWidget({
label,
schema,
uiSchema,
rawErrors
rawErrors,
);

const onChangeLocal = useCallback(
(dateRange: Date[]) => {
let dateRangeString;
const startDate = DateAndTimeService.convertDateObjectToFormattedString(
dateRange[0]
dateRange[0],
);
if (startDate) {
const startDateYMD =
DateAndTimeService.dateStringToYMDFormat(startDate);
const endDate = DateAndTimeService.convertDateObjectToFormattedString(
dateRange[1]
dateRange[1],
);
dateRangeString = startDateYMD;
if (endDate) {
Expand All @@ -65,7 +66,7 @@ export default function DateRangePickerWidget({
}
onChange(dateRangeString);
},
[onChange]
[onChange],
);

let dateValue: (Date | null)[] | null = value;
Expand All @@ -85,6 +86,17 @@ export default function DateRangePickerWidget({
dateValue = [startDate, endDate];
}

const addDebouncedOnChangeDate = useDebouncedCallback(
(fullObject: React.ChangeEvent<HTMLInputElement>) => {
fullObject.target.value =
DateAndTimeService.attemptToConvertUnknownDateStringFormatToKnownFormat(
fullObject.target.value,
);
},
// delay in ms
100,
);

return (
<DatePicker
className="date-input"
Expand All @@ -97,6 +109,7 @@ export default function DateRangePickerWidget({
id={`${id}-start`}
placeholder={DATE_FORMAT_FOR_DISPLAY}
helperText={commonAttributes.helperText}
labelText=""
type="text"
size="md"
autocomplete="off"
Expand All @@ -105,17 +118,20 @@ export default function DateRangePickerWidget({
invalidText={commonAttributes.errorMessageForField}
autoFocus={autofocus}
pattern={null}
onChange={addDebouncedOnChangeDate}
/>
<DatePickerInput
id={`${id}-end`}
placeholder={DATE_FORMAT_FOR_DISPLAY}
labelText=""
type="text"
size="md"
autocomplete="off"
disabled={disabled || readonly}
invalid={commonAttributes.invalid}
autoFocus={autofocus}
pattern={null}
onChange={addDebouncedOnChangeDate}
/>
</DatePicker>
);
Expand Down

0 comments on commit fe2a8da

Please sign in to comment.