TimeOnly заменено на TimeDto

This commit is contained in:
goodmice 2022-06-01 14:05:54 +05:00
parent c768414421
commit 5b5d135bec
3 changed files with 17 additions and 19 deletions

View File

@ -2,25 +2,25 @@ import { Moment } from 'moment'
import { TimePicker, TimePickerProps } from 'antd'
import { memo, useCallback, useMemo } from 'react'
import { defaultTimeFormat, momentToTimeOnly, timeOnlyToMoment } from '@utils/datetime'
import { TimeOnly } from '@api'
import { defaultTimeFormat, momentToTime, timeToMoment } from '@utils/datetime'
import { TimeDto } from '@api'
export type TimePickerWrapperProps = Omit<Omit<TimePickerProps, 'value'>, 'onChange'> & {
value?: TimeOnly,
onChange?: (date: TimeOnly | null) => any
value?: TimeDto,
onChange?: (date: TimeDto | null) => any
isUTC?: boolean
}
export const TimePickerWrapper = memo<TimePickerWrapperProps>(({ value, onChange, isUTC, ...other }) => {
const time = useMemo(() => value ? timeOnlyToMoment(value, isUTC) : null, [value, isUTC])
const time = useMemo(() => value ? timeToMoment(value, isUTC) : null, [value, isUTC])
const onTimeChange = useCallback((time: Moment | null) => onChange?.(time ? momentToTimeOnly(time) : null), [onChange])
const onTimeChange = useCallback((time: Moment | null) => onChange?.(time ? momentToTime(time) : null), [onChange])
return (
<TimePicker
allowClear={false}
format={defaultTimeFormat}
defaultValue={timeOnlyToMoment()}
defaultValue={timeToMoment()}
onChange={onTimeChange}
value={time}
{...other}

View File

@ -1,6 +1,6 @@
import { timeOnlyToMoment } from '@utils/datetime'
import { timeToMoment } from '@utils/datetime'
import { isRawDate } from '@utils'
import { TimeOnly } from '@api'
import { TimeDto } from '@api'
import { DataType } from './Columns'
@ -26,7 +26,7 @@ export const makeDateSorter = <T extends unknown>(key: keyof DataType<T>) => (a:
return date.getTime() - new Date(bdate).getTime()
}
export const makeTimeSorter = <T extends TimeOnly>(key: keyof DataType<T>) => (a: DataType<T>, b: DataType<T>) => {
export const makeTimeSorter = <T extends TimeDto>(key: keyof DataType<T>) => (a: DataType<T>, b: DataType<T>) => {
const elma = a[key]
const elmb = b[key]
@ -34,5 +34,5 @@ export const makeTimeSorter = <T extends TimeOnly>(key: keyof DataType<T>) => (a
if (!elma) return 1
if (!elmb) return -1
return timeOnlyToMoment(elma).diff(timeOnlyToMoment(elmb))
return timeToMoment(elma).diff(timeToMoment(elmb))
}

View File

@ -1,6 +1,6 @@
import moment, { Moment } from 'moment'
import { SimpleTimezoneDto, TimeOnly } from '@api'
import { SimpleTimezoneDto, TimeDto } from '@api'
export type RawDate = number | string | Date
@ -20,7 +20,7 @@ export function isRawDate(value: unknown): value is RawDate {
return !isNaN(Date.parse(String(value)))
}
export function isTimeOnly(value: unknown): value is TimeOnly {
export function isTime(value: unknown): value is TimeDto {
if (!value || typeof value !== 'object')
return false
const keys = Object.keys(value)
@ -34,8 +34,8 @@ export const formatDate = (date: unknown, utc: boolean = false, format: string =
}
export const formatTime = (time: unknown, utc: boolean = false, format: string = defaultTimeFormat) => {
if(!isTimeOnly(time)) return
const out = timeOnlyToMoment(time, utc, format)
if(!isTime(time)) return
const out = timeToMoment(time, utc, format)
return out.format(format)
}
@ -92,15 +92,13 @@ export const findTimezoneId = (value: SimpleTimezoneDto): TimezoneId =>
(isTimezoneId(value.timezoneId) && value.timezoneId) ||
(Object.keys(rawTimezones) as TimezoneId[]).find(id => rawTimezones[id] === value.hours) as TimezoneId
export const timeOnlyToMoment = (time?: TimeOnly | null, isUtc?: boolean, format: string = defaultTimeFormat): Moment => {
export const timeToMoment = (time?: TimeDto | null, isUtc?: boolean, format: string = defaultTimeFormat): Moment => {
const input = `${time?.hour ?? 0}:${time?.minute ?? 0}:${time?.second ?? 0}`
return isUtc ? moment.utc(input, format).local() : moment(input, format)
}
export const momentToTimeOnly = (time?: Moment | null): TimeOnly => ({
export const momentToTime = (time?: Moment | null): TimeDto => ({
hour: time?.hour() ?? 0,
minute: time?.minute() ?? 0,
second: time?.second() ?? 0,
millisecond: 0,
ticks: 0,
})