|
| 1 | +import { useCallback, useState } from "react"; |
| 2 | +import { getLocalTimeZone, DateValue } from "@internationalized/date"; |
| 3 | +import { CommitHandler, toCalendarDate } from "@vuu-ui/vuu-utils"; |
| 4 | +import { NumericFilterClauseOp } from "@vuu-ui/vuu-filter-types"; |
| 5 | +import { FilterClauseValueEditor } from "../filterClauseTypes"; |
| 6 | +import { VuuTimePicker } from "@vuu-ui/vuu-ui-controls"; |
| 7 | + |
| 8 | +interface FilterClauseValueEditorTimeProps |
| 9 | + extends Pick<FilterClauseValueEditor, "onChangeValue" | "inputProps"> { |
| 10 | + className?: string; |
| 11 | + value: number | undefined; |
| 12 | + operator: NumericFilterClauseOp; |
| 13 | +} |
| 14 | + |
| 15 | +export const FilterClauseValueEditorTime = ( |
| 16 | + props: FilterClauseValueEditorTimeProps, |
| 17 | +) => { |
| 18 | + const { inputProps, className, onChangeValue, operator, value } = props; |
| 19 | + const toEpochMilliS = getEpochMillisConverter(operator); |
| 20 | + |
| 21 | + const [date, setDate] = useState<DateValue | undefined>(() => |
| 22 | + getInitialState(value), |
| 23 | + ); |
| 24 | + |
| 25 | + const handleCommit = useCallback<CommitHandler<HTMLElement, number>>( |
| 26 | + (e, selectedDateInputValue) => { |
| 27 | + console.log("change time"); |
| 28 | + if (selectedDateInputValue) { |
| 29 | + const dateValue = toCalendarDate(new Date(selectedDateInputValue)); |
| 30 | + setDate(dateValue); |
| 31 | + if (selectedDateInputValue /* && source === "calendar"*/) { |
| 32 | + onChangeValue(toEpochMilliS(dateValue)); |
| 33 | + } |
| 34 | + } |
| 35 | + }, |
| 36 | + [onChangeValue, toEpochMilliS], |
| 37 | + ); |
| 38 | + |
| 39 | + const onBlur = useCallback(() => { |
| 40 | + date && onChangeValue(toEpochMilliS(date)); |
| 41 | + }, [date, onChangeValue, toEpochMilliS]); |
| 42 | + |
| 43 | + return ( |
| 44 | + <VuuTimePicker |
| 45 | + data-field="value" |
| 46 | + inputProps={inputProps} |
| 47 | + className={className} |
| 48 | + onBlur={onBlur} |
| 49 | + onCommit={handleCommit} |
| 50 | + selectedDate={date} |
| 51 | + /> |
| 52 | + ); |
| 53 | +}; |
| 54 | + |
| 55 | +function getInitialState(value: FilterClauseValueEditorTimeProps["value"]) { |
| 56 | + return value ? toCalendarDate(new Date(value)) : undefined; |
| 57 | +} |
| 58 | + |
| 59 | +const getEpochMillisConverter = |
| 60 | + (op: NumericFilterClauseOp) => |
| 61 | + (date: DateValue, timezone: string = getLocalTimeZone()): number => { |
| 62 | + const d = date.toDate(timezone); |
| 63 | + switch (op) { |
| 64 | + case ">": |
| 65 | + case "<=": |
| 66 | + d.setHours(23, 59, 59, 999); |
| 67 | + return d.getTime(); |
| 68 | + case ">=": |
| 69 | + case "<": |
| 70 | + case "=": // converted to "< `start of next day` and >= `start of this day`" when query is created |
| 71 | + case "!=": // converted to ">= `start of next day` or < `start of this day`" when query is created |
| 72 | + d.setHours(0, 0, 0, 0); |
| 73 | + return d.getTime(); |
| 74 | + } |
| 75 | + }; |
0 commit comments