1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-15 17:50:48 +02:00
unleash.unleash/frontend/src/component/common/ConstraintAccordion/ConstraintAccordionEdit/ConstraintAccordionEditBody/DateSingleValue/DateSingleValue.tsx
andreas-unleash 751b714642
add selectable timezone (#2911)
Signed-off-by: andreas-unleash <andreas@getunleash.ai>

1st Iteration = Show the user the timezone and UTC offset
<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->

## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multipl
![Screenshot 2023-01-27 at 09 46
27](https://user-images.githubusercontent.com/104830839/215034585-d6abe3be-7a13-414c-ada4-b06231ab05a7.png)
e? -->
Closes #
[issue
1-568](https://linear.app/unleash/issue/1-568/show-the-timezone-for-the-currenttime-constraint)

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
Co-authored-by: Thomas Heartman <thomas@getunleash.ai>
2023-01-30 15:36:42 +01:00

80 lines
2.6 KiB
TypeScript

import { ConstraintFormHeader } from '../ConstraintFormHeader/ConstraintFormHeader';
import Input from 'component/common/Input/Input';
import { parseDateValue, parseValidDate } from 'component/common/util';
import { useMemo, useState } from 'react';
import { styled } from '@mui/material';
import TimezoneCountries from 'countries-and-timezones';
interface IDateSingleValueProps {
setValue: (value: string) => void;
value?: string;
error: string;
setError: React.Dispatch<React.SetStateAction<string>>;
}
const StyledWrapper = styled('div')(({ theme }) => ({
display: 'flex',
flexDirection: 'row',
marginBottom: theme.spacing(1),
alignItems: 'center',
gap: theme.spacing(1),
}));
export const DateSingleValue = ({
setValue,
value,
error,
setError,
}: IDateSingleValueProps) => {
const timezones = Object.values(
TimezoneCountries.getAllTimezones({ deprecated: false })
).map(timezone => ({
key: timezone.name,
label: `${timezone.name}`,
utcOffset: timezone.utcOffsetStr,
}));
const { timeZone: localTimezoneName } =
Intl.DateTimeFormat().resolvedOptions();
const [pickedDate, setPickedDate] = useState(value || '');
const timezoneText = useMemo<string>(() => {
const localTimezone = timezones.find(t => t.key === localTimezoneName);
if (localTimezone != null) {
return `${localTimezone.key} (UTC ${localTimezone.utcOffset})`;
} else {
return 'The time shown is in your local time zone according to your browser.';
}
}, [timezones, localTimezoneName]);
if (!value) return null;
return (
<>
<ConstraintFormHeader>Select a date</ConstraintFormHeader>
<StyledWrapper>
<Input
id="date"
label="Date"
type="datetime-local"
value={parseDateValue(pickedDate)}
onChange={e => {
setError('');
const parsedDate = parseValidDate(e.target.value);
const dateString = parsedDate?.toISOString();
dateString && setPickedDate(dateString);
dateString && setValue(dateString);
}}
InputLabelProps={{
shrink: true,
}}
error={Boolean(error)}
errorText={error}
required
/>
<p>{timezoneText}</p>
</StyledWrapper>
</>
);
};