|
| 1 | +import './dateRange.scss'; |
| 2 | + |
| 3 | +import type { MessageDescriptor } from '@formatjs/intl/src/types'; |
| 4 | +import type { MenuToggleElement } from '@patternfly/react-core'; |
| 5 | +import { Dropdown, DropdownItem, DropdownList, MenuToggle } from '@patternfly/react-core'; |
| 6 | +import messages from 'locales/messages'; |
| 7 | +import React from 'react'; |
| 8 | +import { useIntl } from 'react-intl'; |
| 9 | +import { getSinceDateRangeString } from 'utils/dates'; |
| 10 | + |
| 11 | +interface DateRangeOwnProps { |
| 12 | + dateRangeType?: string; |
| 13 | + isCurrentMonthData: boolean; |
| 14 | + isDisabled?: boolean; |
| 15 | + isExplorer?: boolean; |
| 16 | + onSelect(value: string); |
| 17 | +} |
| 18 | + |
| 19 | +type DateRangeProps = DateRangeOwnProps; |
| 20 | + |
| 21 | +const DateRange: React.FC<DateRangeProps> = ({ dateRangeType, isCurrentMonthData, isExplorer, onSelect }) => { |
| 22 | + const [isOpen, setIsOpen] = React.useState(false); |
| 23 | + const intl = useIntl(); |
| 24 | + |
| 25 | + const onToggleClick = () => { |
| 26 | + setIsOpen(!isOpen); |
| 27 | + }; |
| 28 | + |
| 29 | + const getOptions = () => { |
| 30 | + const options: { |
| 31 | + isDisabled?: boolean; |
| 32 | + label: MessageDescriptor; |
| 33 | + value: string; |
| 34 | + }[] = [ |
| 35 | + { label: messages.explorerDateRange, value: 'current_month_to_date', isDisabled: isCurrentMonthData === false }, |
| 36 | + { label: messages.explorerDateRange, value: 'previous_month' }, |
| 37 | + ]; |
| 38 | + if (isExplorer) { |
| 39 | + options.push( |
| 40 | + { label: messages.explorerDateRange, value: 'previous_month_to_date' }, |
| 41 | + { label: messages.explorerDateRange, value: 'last_thirty_days' }, |
| 42 | + { label: messages.explorerDateRange, value: 'last_sixty_days' }, |
| 43 | + { label: messages.explorerDateRange, value: 'last_ninety_days' }, |
| 44 | + { label: messages.explorerDateRange, value: 'custom' } |
| 45 | + ); |
| 46 | + } |
| 47 | + return options; |
| 48 | + }; |
| 49 | + |
| 50 | + const handleOnSelect = (_evt, value) => { |
| 51 | + if (onSelect) { |
| 52 | + onSelect(value); |
| 53 | + } |
| 54 | + setIsOpen(false); |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <div className="dropdownOverride"> |
| 59 | + <Dropdown |
| 60 | + isOpen={isOpen} |
| 61 | + onSelect={handleOnSelect} |
| 62 | + onOpenChange={(val: boolean) => setIsOpen(val)} |
| 63 | + toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( |
| 64 | + <MenuToggle ref={toggleRef} onClick={onToggleClick} isExpanded={isOpen} isFullWidth> |
| 65 | + {intl.formatMessage(messages.explorerDateRange, { value: dateRangeType })} |
| 66 | + </MenuToggle> |
| 67 | + )} |
| 68 | + > |
| 69 | + <DropdownList> |
| 70 | + {getOptions().map((option, index) => ( |
| 71 | + <DropdownItem |
| 72 | + value={option.value} |
| 73 | + isAriaDisabled={option.isDisabled} |
| 74 | + key={index} |
| 75 | + tooltipProps={ |
| 76 | + option.isDisabled |
| 77 | + ? { |
| 78 | + content: intl.formatMessage(messages.noDataForDate, { |
| 79 | + dateRange: getSinceDateRangeString(undefined, option.value === 'previous_month' ? 1 : 0, true), |
| 80 | + }), |
| 81 | + position: 'right', |
| 82 | + } |
| 83 | + : undefined |
| 84 | + } |
| 85 | + > |
| 86 | + {intl.formatMessage(option.label, { value: option.value })} |
| 87 | + </DropdownItem> |
| 88 | + ))} |
| 89 | + </DropdownList> |
| 90 | + </Dropdown> |
| 91 | + </div> |
| 92 | + ); |
| 93 | +}; |
| 94 | + |
| 95 | +export { DateRange }; |
0 commit comments