Skip to content

Commit 3a6d4be

Browse files
committed
fix: avoid locale check at expirty date field, date picker handles locale
1 parent 901fb11 commit 3a6d4be

File tree

2 files changed

+20
-49
lines changed

2 files changed

+20
-49
lines changed

dashboard/src/components/common/date-picker.tsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,26 @@ export interface DatePickerProps {
9696
}
9797

9898
/**
99-
* Helper function to get local ISO time string
99+
* Helper function to get local ISO time string with timezone offset
100100
*/
101101
const getLocalISOTime = (date: Date): string => {
102+
// Create a properly formatted ISO string with timezone offset
103+
const tzOffset = -date.getTimezoneOffset()
104+
const offsetSign = tzOffset >= 0 ? '+' : '-'
105+
const pad = (num: number) => Math.abs(num).toString().padStart(2, '0')
106+
107+
const offsetHours = pad(Math.floor(Math.abs(tzOffset) / 60))
108+
const offsetMinutes = pad(Math.abs(tzOffset) % 60)
109+
110+
// Get the local date/time components without timezone conversion
102111
const year = date.getFullYear()
103-
const month = String(date.getMonth() + 1).padStart(2, '0')
104-
const day = String(date.getDate()).padStart(2, '0')
105-
const hours = String(date.getHours()).padStart(2, '0')
106-
const minutes = String(date.getMinutes()).padStart(2, '0')
107-
const seconds = String(date.getSeconds()).padStart(2, '0')
108-
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`
112+
const month = pad(date.getMonth() + 1)
113+
const day = pad(date.getDate())
114+
const hours = pad(date.getHours())
115+
const minutes = pad(date.getMinutes())
116+
const seconds = pad(date.getSeconds())
117+
118+
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${offsetSign}${offsetHours}:${offsetMinutes}`
109119
}
110120

111121
/**

dashboard/src/components/dialogs/user-modal.tsx

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ const templateModifySchema = z.object({
6565
// Helper for UUID namespace (for v5)
6666
const UUID_NAMESPACE = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'
6767

68-
// Add this helper function at the top level
68+
// Helper function to get local ISO time string with timezone offset
69+
// This is kept for backward compatibility with normalizeExpire function
6970
function getLocalISOTime(date: Date): string {
7071
// Create a properly formatted ISO string with timezone offset
7172
const tzOffset = -date.getTimezoneOffset()
@@ -90,7 +91,6 @@ function getLocalISOTime(date: Date): string {
9091
const ExpiryDateField = ({
9192
field,
9293
displayDate,
93-
usePersianCalendar,
9494
calendarOpen,
9595
setCalendarOpen,
9696
handleFieldChange,
@@ -100,7 +100,6 @@ const ExpiryDateField = ({
100100
}: {
101101
field: any
102102
displayDate: Date | null
103-
usePersianCalendar: boolean
104103
calendarOpen: boolean
105104
setCalendarOpen: (open: boolean) => void
106105
handleFieldChange: (field: string, value: any) => void
@@ -116,6 +115,7 @@ const ExpiryDateField = ({
116115
const handleDateChange = React.useCallback(
117116
(date: Date | undefined) => {
118117
if (date) {
118+
// Use the same logic as centralized DatePicker
119119
const value = useUtcTimestamp ? Math.floor(date.getTime() / 1000) : getLocalISOTime(date)
120120
startTransition(() => {
121121
field.onChange(value)
@@ -152,36 +152,6 @@ const ExpiryDateField = ({
152152
onOpenChange={setCalendarOpen}
153153
fieldName={fieldName}
154154
onFieldChange={handleFieldChange}
155-
formatDate={(date: Date) => {
156-
if (usePersianCalendar) {
157-
return (
158-
date.toLocaleDateString('fa-IR', {
159-
year: 'numeric',
160-
month: '2-digit',
161-
day: '2-digit',
162-
}) +
163-
' ' +
164-
date.toLocaleTimeString('fa-IR', {
165-
hour: '2-digit',
166-
minute: '2-digit',
167-
hour12: false,
168-
})
169-
)
170-
}
171-
return (
172-
date.toLocaleDateString('sv-SE', {
173-
year: 'numeric',
174-
month: '2-digit',
175-
day: '2-digit',
176-
}) +
177-
' ' +
178-
date.toLocaleTimeString('sv-SE', {
179-
hour: '2-digit',
180-
minute: '2-digit',
181-
hour12: false,
182-
})
183-
)
184-
}}
185155
/>
186156
{expireInfo && (
187157
<p className={cn('absolute top-full mt-1 whitespace-nowrap text-xs text-muted-foreground', !expireInfo.time && 'hidden', dir === 'rtl' ? 'right-0' : 'left-0')}>
@@ -302,9 +272,6 @@ export default function UserModal({ isDialogOpen, onOpenChange, form, editingUse
302272
const [selectedTemplateId, setSelectedTemplateId] = useState<number | null>(null)
303273
const [expireCalendarOpen, setExpireCalendarOpen] = useState(false)
304274
const [onHoldCalendarOpen, setOnHoldCalendarOpen] = useState(false)
305-
const { i18n } = useTranslation()
306-
const isPersianLocale = i18n.language === 'fa'
307-
const [usePersianCalendar, setUsePersianCalendar] = useState(isPersianLocale)
308275

309276
// Reset calendar state when modal opens/closes
310277
useEffect(() => {
@@ -1145,10 +1112,6 @@ export default function UserModal({ isDialogOpen, onOpenChange, form, editingUse
11451112
// eslint-disable-next-line
11461113
}, [isDialogOpen, editingUser, generalSettings])
11471114

1148-
// Add effect to handle locale changes
1149-
useEffect(() => {
1150-
setUsePersianCalendar(i18n.language === 'fa')
1151-
}, [i18n.language])
11521115

11531116
return (
11541117
<Dialog open={isDialogOpen} onOpenChange={handleModalOpenChange}>
@@ -1438,7 +1401,6 @@ export default function UserModal({ isDialogOpen, onOpenChange, form, editingUse
14381401
<ExpiryDateField
14391402
field={field}
14401403
displayDate={displayDate}
1441-
usePersianCalendar={usePersianCalendar}
14421404
calendarOpen={expireCalendarOpen}
14431405
setCalendarOpen={setExpireCalendarOpen}
14441406
handleFieldChange={handleFieldChange}
@@ -1459,7 +1421,6 @@ export default function UserModal({ isDialogOpen, onOpenChange, form, editingUse
14591421
<ExpiryDateField
14601422
field={field}
14611423
displayDate={onHoldDisplayDate}
1462-
usePersianCalendar={usePersianCalendar}
14631424
calendarOpen={onHoldCalendarOpen}
14641425
setCalendarOpen={setOnHoldCalendarOpen}
14651426
handleFieldChange={handleFieldChange}

0 commit comments

Comments
 (0)