-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
063f50e
commit ab2101a
Showing
8 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
datahub-web-react/src/app/search/filters/DateRangeMenu/DateRangeMenu.tsx
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,88 @@ | ||
import moment from 'moment'; | ||
import { Text } from '@src/alchemy-components'; | ||
import React, { useCallback, useRef, useState } from 'react'; | ||
import { DatePicker } from 'antd'; | ||
import styled from 'styled-components'; | ||
import { FacetFilterInput, FacetMetadata, FilterOperator } from '@src/types.generated'; | ||
import { useFilterDisplayName } from '../utils'; | ||
import useDateRangeFilterValues, { Datetime } from './useDateRangeFilterValues'; | ||
|
||
const { RangePicker } = DatePicker; | ||
|
||
const Container = styled.div` | ||
padding: 16px; | ||
background-color: #ffffff; | ||
box-shadow: 0 3px 6px -4px rgba(0, 0, 0, 0.12), 0 6px 16px 0 rgba(0, 0, 0, 0.08), 0 9px 28px 8px rgba(0, 0, 0, 0.05); | ||
border-radius: 8px; | ||
min-width: 225px; | ||
`; | ||
|
||
interface Props { | ||
field: FacetMetadata; | ||
manuallyUpdateFilters: (newValues: FacetFilterInput[]) => void; | ||
} | ||
|
||
export default function DateRangeMenu({ field, manuallyUpdateFilters }: Props) { | ||
const displayName = useFilterDisplayName(field); | ||
moment.tz.setDefault('GMT'); | ||
|
||
const [startDate, setStartDate] = useState<Datetime>(null); | ||
const [endDate, setEndDate] = useState<Datetime>(null); | ||
const [isOpen, setIsOpen] = useState<boolean>(false); | ||
const ref = useRef<any>(null); | ||
|
||
useDateRangeFilterValues({ filterField: field.field, setStartDate, setEndDate }); | ||
|
||
const handleOpenChange = useCallback( | ||
(open: boolean) => { | ||
setIsOpen(open); | ||
if (!open) { | ||
ref.current?.blur(); | ||
if (startDate && endDate) { | ||
manuallyUpdateFilters([ | ||
{ | ||
field: field.field, | ||
values: [startDate.valueOf().toString()], | ||
condition: FilterOperator.GreaterThan, | ||
}, | ||
{ | ||
field: field.field, | ||
values: [endDate.valueOf().toString()], | ||
condition: FilterOperator.LessThan, | ||
}, | ||
]); | ||
} | ||
} | ||
}, | ||
[startDate, endDate, field.field, manuallyUpdateFilters], | ||
); | ||
|
||
const handleRangeChange = useCallback((dates: [Datetime, Datetime] | null) => { | ||
const [start, end] = dates || [null, null]; | ||
|
||
start?.set({ hour: 0, minute: 0, second: 0, millisecond: 0 }); | ||
end?.set({ hour: 23, minute: 59, second: 59, millisecond: 999 }); | ||
|
||
setStartDate(start); | ||
setEndDate(end); | ||
}, []); | ||
|
||
return ( | ||
<Container> | ||
<Text weight="bold">Filter by {displayName}</Text> | ||
<RangePicker | ||
ref={ref} | ||
open={isOpen} | ||
allowClear | ||
allowEmpty={[true, true]} | ||
bordered={false} | ||
value={[startDate, endDate]} | ||
format="ll" | ||
onChange={handleRangeChange} | ||
onOpenChange={handleOpenChange} | ||
onCalendarChange={() => handleOpenChange(true)} | ||
style={{ paddingLeft: 0, paddingRight: 0 }} | ||
/> | ||
</Container> | ||
); | ||
} |
48 changes: 48 additions & 0 deletions
48
datahub-web-react/src/app/search/filters/DateRangeMenu/useDateRangeFilterValues.ts
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,48 @@ | ||
import { useEffect, useMemo } from 'react'; | ||
import { FilterOperator } from '@src/types.generated'; | ||
import moment from 'moment'; | ||
import useGetSearchQueryInputs from '../../useGetSearchQueryInputs'; | ||
|
||
export type Datetime = moment.Moment | null; | ||
|
||
interface Props { | ||
filterField: string; | ||
setStartDate: React.Dispatch<React.SetStateAction<Datetime>>; | ||
setEndDate: React.Dispatch<React.SetStateAction<Datetime>>; | ||
} | ||
|
||
export default function useDateRangeFilterValues({ filterField, setStartDate, setEndDate }: Props) { | ||
const { filters: activeFilters } = useGetSearchQueryInputs(); | ||
const matchingFilters = useMemo( | ||
() => activeFilters.filter((f) => f.field === filterField), | ||
[activeFilters, filterField], | ||
); | ||
const startDateFromFilter = useMemo( | ||
() => matchingFilters.find((f) => f.condition === FilterOperator.GreaterThan)?.values?.[0], | ||
[matchingFilters], | ||
); | ||
const endDateFromFilter = useMemo( | ||
() => matchingFilters.find((f) => f.condition === FilterOperator.LessThan)?.values?.[0], | ||
[matchingFilters], | ||
); | ||
|
||
useEffect(() => { | ||
if (startDateFromFilter) { | ||
setStartDate( | ||
moment(parseInt(startDateFromFilter, 10)).set({ hour: 0, minute: 0, second: 0, millisecond: 0 }), | ||
); | ||
} else { | ||
setStartDate(null); | ||
} | ||
}, [startDateFromFilter, setStartDate]); | ||
|
||
useEffect(() => { | ||
if (endDateFromFilter) { | ||
setEndDate( | ||
moment(parseInt(endDateFromFilter, 10)).set({ hour: 23, minute: 59, second: 59, millisecond: 999 }), | ||
); | ||
} else { | ||
setEndDate(null); | ||
} | ||
}, [endDateFromFilter, setEndDate]); | ||
} |
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
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