Skip to content

Commit

Permalink
fix: fix property type 'date' when backend returns an iso string
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Sep 18, 2024
1 parent ca204b9 commit de86636
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/frontend/components/property-type/datetime/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import { PropertyLabel } from '../utils/property-label/index.js'
import allowOverride from '../../../hoc/allow-override.js'
import { useTranslation } from '../../../hooks/index.js'
import { PropertyType } from '../../../../backend/index.js'
import { stripTimeFromISO } from './strip-time-from-iso.js'

const formatDate = (val:string|null, propertyType: PropertyType) => {
if (val) return (propertyType === 'date' ? `${val}T00:00:00` : val)
return ''
const formatDate = (date: string | Date | null, propertyType: PropertyType) => {
if (!date) return ''

if (propertyType !== 'date') return date

return `${stripTimeFromISO(date)}T00:00:00`
}

const Edit: React.FC<EditPropertyProps> = (props) => {
Expand All @@ -26,7 +30,10 @@ const Edit: React.FC<EditPropertyProps> = (props) => {
value={value}
disabled={property.isDisabled}
onChange={(date) => {
onChange(property.path, property.type === 'date' ? date?.substring(0, 10) ?? date : date)
onChange(
property.path,
property.type === 'date' ? stripTimeFromISO(date) ?? date : date,
)
}}
propertyType={property.type}
{...property.props}
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/components/property-type/datetime/map-value.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { formatDateProperty } from '@adminjs/design-system'

import { PropertyType } from '../../../../backend/adapters/property/base-property.js'
import { stripTimeFromISO } from './strip-time-from-iso.js'

export default (value: Date, propertyType: PropertyType): string => {
if (!value) {
return ''
}
const date = propertyType === 'date' ? new Date(`${value}T00:00:00`) : new Date(value)
const date = propertyType === 'date' ? new Date(`${stripTimeFromISO(value)}T00:00:00`) : new Date(value)
if (date) {
return formatDateProperty(date, propertyType)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const stripTimeFromISO = (date: string | Date | null): string | null => {
if (date === null) return null

if (typeof date === 'string') {
return date.replace(/T\d{2}:\d{2}:\d{2}\.\d{3}Z$/, '')
}

return date.toISOString().replace(/T\d{2}:\d{2}:\d{2}\.\d{3}Z$/, '')
}

0 comments on commit de86636

Please sign in to comment.