-
Notifications
You must be signed in to change notification settings - Fork 651
/
index.js
149 lines (138 loc) · 3.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import React, { Component } from 'react';
import Link from 'next/link';
import Router from 'next/router';
import WPAPI from 'wpapi';
import Layout from '../components/Layout';
import PageWrapper from '../components/PageWrapper';
import Menu from '../components/Menu';
import Config from '../config';
import Logo from '../static/images/starter-kit-logo.svg';
const wp = new WPAPI({ endpoint: Config.apiUrl });
const headerImageStyle = {
marginTop: 50,
marginBottom: 50,
};
const tokenExpired = () => {
if (process.browser) {
localStorage.removeItem(Config.AUTH_TOKEN);
}
wp.setHeaders('Authorization', '');
Router.push('/login');
};
class Index extends Component {
state = {
id: '',
};
static async getInitialProps() {
try {
const [page, posts, pages] = await Promise.all([
wp
.pages()
.slug('welcome')
.embed()
.then(data => {
return data[0];
}),
wp.posts().embed(),
wp.pages().embed(),
]);
return { page, posts, pages };
} catch (err) {
if (err.data.status === 403) {
tokenExpired();
}
}
return null;
}
componentDidMount() {
const token = localStorage.getItem(Config.AUTH_TOKEN);
if (token) {
wp.setHeaders('Authorization', `Bearer ${token}`);
wp.users()
.me()
.then(data => {
const { id } = data;
this.setState({ id });
})
.catch(err => {
if (err.data.status === 403) {
tokenExpired();
}
});
}
}
render() {
const { id } = this.state;
const { posts, pages, headerMenu, page } = this.props;
const fposts = posts.map(post => {
return (
<ul key={post.slug}>
<li>
<Link
as={`/post/${post.slug}`}
href={`/post?slug=${post.slug}&apiRoute=post`}
>
<a>{post.title.rendered}</a>
</Link>
</li>
</ul>
);
});
const fpages = pages.map(ipage => {
if (ipage.slug !== 'welcome') {
return (
<ul key={ipage.slug}>
<li>
<Link
as={`/page/${ipage.slug}`}
href={`/post?slug=${ipage.slug}&apiRoute=page`}
>
<a>{ipage.title.rendered}</a>
</Link>
</li>
</ul>
);
}
});
return (
<Layout>
<Menu menu={headerMenu} />
<div className="intro bg-black white ph3 pv4 ph5-m pv5-l flex flex-column flex-row-l">
<div className="color-logo w-50-l mr3-l">
<Logo width={440} height={280} />
</div>
<div className="subhed pr6-l">
<h1>{page.title.rendered}</h1>
<div className="dek">
You are now running a WordPress backend with a React frontend.
</div>
<div className="api-info b mt4">
Starter Kit supports both REST API and GraphQL
<div className="api-toggle">
<a className="rest" href="http://localhost:3000">REST API</a>
<a className="graphql" href="http://localhost:3001">GraphQL</a>
</div>
</div>
</div>
</div>
<div className="recent flex mh4 mv4 w-two-thirds-l center-l">
<div className="w-50 pr3">
<h2>Posts</h2>
{fposts}
</div>
<div className="w-50 pl3">
<h2>Pages</h2>
{fpages}
</div>
</div>
<div className="content mh4 mv4 w-two-thirds-l center-l home"
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: page.content.rendered,
}}
/>
</Layout>
);
}
}
export default PageWrapper(Index);