-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add date filter #747
Merged
Merged
Add date filter #747
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { FormEvent, useState } from 'react'; | ||
|
||
import { DatePicker, InputGroup, ToolbarFilter } from '@patternfly/react-core'; | ||
|
||
import { changeFormatToISODate, isValidDate, parseISOtoJSDate, toISODate } from '../../utils'; | ||
|
||
import { FilterTypeProps } from './types'; | ||
|
||
/** | ||
* This Filter type enables selecting a single date (a day). | ||
* | ||
* **FilterTypeProps are interpreted as follows**:<br> | ||
* 1) selectedFilters - dates in YYYY-MM-DD format (ISO date format).<br> | ||
* 2) onFilterUpdate - accepts the list of dates.<br> | ||
* | ||
* [<img src="static/media/src/components-stories/assets/github-logo.svg"><i class="fi fi-brands-github"> | ||
* <font color="green">View component source on GitHub</font>](https://github.com/kubev2v/forklift-console-plugin/blob/main/packages/common/src/components/Filter/DateFilter.tsx) | ||
*/ | ||
export const DateFilter = ({ | ||
selectedFilters = [], | ||
onFilterUpdate, | ||
title, | ||
filterId, | ||
placeholderLabel, | ||
showFilter = true, | ||
}: FilterTypeProps) => { | ||
const validFilters = selectedFilters?.map(changeFormatToISODate)?.filter(Boolean) ?? []; | ||
|
||
// internal state - stored as ISO date string (no time) | ||
const [date, setDate] = useState(toISODate(new Date())); | ||
|
||
const clearSingleDate = (option) => { | ||
onFilterUpdate([...validFilters.filter((d) => d !== option)]); | ||
}; | ||
|
||
const onDateChange = (even: FormEvent<HTMLInputElement>, value: string) => { | ||
// require full format "YYYY-MM-DD" although partial date is also accepted | ||
// i.e. YYYY-MM gets parsed as YYYY-MM-01 and results in auto completing the date | ||
// unfortunately due to auto-complete user cannot delete the date char after char | ||
if (value?.length === 10 && isValidDate(value)) { | ||
const targetDate = changeFormatToISODate(value); | ||
setDate(targetDate); | ||
onFilterUpdate([...validFilters.filter((d) => d !== targetDate), targetDate]); | ||
} | ||
}; | ||
|
||
return ( | ||
<ToolbarFilter | ||
key={filterId} | ||
chips={validFilters} | ||
deleteChip={(category, option) => clearSingleDate(option)} | ||
deleteChipGroup={() => onFilterUpdate([])} | ||
categoryName={title} | ||
showToolbarItem={showFilter} | ||
> | ||
<InputGroup> | ||
<DatePicker | ||
value={date} | ||
dateFormat={toISODate} | ||
dateParse={parseISOtoJSDate} | ||
onChange={onDateChange} | ||
aria-label={title} | ||
placeholder={placeholderLabel} | ||
invalidFormatText={placeholderLabel} | ||
// default value ("parent") creates collision with sticky table header | ||
appendTo={document.body} | ||
/> | ||
</InputGroup> | ||
</ToolbarFilter> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { | ||
areSameDayInUTCZero, | ||
changeFormatToISODate, | ||
changeTimeZoneToUTCZero, | ||
isValidDate, | ||
parseISOtoJSDate, | ||
toISODate, | ||
} from '../dates'; | ||
|
||
describe('changeTimeZoneToUTCZero', () => { | ||
test('from UTC+02:00', () => { | ||
expect(changeTimeZoneToUTCZero('2023-10-31T01:30:00.000+02:00')).toBe( | ||
'2023-10-30T23:30:00.000Z', | ||
); | ||
}); | ||
test('invalid input', () => { | ||
expect(changeTimeZoneToUTCZero('2023-broken-10-31T01:30:00.000+02:00')).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('changeFormatToISODate', () => { | ||
test('from ISO date time with zone', () => { | ||
expect(changeFormatToISODate('2023-10-31T01:30:00.000+02:00')).toBe('2023-10-31'); | ||
}); | ||
test('invalid input', () => { | ||
expect(changeFormatToISODate('2023-broken-10-31T01:30:00.000+02:00')).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('toISODate', () => { | ||
test('unix epoch', () => { | ||
expect(toISODate(new Date(0))).toBe('1970-01-01'); | ||
}); | ||
test('missing date', () => { | ||
expect(toISODate(undefined)).toBe(undefined); | ||
}); | ||
test('invalid date', () => { | ||
expect(toISODate(new Date('foo'))).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('isValidDate', () => { | ||
test('2023-10-31T01:30:00.000+02:00', () => { | ||
expect(isValidDate('2023-10-31T01:30:00.000+02:00')).toBeTruthy(); | ||
}); | ||
test('invalid string', () => { | ||
expect(isValidDate('2023-broken-10-31')).toBeFalsy(); | ||
}); | ||
test('invalid number of days', () => { | ||
expect(isValidDate('2023-10-60')).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('parseISOtoJSDate', () => { | ||
test('2023-10-31T01:30:00.000+02:00', () => { | ||
const date = parseISOtoJSDate('2023-10-31T01:30:00.000+02:00'); | ||
expect(date.toUTCString()).toBe('Mon, 30 Oct 2023 23:30:00 GMT'); | ||
}); | ||
test('invalid input', () => { | ||
expect(parseISOtoJSDate('2023-broken-10-31T01:30:00.000+02:00')).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('areSameDayInUTCZero', () => { | ||
test('the same date', () => { | ||
expect(areSameDayInUTCZero('2023-10-31T01:30:00.000+02:00', '2023-10-30')).toBeTruthy(); | ||
}); | ||
test('the different dates', () => { | ||
expect(areSameDayInUTCZero('2023-10-31T01:30:00.000+02:00', '2023-10-31')).toBeFalsy(); | ||
}); | ||
test('one date invalid', () => { | ||
expect(areSameDayInUTCZero('2023-10-31T10:00:00.000+02:00', '2023-foo')).toBeFalsy(); | ||
}); | ||
test('one date missing, one invalid', () => { | ||
expect(areSameDayInUTCZero(undefined, '2023-foo')).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { DateTime } from 'luxon'; | ||
|
||
/** | ||
* Converts a given ISO date time string to UTC+00:00 time zone. | ||
* | ||
* @param {string} isoDateString - The ISO date time string | ||
* @returns {string} The equivalent UTC+00:00 date time ISO string if input is valid or undefined otherwise. | ||
*/ | ||
export function changeTimeZoneToUTCZero(isoDateString: string): string | undefined { | ||
const date = DateTime.fromISO(isoDateString); | ||
return date.isValid ? date.toUTC().toISO() : undefined; | ||
} | ||
|
||
/** | ||
* Converts a given ISO date time string to ISO date string(no time). | ||
* | ||
* @param {string} isoDateString - The ISO date time string | ||
* @returns {string} The equivalent ISO date string if input is valid or undefined otherwise. | ||
*/ | ||
export const changeFormatToISODate = (isoDateString: string): string | undefined => { | ||
// preserve the original zone | ||
const date = DateTime.fromISO(isoDateString, { setZone: true, zone: 'utc' }); | ||
return date.isValid ? date.toISODate() : undefined; | ||
}; | ||
|
||
/** | ||
* Prints JS Date instance as ISO date format (no time) | ||
* @param date | ||
* @returns ISO date string if input is valid or undefined otherwise. | ||
*/ | ||
export const toISODate = (date: Date): string => { | ||
const dt = DateTime.fromJSDate(date); | ||
return dt.isValid ? dt.toISODate() : undefined; | ||
}; | ||
|
||
export const isValidDate = (isoDateString: string) => DateTime.fromISO(isoDateString).isValid; | ||
|
||
/** | ||
* | ||
* @param isoDateString | ||
* @returns JS Date instance if input is valid or undefined otherwise. | ||
*/ | ||
export const parseISOtoJSDate = (isoDateString: string) => { | ||
const date = DateTime.fromISO(isoDateString); | ||
return date.isValid ? date.toJSDate() : undefined; | ||
}; | ||
|
||
/** | ||
* | ||
* @param dateTime ISO date time formatted string (with time zone) | ||
* @param calendarDate local date as ISO date formatted string (no time, no time zone) | ||
* @returns true if both dates are on the same day in UTC+00:00 | ||
*/ | ||
export const areSameDayInUTCZero = (dateTime: string, calendarDate: string): boolean => { | ||
// calendar date has no zone - during conversion to UTC the local zone is used | ||
// which results in shifting to previous day for zones with positive offsets | ||
return DateTime.fromISO(dateTime).toUTC().hasSame(DateTime.fromISO(calendarDate), 'day'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!