Skip to content

Commit e7773ed

Browse files
author
Stefen Alper
authored
Merge pull request #30 from smooth-code/timepicker
feat: timepicker
2 parents cb973d3 + 99c1fa6 commit e7773ed

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('#generateConfig', () => {
3737
['Boolean @field', { type: 'true_false' }],
3838
['Date @field', { type: 'date_picker' }],
3939
['DateTime @field', { type: 'date_time_picker' }],
40+
['Time @field', { type: 'time_picker' }],
4041
['Int @field', { type: 'number' }],
4142
['Float @field', { type: 'number' }],
4243
['Image @field', { type: 'image' }],

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ const handlers = {
4242
preventList(infos)
4343
return { type: 'date_time_picker' }
4444
},
45+
time(infos) {
46+
preventList(infos)
47+
return { type: 'time_picker' }
48+
},
4549
shortText({ list }) {
4650
if (list) return { type: 'textarea' }
4751
return { type: 'text' }

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

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

33
function acfField(object, name) {
44
if (object.acf) {
@@ -17,6 +17,10 @@ const handlers = {
1717
if (list) return null
1818
return object => formatDateTime(acfField(object, name))
1919
},
20+
time({ name, list }) {
21+
if (list) return null
22+
return object => formatTime(acfField(object, name))
23+
},
2024
link({ name, list }, helpers, state) {
2125
const { homeUrl } = state.options
2226
if (list) return null

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ export function formatDateTime(value) {
2424
const [date, time, period] = value.split(' ')
2525
return new Date(`${formatDateString(date)} ${time} ${period} UTC`)
2626
}
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`
38+
}

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

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

33
describe('util', () => {
44
describe('#formatDate', () => {
@@ -27,6 +27,20 @@ describe('util', () => {
2727
})
2828
})
2929

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+
3044
describe('#toRelativeUrl', () => {
3145
it('should support baseUrl without end slash', () => {
3246
expect(
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import gql from 'graphql-tag'
2-
import { GraphQLDate, GraphQLDateTime } from 'graphql-iso-date'
2+
import { GraphQLDate, GraphQLDateTime, GraphQLTime } from 'graphql-iso-date'
33

44
export const typeDefs = gql`
55
scalar Date
66
scalar DateTime
7+
scalar Time
78
`
89

910
export const resolvers = {
1011
Date: GraphQLDate,
1112
DateTime: GraphQLDateTime,
13+
Time: GraphQLTime,
1214
}

packages/smooth/src/graphql/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ export function getKnownType(type) {
107107
return 'date'
108108
case 'DateTime':
109109
return 'dateTime'
110+
case 'Time':
111+
return 'time'
110112
case 'Block':
111113
return 'block'
112114
default:

0 commit comments

Comments
 (0)