Skip to content

Commit 8709a86

Browse files
committed
Merge branch 'enter-single-startdate-in-daterangepicker' of Arnei/opencast-admin-interface into main
Pull request #1119 Fix entering single date in date range picker
2 parents 879f128 + 13141a7 commit 8709a86

1 file changed

Lines changed: 40 additions & 15 deletions

File tree

src/components/shared/TableFilters.tsx

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,47 @@ const TableFilters = ({
159159
end?.setMinutes(59);
160160
end?.setSeconds(59);
161161

162-
if (start && end && moment(start).isValid() && moment(end).isValid()) {
163-
let filter = filterMap.find(({ name }) => name === selectedFilter);
164-
if (filter) {
165-
dispatch(editFilterValue({
166-
filterName: filter.name,
167-
value: start.toISOString() + "/" + end.toISOString()
168-
}));
169-
setFilterSelector(false);
170-
dispatch(removeSelectedFilter());
171-
// Reload of resource after going to very first page.
172-
dispatch(goToPage(0))
173-
await dispatch(loadResource());
174-
dispatch(loadResourceIntoTable());
175-
}
176-
}
162+
submitDateFilter(start, end);
163+
177164
if (start) setStartDate(start);
178165
if (end) setEndDate(end);
179166
}
180167
}
181168

169+
// Workaround for entering a date range by only entering one date
170+
// (e.g. 01/01/2025 results in a range of 01/01/2025 - 01/01/2025)
171+
const handleDatePickerOnKeyDown = async(keyEvent: React.KeyboardEvent<HTMLElement>) => {
172+
if (keyEvent.key === "Enter") {
173+
let end = endDate ?? (startDate ? new Date(startDate) : undefined);
174+
end?.setHours(23);
175+
end?.setMinutes(59);
176+
end?.setSeconds(59);
177+
178+
submitDateFilter(
179+
startDate,
180+
end
181+
)
182+
}
183+
}
184+
185+
const submitDateFilter = async(start: Date | undefined | null, end: Date | undefined | null) => {
186+
if (start && end && moment(start).isValid() && moment(end).isValid()) {
187+
let filter = filterMap.find(({ name }) => name === selectedFilter);
188+
if (filter) {
189+
dispatch(editFilterValue({
190+
filterName: filter.name,
191+
value: start.toISOString() + "/" + end.toISOString()
192+
}));
193+
setFilterSelector(false);
194+
dispatch(removeSelectedFilter());
195+
// Reload of resource after going to very first page.
196+
dispatch(goToPage(0))
197+
await dispatch(loadResource());
198+
dispatch(loadResourceIntoTable());
199+
}
200+
}
201+
}
202+
182203
useHotkeys(
183204
availableHotkeys.general.REMOVE_FILTERS.sequence,
184205
() => removeFilters(),
@@ -275,6 +296,7 @@ const TableFilters = ({
275296
startDate={startDate}
276297
endDate={endDate}
277298
handleDate={handleDatepicker}
299+
handleDatePickerOnKeyDown={handleDatePickerOnKeyDown}
278300
handleChange={handleChange}
279301
openSecondFilterMenu={openSecondFilterMenu}
280302
setOpenSecondFilterMenu={setOpenSecondFilterMenu}
@@ -353,6 +375,7 @@ const TableFilters = ({
353375
const FilterSwitch = ({
354376
filter,
355377
handleChange,
378+
handleDatePickerOnKeyDown,
356379
startDate,
357380
endDate,
358381
handleDate,
@@ -362,6 +385,7 @@ const FilterSwitch = ({
362385
} : {
363386
filter: FilterData | undefined,
364387
handleChange: (name: string, value: string) => void,
388+
handleDatePickerOnKeyDown: (keyEvent: React.KeyboardEvent<HTMLElement>) => void,
365389
startDate: Date | undefined,
366390
endDate: Date | undefined,
367391
handleDate: (dates: [Date | undefined | null, Date | undefined | null]) => void,
@@ -429,6 +453,7 @@ const FilterSwitch = ({
429453
autoFocus
430454
selected={startDate}
431455
onChange={(dates) => handleDate(dates)}
456+
onKeyDown={(key) => handleDatePickerOnKeyDown(key)}
432457
startDate={startDate}
433458
endDate={endDate}
434459
selectsRange

0 commit comments

Comments
 (0)