mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-09 00:18:00 +01:00
feat: support localization in date filter (#5572)
This commit is contained in:
parent
b6f1929efb
commit
166432bcb0
@ -4,8 +4,10 @@ import { StyledPopover } from '../FilterItem/FilterItem.styles';
|
|||||||
import { FilterItemChip } from '../FilterItem/FilterItemChip/FilterItemChip';
|
import { FilterItemChip } from '../FilterItem/FilterItemChip/FilterItemChip';
|
||||||
import { FilterItem } from '../FilterItem/FilterItem';
|
import { FilterItem } from '../FilterItem/FilterItem';
|
||||||
import { DateCalendar, LocalizationProvider } from '@mui/x-date-pickers';
|
import { DateCalendar, LocalizationProvider } from '@mui/x-date-pickers';
|
||||||
import { parseISO, format } from 'date-fns';
|
|
||||||
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
|
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
|
||||||
|
import { format } from 'date-fns';
|
||||||
|
import { useLocationSettings } from 'hooks/useLocationSettings';
|
||||||
|
import { getLocalizedDateString } from '../util';
|
||||||
|
|
||||||
interface IFilterDateItemProps {
|
interface IFilterDateItemProps {
|
||||||
label: string;
|
label: string;
|
||||||
@ -24,7 +26,7 @@ export const FilterDateItem: FC<IFilterDateItemProps> = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
const [anchorEl, setAnchorEl] = useState<HTMLDivElement | null>(null);
|
const [anchorEl, setAnchorEl] = useState<HTMLDivElement | null>(null);
|
||||||
const [searchText, setSearchText] = useState('');
|
const { locationSettings } = useLocationSettings();
|
||||||
|
|
||||||
const onClick = () => {
|
const onClick = () => {
|
||||||
setAnchorEl(ref.current);
|
setAnchorEl(ref.current);
|
||||||
@ -34,10 +36,16 @@ export const FilterDateItem: FC<IFilterDateItemProps> = ({
|
|||||||
setAnchorEl(null);
|
setAnchorEl(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const selectedOptions = state ? state.values : [];
|
const selectedOptions = state
|
||||||
|
? [
|
||||||
|
getLocalizedDateString(
|
||||||
|
state.values[0],
|
||||||
|
locationSettings.locale,
|
||||||
|
) || '',
|
||||||
|
]
|
||||||
|
: [];
|
||||||
const selectedDate = state ? new Date(state.values[0]) : null;
|
const selectedDate = state ? new Date(state.values[0]) : null;
|
||||||
const currentOperator = state ? state.operator : operators[0];
|
const currentOperator = state ? state.operator : operators[0];
|
||||||
|
|
||||||
const onDelete = () => {
|
const onDelete = () => {
|
||||||
onChange(undefined);
|
onChange(undefined);
|
||||||
onClose();
|
onClose();
|
||||||
@ -45,7 +53,7 @@ export const FilterDateItem: FC<IFilterDateItemProps> = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const setValue = (value: Date | null) => {
|
const setValue = (value: Date | null) => {
|
||||||
const formattedValue = value ? format(value, 'MM/dd/yyyy') : '';
|
const formattedValue = value ? format(value, 'yyyy-MM-dd') : '';
|
||||||
onChange({ operator: currentOperator, values: [formattedValue] });
|
onChange({ operator: currentOperator, values: [formattedValue] });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import { VFC } from 'react';
|
import { VFC } from 'react';
|
||||||
import { useLocationSettings } from 'hooks/useLocationSettings';
|
import { useLocationSettings } from 'hooks/useLocationSettings';
|
||||||
import { formatDateYMD } from 'utils/formatDate';
|
|
||||||
import { parseISO } from 'date-fns';
|
|
||||||
import { TextCell } from 'component/common/Table/cells/TextCell/TextCell';
|
import { TextCell } from 'component/common/Table/cells/TextCell/TextCell';
|
||||||
|
import { getLocalizedDateString } from '../../../util';
|
||||||
|
|
||||||
interface IDateCellProps {
|
interface IDateCellProps {
|
||||||
value?: Date | string | null;
|
value?: Date | string | null;
|
||||||
@ -10,12 +9,7 @@ interface IDateCellProps {
|
|||||||
|
|
||||||
export const DateCell: VFC<IDateCellProps> = ({ value }) => {
|
export const DateCell: VFC<IDateCellProps> = ({ value }) => {
|
||||||
const { locationSettings } = useLocationSettings();
|
const { locationSettings } = useLocationSettings();
|
||||||
|
const date = getLocalizedDateString(value, locationSettings.locale);
|
||||||
const date = value
|
|
||||||
? value instanceof Date
|
|
||||||
? formatDateYMD(value, locationSettings.locale)
|
|
||||||
: formatDateYMD(parseISO(value), locationSettings.locale)
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
return <TextCell lineClamp={1}>{date}</TextCell>;
|
return <TextCell lineClamp={1}>{date}</TextCell>;
|
||||||
};
|
};
|
||||||
|
@ -2,8 +2,9 @@ import { weightTypes } from 'constants/weightTypes';
|
|||||||
import { IUiConfig } from 'interfaces/uiConfig';
|
import { IUiConfig } from 'interfaces/uiConfig';
|
||||||
import { INavigationMenuItem } from 'interfaces/route';
|
import { INavigationMenuItem } from 'interfaces/route';
|
||||||
import { IFeatureVariant } from 'interfaces/featureToggle';
|
import { IFeatureVariant } from 'interfaces/featureToggle';
|
||||||
import { format, isValid } from 'date-fns';
|
import { format, isValid, parseISO } from 'date-fns';
|
||||||
import { IFeatureVariantEdit } from 'component/feature/FeatureView/FeatureVariants/FeatureEnvironmentVariants/EnvironmentVariantsModal/EnvironmentVariantsModal';
|
import { IFeatureVariantEdit } from 'component/feature/FeatureView/FeatureVariants/FeatureEnvironmentVariants/EnvironmentVariantsModal/EnvironmentVariantsModal';
|
||||||
|
import { formatDateYMD } from '../../utils/formatDate';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle feature flags and configuration for different plans.
|
* Handle feature flags and configuration for different plans.
|
||||||
@ -48,6 +49,18 @@ export const trim = (value: string): string => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getLocalizedDateString = (
|
||||||
|
value: Date | string | null | undefined,
|
||||||
|
locale: string,
|
||||||
|
) => {
|
||||||
|
const date = value
|
||||||
|
? value instanceof Date
|
||||||
|
? formatDateYMD(value, locale)
|
||||||
|
: formatDateYMD(parseISO(value), locale)
|
||||||
|
: undefined;
|
||||||
|
return date;
|
||||||
|
};
|
||||||
|
|
||||||
export const parseDateValue = (value: string) => {
|
export const parseDateValue = (value: string) => {
|
||||||
const date = new Date(value);
|
const date = new Date(value);
|
||||||
return `${format(date, 'yyyy-MM-dd')}T${format(date, 'HH:mm')}`;
|
return `${format(date, 'yyyy-MM-dd')}T${format(date, 'HH:mm')}`;
|
||||||
|
@ -829,13 +829,13 @@ test('should search features by created date with operators', async () => {
|
|||||||
createdAt: '2023-01-29T15:21:39.975Z',
|
createdAt: '2023-01-29T15:21:39.975Z',
|
||||||
});
|
});
|
||||||
|
|
||||||
const { body } = await filterFeaturesByCreated('IS_BEFORE:01/28/2023');
|
const { body } = await filterFeaturesByCreated('IS_BEFORE:2023-01-28');
|
||||||
expect(body).toMatchObject({
|
expect(body).toMatchObject({
|
||||||
features: [{ name: 'my_feature_a' }],
|
features: [{ name: 'my_feature_a' }],
|
||||||
});
|
});
|
||||||
|
|
||||||
const { body: afterBody } = await filterFeaturesByCreated(
|
const { body: afterBody } = await filterFeaturesByCreated(
|
||||||
'IS_ON_OR_AFTER:01/28/2023',
|
'IS_ON_OR_AFTER:2023-01-28',
|
||||||
);
|
);
|
||||||
expect(afterBody).toMatchObject({
|
expect(afterBody).toMatchObject({
|
||||||
features: [{ name: 'my_feature_b' }],
|
features: [{ name: 'my_feature_b' }],
|
||||||
@ -861,7 +861,7 @@ test('should filter features by combined operators', async () => {
|
|||||||
const { body } = await filterFeaturesByOperators(
|
const { body } = await filterFeaturesByOperators(
|
||||||
'IS_NOT:active',
|
'IS_NOT:active',
|
||||||
'DO_NOT_INCLUDE:simple:my_tag',
|
'DO_NOT_INCLUDE:simple:my_tag',
|
||||||
'IS_BEFORE:01/28/2023',
|
'IS_BEFORE:2023-01-28',
|
||||||
);
|
);
|
||||||
expect(body).toMatchObject({
|
expect(body).toMatchObject({
|
||||||
features: [{ name: 'my_feature_a' }],
|
features: [{ name: 'my_feature_a' }],
|
||||||
|
@ -137,8 +137,8 @@ export const featureSearchQueryParameters = [
|
|||||||
name: 'createdAt',
|
name: 'createdAt',
|
||||||
schema: {
|
schema: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
example: 'IS_ON_OR_AFTER:28/01/2023',
|
example: 'IS_ON_OR_AFTER:2023-01-28',
|
||||||
pattern: '^(IS_BEFORE|IS_ON_OR_AFTER):\\d{2}\\/\\d{2}\\/\\d{4}$',
|
pattern: '^(IS_BEFORE|IS_ON_OR_AFTER):\\d{4}-\\d{2}-\\d{2}$',
|
||||||
},
|
},
|
||||||
description:
|
description:
|
||||||
'The date the feature was created. The date can be specified with an operator. The supported operators are IS_BEFORE, IS_ON_OR_AFTER.',
|
'The date the feature was created. The date can be specified with an operator. The supported operators are IS_BEFORE, IS_ON_OR_AFTER.',
|
||||||
|
Loading…
Reference in New Issue
Block a user