2021-06-30 01:35:08 +00:00
|
|
|
import { Switch } from "@headlessui/react";
|
2021-06-20 14:19:41 +00:00
|
|
|
import TimezoneSelect from "react-timezone-select";
|
2021-06-30 01:35:08 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2021-07-07 10:43:13 +00:00
|
|
|
import { is24h, timeZone } from "../../lib/clock";
|
2021-08-03 08:30:35 +00:00
|
|
|
import classNames from "@lib/classNames";
|
2021-06-20 14:19:41 +00:00
|
|
|
|
|
|
|
const TimeOptions = (props) => {
|
2021-06-30 01:35:08 +00:00
|
|
|
const [selectedTimeZone, setSelectedTimeZone] = useState("");
|
2021-06-20 14:19:41 +00:00
|
|
|
const [is24hClock, setIs24hClock] = useState(false);
|
|
|
|
|
2021-06-30 01:35:08 +00:00
|
|
|
useEffect(() => {
|
2021-06-20 14:19:41 +00:00
|
|
|
setIs24hClock(is24h());
|
|
|
|
setSelectedTimeZone(timeZone());
|
|
|
|
}, []);
|
|
|
|
|
2021-06-30 01:35:08 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (selectedTimeZone && timeZone() && selectedTimeZone !== timeZone()) {
|
|
|
|
props.onSelectTimeZone(timeZone(selectedTimeZone));
|
|
|
|
}
|
2021-06-20 14:19:41 +00:00
|
|
|
}, [selectedTimeZone]);
|
|
|
|
|
2021-08-19 12:36:17 +00:00
|
|
|
const handle24hClockToggle = (is24hClock: boolean) => {
|
|
|
|
setIs24hClock(is24hClock);
|
2021-06-20 14:19:41 +00:00
|
|
|
props.onToggle24hClock(is24h(is24hClock));
|
2021-08-19 12:36:17 +00:00
|
|
|
};
|
2021-06-20 14:19:41 +00:00
|
|
|
|
2021-06-30 01:35:08 +00:00
|
|
|
return (
|
|
|
|
selectedTimeZone !== "" && (
|
2021-08-12 14:29:48 +00:00
|
|
|
<div className="absolute z-10 w-full max-w-80 rounded-sm border border-gray-200 dark:bg-gray-700 dark:border-0 bg-white px-4 py-2">
|
2021-06-30 01:35:08 +00:00
|
|
|
<div className="flex mb-4">
|
2021-07-07 19:58:18 +00:00
|
|
|
<div className="w-1/2 dark:text-white text-gray-600 font-medium">Time Options</div>
|
2021-06-30 01:35:08 +00:00
|
|
|
<div className="w-1/2">
|
|
|
|
<Switch.Group as="div" className="flex items-center justify-end">
|
|
|
|
<Switch.Label as="span" className="mr-3">
|
2021-07-07 19:58:18 +00:00
|
|
|
<span className="text-sm dark:text-white text-gray-500">am/pm</span>
|
2021-06-30 01:35:08 +00:00
|
|
|
</Switch.Label>
|
|
|
|
<Switch
|
|
|
|
checked={is24hClock}
|
2021-08-19 12:36:17 +00:00
|
|
|
onChange={handle24hClockToggle}
|
2021-06-20 14:19:41 +00:00
|
|
|
className={classNames(
|
2021-08-08 15:13:31 +00:00
|
|
|
is24hClock ? "bg-black" : "dark:bg-gray-600 bg-gray-200",
|
2021-08-02 15:24:01 +00:00
|
|
|
"relative inline-flex flex-shrink-0 h-5 w-8 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-black"
|
2021-06-30 01:35:08 +00:00
|
|
|
)}>
|
|
|
|
<span className="sr-only">Use setting</span>
|
|
|
|
<span
|
|
|
|
aria-hidden="true"
|
|
|
|
className={classNames(
|
|
|
|
is24hClock ? "translate-x-3" : "translate-x-0",
|
|
|
|
"pointer-events-none inline-block h-4 w-4 rounded-full bg-white shadow transform ring-0 transition ease-in-out duration-200"
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</Switch>
|
|
|
|
<Switch.Label as="span" className="ml-3">
|
2021-07-07 19:58:18 +00:00
|
|
|
<span className="text-sm dark:text-white text-gray-500">24h</span>
|
2021-06-30 01:35:08 +00:00
|
|
|
</Switch.Label>
|
|
|
|
</Switch.Group>
|
|
|
|
</div>
|
2021-06-20 14:19:41 +00:00
|
|
|
</div>
|
2021-06-30 01:35:08 +00:00
|
|
|
<TimezoneSelect
|
|
|
|
id="timeZone"
|
|
|
|
value={selectedTimeZone}
|
|
|
|
onChange={(tz) => setSelectedTimeZone(tz.value)}
|
2021-08-02 15:24:01 +00:00
|
|
|
className="mb-2 shadow-sm focus:ring-black focus:border-black mt-1 block w-full sm:text-sm border-gray-300 rounded-md"
|
2021-06-30 01:35:08 +00:00
|
|
|
/>
|
2021-06-20 14:19:41 +00:00
|
|
|
</div>
|
2021-06-30 01:35:08 +00:00
|
|
|
)
|
2021-06-20 14:19:41 +00:00
|
|
|
);
|
2021-06-30 01:35:08 +00:00
|
|
|
};
|
2021-06-20 14:19:41 +00:00
|
|
|
|
2021-06-30 01:35:08 +00:00
|
|
|
export default TimeOptions;
|