1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-19 17:52:45 +02:00
unleash.unleash/frontend/src/component/common/DateTimePicker/DateTimePicker.tsx
Nuno Góis 93bd9d869a
Variants per environment (frontend) (#2453)
![image](https://user-images.githubusercontent.com/14320932/202286759-b9c30228-59cc-4c58-a7b0-3c6c3d0ecba6.png)
## About the changes

https://linear.app/unleash/issue/2-425/variants-crud-new-environment-cards-with-tables-inside-add-edit-and

Basically created parallel components for the **variants per
environments** feature, so both flows should work correctly depending on
the feature flag state. Some of the duplication means that cleanup
should be straightforward - Once we're happy with this feature it should
be enough to delete
`frontend/src/component/feature/FeatureView/FeatureVariants/FeatureVariantsList`
and do some little extra cleanup.

I noticed we had some legacy-looking code in variants, so this involved
*some* rewriting of the current variants logic. Hopefully this new code
looks nicer, more maintainable, and more importantly **doesn't break
anything**.

Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#2254

### Important files
Everything inside the
`frontend/src/component/feature/FeatureView/FeatureVariants/FeatureEnvironmentVariants`
folder.
2022-11-18 11:43:24 +00:00

65 lines
1.7 KiB
TypeScript

import { INPUT_ERROR_TEXT } from 'utils/testIds';
import { TextField, OutlinedTextFieldProps } from '@mui/material';
import { parseValidDate } from '../util';
import { format } from 'date-fns';
interface IDateTimePickerProps extends Omit<OutlinedTextFieldProps, 'variant'> {
label: string;
type?: 'date' | 'datetime';
error?: boolean;
errorText?: string;
min?: Date;
max?: Date;
value: Date;
onChange: (e: any) => any;
}
export const formatDate = (value: string) => {
const date = new Date(value);
return format(date, 'yyyy-MM-dd');
};
export const formatDateTime = (value: string) => {
const date = new Date(value);
return format(date, 'yyyy-MM-dd') + 'T' + format(date, 'HH:mm');
};
export const DateTimePicker = ({
label,
type = 'datetime',
error,
errorText,
min,
max,
value,
onChange,
...rest
}: IDateTimePickerProps) => {
const getDate = type === 'datetime' ? formatDateTime : formatDate;
const inputType = type === 'datetime' ? 'datetime-local' : 'date';
return (
<TextField
type={inputType}
size="small"
variant="outlined"
label={label}
error={error}
helperText={errorText}
value={getDate(value.toISOString())}
onChange={e => {
const parsedDate = parseValidDate(e.target.value);
onChange(parsedDate ?? value);
}}
FormHelperTextProps={{
['data-testid']: INPUT_ERROR_TEXT,
}}
inputProps={{
min: min ? getDate(min.toISOString()) : min,
max: max ? getDate(max.toISOString()) : max,
}}
{...rest}
/>
);
};