-
Notifications
You must be signed in to change notification settings - Fork 649
/
search.js
72 lines (65 loc) · 1.93 KB
/
search.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
import React, { Component } from 'react';
import Link from 'next/link';
import axios from 'axios';
import Router from 'next/router';
import Layout from '../components/Layout';
import PageWrapper from '../components/PageWrapper';
import Menu from '../components/Menu';
import WPAPI from 'wpapi';
import Config from '../config';
const wp = new WPAPI({ endpoint: Config.apiUrl });
class Search extends Component {
state = {
posts: [],
filter: '',
};
/**
* Execute search query, process the response and set the state
*/
executeSearch = async () => {
const { filter } = this.state;
let posts = await wp
.posts()
.search(filter);
this.setState({ posts });
}
render() {
const { posts } = this.state;
const { headerMenu } = this.props;
return (
<Layout>
<Menu menu={headerMenu} />
<div className="content login mh4 mv4 w-two-thirds-l center-l">
<div>
<h1>Search</h1>
<input
className="db w-100 pa3 mv3 br6 ba b--black"
type="text"
placeholder="Search by name and content"
onChange={e => this.setState({ filter: e.target.value })}
onKeyDown={this.handleKeyDown}
/>
<button
className="round-btn invert ba bw1 pv2 ph3"
type="button"
onClick={() => this.executeSearch()}
>
Submit
</button>
</div>
<div className="mv4">
{posts ? posts.map((post, index) => (
<div className="mv4" key={post.slug}>
<span className="gray">{index + 1}.</span>
<Link href={`/post/${post.slug}`}>
<h3 className="ml1 dib pointer">{post.title.rendered}</h3>
</Link>
</div>
)) : ''}
</div>
</div>
</Layout>
);
}
}
export default PageWrapper(Search);