Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 272a595

Browse files
committed
fix: fix date, dateTime & time with i18n
1 parent b088476 commit 272a595

File tree

5 files changed

+29
-65
lines changed

5 files changed

+29
-65
lines changed

packages/smooth-backend-wordpress/src/acf/config/index.test.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,22 @@ describe('#generateConfig', () => {
3535
['String @field(type: longText)', { type: 'textarea' }],
3636
['String @field(type: richText)', { type: 'wysiwyg' }],
3737
['Boolean @field', { type: 'true_false' }],
38-
['Date @field', { type: 'date_picker' }],
39-
['DateTime @field', { type: 'date_time_picker' }],
40-
['Time @field', { type: 'time_picker' }],
38+
[
39+
'Date @field',
40+
{ type: 'date_picker', display_format: 'd/m/Y', return_format: 'c' },
41+
],
42+
[
43+
'DateTime @field',
44+
{
45+
type: 'date_time_picker',
46+
display_format: 'd/m/Y g:i a',
47+
return_format: 'c',
48+
},
49+
],
50+
[
51+
'Time @field',
52+
{ type: 'time_picker', display_format: 'g:i a', return_format: 'c' },
53+
],
4154
['Int @field', { type: 'number' }],
4255
['Float @field', { type: 'number' }],
4356
['Image @field', { type: 'image' }],

packages/smooth-backend-wordpress/src/acf/config/oneField.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,19 @@ const handlers = {
3636
},
3737
date(infos) {
3838
preventList(infos)
39-
return { type: 'date_picker' }
39+
return { type: 'date_picker', display_format: 'd/m/Y', return_format: 'c' }
4040
},
4141
dateTime(infos) {
4242
preventList(infos)
43-
return { type: 'date_time_picker' }
43+
return {
44+
type: 'date_time_picker',
45+
display_format: 'd/m/Y g:i a',
46+
return_format: 'c',
47+
}
4448
},
4549
time(infos) {
4650
preventList(infos)
47-
return { type: 'time_picker' }
51+
return { type: 'time_picker', display_format: 'g:i a', return_format: 'c' }
4852
},
4953
shortText({ list }) {
5054
if (list) return { type: 'textarea' }

packages/smooth-backend-wordpress/src/acf/resolvers/createDefaultResolvers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toRelativeUrl, formatDate, formatDateTime, formatTime } from './util'
1+
import { toRelativeUrl, formatDate } from './util'
22

33
function acfField(object, name) {
44
if (object.acf) {
@@ -15,11 +15,11 @@ const handlers = {
1515
},
1616
dateTime({ name, list }) {
1717
if (list) return null
18-
return object => formatDateTime(acfField(object, name))
18+
return object => formatDate(acfField(object, name))
1919
},
2020
time({ name, list }) {
2121
if (list) return null
22-
return object => formatTime(acfField(object, name))
22+
return object => formatDate(acfField(object, name))
2323
},
2424
link({ name, list }, helpers, state) {
2525
const { homeUrl } = state.options

packages/smooth-backend-wordpress/src/acf/resolvers/util.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,7 @@ export function toRelativeUrl(baseUrl, url) {
77
return finalUrl
88
}
99

10-
function formatDateString(value) {
11-
return value
12-
.split('/')
13-
.reverse()
14-
.join('-')
15-
}
16-
1710
export function formatDate(value) {
1811
if (typeof value !== 'string' || value === '') return null
19-
return new Date(formatDateString(value))
20-
}
21-
22-
export function formatDateTime(value) {
23-
if (typeof value !== 'string' || value === '') return null
24-
const [date, time, period] = value.split(' ')
25-
return new Date(`${formatDateString(date)} ${time} ${period} UTC`)
26-
}
27-
28-
export function formatTime(value) {
29-
if (typeof value !== 'string' || value === '') return null
30-
const [, hours, minutes, type] = value.match(/^(\d+):(\d+)\s+([ap]m)$/)
31-
return `${[
32-
type === 'pm' ? ((12 + Number(hours)) % 24).toString() : hours,
33-
minutes,
34-
'0',
35-
]
36-
.map(x => x.padStart(2, '0'))
37-
.join(':')}Z`
12+
return new Date(value)
3813
}

packages/smooth-backend-wordpress/src/acf/resolvers/util.test.js

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { toRelativeUrl, formatDate, formatDateTime, formatTime } from './util'
1+
import { toRelativeUrl, formatDate } from './util'
22

33
describe('util', () => {
44
describe('#formatDate', () => {
55
it('should format date', () => {
6-
expect(formatDate('21/05/1989').toJSON()).toBe('1989-05-21T00:00:00.000Z')
6+
expect(formatDate('1989-05-21').toJSON()).toBe('1989-05-21T00:00:00.000Z')
77
})
88

99
it('should return null if not a string (or empty)', () => {
@@ -13,34 +13,6 @@ describe('util', () => {
1313
})
1414
})
1515

16-
describe('#formatDateTime', () => {
17-
it('should format datetime', () => {
18-
expect(formatDateTime('21/05/1989 02:00 am').toJSON()).toBe(
19-
'1989-05-21T02:00:00.000Z',
20-
)
21-
})
22-
23-
it('should return null if not a string (or empty)', () => {
24-
expect(formatDateTime(null)).toBe(null)
25-
expect(formatDateTime('')).toBe(null)
26-
expect(formatDateTime(undefined)).toBe(null)
27-
})
28-
})
29-
30-
describe('#formatTime', () => {
31-
it('should format time', () => {
32-
expect(formatTime('2:30 am')).toBe('02:30:00Z')
33-
expect(formatTime('7:45 pm')).toBe('19:45:00Z')
34-
expect(formatTime('12:15 pm')).toBe('00:15:00Z')
35-
})
36-
37-
it('should return null if not a string (or empty)', () => {
38-
expect(formatTime(null)).toBe(null)
39-
expect(formatTime('')).toBe(null)
40-
expect(formatTime(undefined)).toBe(null)
41-
})
42-
})
43-
4416
describe('#toRelativeUrl', () => {
4517
it('should support baseUrl without end slash', () => {
4618
expect(

0 commit comments

Comments
 (0)