Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix property type 'date' when backend returns an iso string #1700

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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$/, '')
}
Loading