Skip to content

Commit

Permalink
bug(datepicker): fix subscription date default timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
ansmonjol committed Jun 19, 2023
1 parent 7b05264 commit 801499c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/components/customers/subscriptions/AddSubscriptionDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ export const AddSubscriptionDrawer = forwardRef<
if (formikProps.values.subscriptionAt) {
if (!!customerTimezone) {
const date = DateTime.fromISO(formikProps.values.subscriptionAt)
.setZone(GMT)
.setZone(customerTimezoneConfig.name)
.toFormat('LLL. dd, yyyy')
const time = DateTime.fromISO(formikProps.values.subscriptionAt)
const time = `${DateTime.fromISO(formikProps.values.subscriptionAt)
.setZone(customerTimezoneConfig.name)
.toFormat('T')} ${DateTime.fromISO(formikProps.values.subscriptionAt)
.setZone(customerTimezoneConfig.name)
.toFormat('t')
.toFormat('a')}`
const offset = TimeZonesConfig[customerTimezone].offset

if (customerTimezoneConfig?.offsetInMinute < 0) {
Expand All @@ -162,11 +164,13 @@ export const AddSubscriptionDrawer = forwardRef<
}
} else if (!!organizationTimezone) {
const date = DateTime.fromISO(formikProps.values.subscriptionAt)
.setZone(GMT)
.setZone(orgaTimezoneConfig.name)
.toFormat('LLL. dd, yyyy')
const time = DateTime.fromISO(formikProps.values.subscriptionAt)
const time = `${DateTime.fromISO(formikProps.values.subscriptionAt)
.setZone(orgaTimezoneConfig.name)
.toFormat('T')} ${DateTime.fromISO(formikProps.values.subscriptionAt)
.setZone(orgaTimezoneConfig.name)
.toFormat('t')
.toFormat('a')}`
const offset = TimeZonesConfig[organizationTimezone].offset

if (orgaTimezoneConfig.offsetInMinute < 0) {
Expand All @@ -181,7 +185,6 @@ export const AddSubscriptionDrawer = forwardRef<
}, [
organizationTimezone,
customerTimezone,
GMT,
customerTimezoneConfig,
formikProps.values.subscriptionAt,
orgaTimezoneConfig,
Expand Down Expand Up @@ -291,11 +294,13 @@ export const AddSubscriptionDrawer = forwardRef<
<DatePickerField
name="subscriptionAt"
label={translate('text_648b1828ead1c3004b930334')}
defaultZone={getTimezoneConfig(TimezoneEnum.TzUtc).name}
formikProps={formikProps}
/>
<TimePickerField
name="subscriptionAt"
label={translate('text_648b1837da6496008dfe4b3c')}
defaultZone={getTimezoneConfig(TimezoneEnum.TzUtc).name}
formikProps={formikProps}
/>
</InlineFields>
Expand Down
13 changes: 12 additions & 1 deletion src/components/form/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon'
import { DatePicker as MuiDatePicker } from '@mui/x-date-pickers/DatePicker'
import { PopperProps as MuiPopperProps } from '@mui/material'
import { DateTime } from 'luxon'
import { DateTime, Settings } from 'luxon'
import styled from 'styled-components'
import _omit from 'lodash/omit'

Expand All @@ -25,6 +25,7 @@ export interface DatePickerProps
placeholder?: string
error?: string
helperText?: string
defaultZone?: string // Overrides the default timezone of the date picker
disableFuture?: boolean
disablePast?: boolean
placement?: MuiPopperProps['placement']
Expand All @@ -40,6 +41,7 @@ export const DatePicker = ({
className,
value,
error,
defaultZone,
disableFuture,
disablePast,
placeholder,
Expand All @@ -60,6 +62,15 @@ export const DatePicker = ({
const isInvalid = !!localDate && !localDate.isValid
const { translate } = useInternationalization()

useEffect(() => {
if (defaultZone) Settings.defaultZone = defaultZone

return () => {
if (defaultZone) Settings.defaultZone = defaultZone
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

useEffect(() => {
setLocalDate(!!value ? (typeof value === 'string' ? DateTime.fromISO(value) : value) : null)
}, [value])
Expand Down
13 changes: 12 additions & 1 deletion src/components/form/TimePicker/TimePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useEffect } from 'react'
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon'
import { DateTime } from 'luxon'
import { DateTime, Settings } from 'luxon'
import _omit from 'lodash/omit'
import { TimePicker as MuiTimePicker } from '@mui/x-date-pickers/TimePicker'

Expand All @@ -21,6 +21,7 @@ export interface TimePickerProps
placeholder?: string
error?: string
helperText?: string
defaultZone?: string // Overrides the default timezone of the date picker
onError?: (err: keyof typeof TIME_PICKER_ERROR_ENUM | undefined) => void
onChange: (value?: string | null) => void
}
Expand All @@ -29,6 +30,7 @@ export const TimePicker = ({
className,
value,
error,
defaultZone,
placeholder,
disabled,
onError,
Expand All @@ -45,6 +47,15 @@ export const TimePicker = ({
)
const isInvalid = !!localTime && !localTime.isValid

useEffect(() => {
if (defaultZone) Settings.defaultZone = defaultZone

return () => {
if (defaultZone) Settings.defaultZone = defaultZone
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

useEffect(() => {
setLocalTime(!!value ? (typeof value === 'string' ? DateTime.fromISO(value) : value) : null)
}, [value])
Expand Down

0 comments on commit 801499c

Please sign in to comment.