Skip to content

Commit ebdb742

Browse files
author
Ethan Jon
committed
updates
1 parent 1a69664 commit ebdb742

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed

react/.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
'react/jsx-no-bind': ['warn', {ignoreRefs: true}],
2424
'react/jsx-filename-extension': 'off',
2525
'react/jsx-indent': ['warn', 2],
26-
'react/jsx-indent-props': ['warn', 2]
26+
'react/jsx-indent-props': ['warn', 2],
27+
'no-return-assign': 'off'
2728
}
2829
}

react/helpers/create-page.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export default function (Child: Object, {propPaths = () => ({}), fullWidth = fal
5353

5454
let fetches = await Promise.all(pathsKeys.map(async k => {
5555
const input = paths[k]
56-
const [path, authorize] = typeof input === 'string' ? [input, false] : [input.path, input.authorize]
56+
const [path, authorize, makeSingle] = typeof input === 'string'
57+
? [input, false, false]
58+
: [input.path, input.authorize, input.makeSingle]
5759

5860
if (fetchCache[path]) {
5961
return fetchCache[path]
@@ -67,9 +69,13 @@ export default function (Child: Object, {propPaths = () => ({}), fullWidth = fal
6769
})
6870
})
6971

70-
fetchCache[path] = await res.json()
72+
let json = await res.json()
7173

72-
return fetchCache[path]
74+
if (makeSingle && Array.isArray(json)) {
75+
json = json[0]
76+
}
77+
78+
return fetchCache[path] = json
7379
}))
7480

7581
const finalProps = pathsKeys.reduce((obj, key, i) => {

react/pages/_error.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import React from 'react'
55
import createPage from '../helpers/create-page'
66

77
export const Error = () => (
8-
<div className='center h2'>
8+
<div className='center'>
99
<hr />
10-
{'There was an error.'}
10+
<h1 className='my4'>{'There was an error.'}</h1>
1111
<hr />
1212
</div>
1313
)

react/pages/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import createPage from '../helpers/create-page'
1111
import {didPassPasswordAuthorization, getUrlObj, getFeaturedImageProps, getThumbnailImageProps} from '../helpers/post-data'
1212
import styles from '../styles/pages/index.scss'
1313

14-
export const Home = ({postsData}: Object, {siteData}: Object) => {
14+
export const Home = ({pageData, postsData}: Object, {siteData}: Object) => {
1515
return (
1616
<div>
1717
<Head>
@@ -87,7 +87,6 @@ export const Home = ({postsData}: Object, {siteData}: Object) => {
8787
className='my1 h5'
8888
postData={postData}
8989
/>
90-
9190
</div>
9291
</div>
9392
</a>
@@ -111,6 +110,10 @@ export default createPage(Home, {
111110
fullWidth: true,
112111
maxWidth: 3,
113112
propPaths: ({query: {preview}}) => ({
113+
pageData: {
114+
path: '/wp/v2/pages?slug=home',
115+
makeSingle: true
116+
},
114117
postsData: {
115118
authorize: !!preview,
116119
path: `/wp/v2/posts?_embed&per_page=50${preview ? '&status[]=pending&status[]=publish' : ''}`

react/pages/singular.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,21 @@
33
import React from 'react'
44

55
import Post from '../components/post'
6+
import {Error} from '../pages/_error'
67
import createPage from '../helpers/create-page'
78
import {didFailPasswordAuthorization} from '../helpers/post-data'
89

9-
const extractPostData = ({postsData, revisionsData}: Object) => Object.assign({},
10-
Array.isArray(postsData) ? postsData[0] : postsData,
11-
revisionsData ? revisionsData[0] : {}
12-
)
10+
export const Singular = ({postData, revisionsData}: Object) => {
11+
const postWithRevisionData = Object.assign({}, postData, revisionsData && revisionsData[0])
1312

14-
export const Singular = (props: Object) => {
15-
const postData = extractPostData(props)
16-
17-
if (!Object.keys(postData).length || didFailPasswordAuthorization(postData)) {
18-
return (
19-
<div>
20-
<hr />
21-
<h2 className='center my3'>{"We couldn't find that page."}</h2>
22-
<hr />
23-
</div>
24-
)
13+
if (!Object.keys(postWithRevisionData).length || didFailPasswordAuthorization(postWithRevisionData)) {
14+
return <Error />
2515
}
2616

2717
return (
2818
<Post
29-
key={`Post${postData.id}`}
30-
postData={postData}
19+
key={`Post${postWithRevisionData.id}`}
20+
postData={postWithRevisionData}
3121
/>
3222
)
3323
}
@@ -39,8 +29,9 @@ export default createPage(Singular, {
3929
fullWidth: true,
4030
maxWidth: 3,
4131
propPaths: ({asPath, query: {p, password, page_id, preview, preview_id, type, slug}}) => ({
42-
postsData: {
32+
postData: {
4333
authorize: !!preview,
34+
makeSingle: true,
4435
path: (p || page_id) ? `/wp/v2/${type}s/${p || page_id}/?_embed` : `/wp/v2/${type}s?_embed&slug=${slug}&password=${password}`
4536
},
4637
revisionsData: preview ? {

0 commit comments

Comments
 (0)