Skip to content

Commit 52ad79c

Browse files
authored
Merge pull request #31 from smooth-code/various-fixes2
Fix date & improve i18n support
2 parents b088476 + 50d8dd8 commit 52ad79c

File tree

22 files changed

+142
-167
lines changed

22 files changed

+142
-167
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(

packages/smooth-backend-wordpress/src/wordpress/plugin/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function get_content($data)
149149
$slug = $data['slug'];
150150
$lang = $data['lang'];
151151
$post = get_page_by_path($slug, OBJECT, $postType);
152-
if ($lang) {
152+
if ($lang && function_exists(icl_object_id)) {
153153
$post = get_page(icl_object_id($post->ID, $postType, true, $lang));
154154
}
155155
if (!$post) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/* eslint-disable import/no-unresolved */
2-
module.exports = require('./lib/page')
2+
module.exports = require('./lib/i18n')
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
import React from 'react'
22

3-
export default React.createContext({ error: null })
3+
const ErrorContext = React.createContext({ error: null })
4+
5+
export function ErrorContextProvider({ error, children }) {
6+
const value = React.useMemo(() => ({ error }), [error])
7+
return <ErrorContext.Provider value={value}>{children}</ErrorContext.Provider>
8+
}
9+
10+
export function useError() {
11+
const { error } = React.useContext(ErrorContext)
12+
return error
13+
}

packages/smooth/src/client/Root.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import React from 'react'
2+
import { Route } from '../router'
23
import Routes from './Routes'
34
import ErrorBoundary from './ErrorBoundary'
4-
import ErrorContext from './ErrorContext'
5+
import { useError } from './ErrorContext'
6+
import { HiddenHistoryProvider } from '../router/HiddenHistory'
7+
import { I18nContextProvider } from '../i18n/I18nContext'
8+
import { i18nRootPath } from '../i18n/Route'
59

610
export default function Root() {
11+
const error = useError()
712
return (
8-
<ErrorContext.Consumer>
9-
{({ error }) => (
10-
<ErrorBoundary error={error}>
11-
<Routes />
12-
</ErrorBoundary>
13-
)}
14-
</ErrorContext.Consumer>
13+
<HiddenHistoryProvider>
14+
<Route
15+
path={i18nRootPath}
16+
render={routeProps => (
17+
<I18nContextProvider lang={routeProps.match.params.lang || null}>
18+
<ErrorBoundary error={error}>
19+
<Routes {...routeProps} />
20+
</ErrorBoundary>
21+
</I18nContextProvider>
22+
)}
23+
/>
24+
</HiddenHistoryProvider>
1525
)
1626
}

packages/smooth/src/client/Routes.js

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,33 @@ import React from 'react'
22
import { Switch, Route } from '../router'
33
import Page, { getPages } from '../page/Page'
44
import { HiddenRouter } from '../router/HiddenRouter'
5-
import { HiddenHistoryProvider } from '../router/HiddenHistory'
65

76
const pages = getPages()
87

9-
export default function Routes() {
8+
export default function Routes({ match: { url } }) {
9+
const routes = (
10+
<Switch>
11+
{pages.map((page, index) => (
12+
<Route
13+
key={index}
14+
path={`${url}${page.routePath}`.replace(/\/\//, '/')}
15+
render={({ history, match, location }) => (
16+
<Page
17+
indexUrl={`${url}${page.indexPath}`}
18+
page={page}
19+
history={history}
20+
match={match}
21+
location={location}
22+
/>
23+
)}
24+
/>
25+
))}
26+
</Switch>
27+
)
1028
return (
11-
<HiddenHistoryProvider>
12-
<Route
13-
path="/:lang(.{2})?"
14-
render={({
15-
match: {
16-
url,
17-
params: { lang = null },
18-
},
19-
}) => {
20-
const routes = (
21-
<Switch>
22-
{pages.map((page, index) => (
23-
<Route
24-
key={index}
25-
path={`${url}${page.routePath}`.replace(/\/\//, '/')}
26-
render={({ history, match, location }) => (
27-
<Page
28-
lang={lang}
29-
indexUrl={`${url}${page.indexPath}`}
30-
page={page}
31-
history={history}
32-
match={match}
33-
location={location}
34-
/>
35-
)}
36-
/>
37-
))}
38-
</Switch>
39-
)
40-
return (
41-
<>
42-
{routes}
43-
<HiddenRouter>{routes}</HiddenRouter>
44-
</>
45-
)
46-
}}
47-
/>
48-
</HiddenHistoryProvider>
29+
<>
30+
{routes}
31+
<HiddenRouter>{routes}</HiddenRouter>
32+
</>
4933
)
5034
}

0 commit comments

Comments
 (0)