|
1 | | -require( 'isomorphic-fetch' ); |
2 | | -import React from 'react'; |
3 | | -import qs from 'query-string'; |
4 | | -import { DebounceInput } from 'react-debounce-input'; |
5 | | -import SearchResults from './searchResults'; |
| 1 | +require("isomorphic-fetch"); |
| 2 | +import React from "react"; |
| 3 | +import qs from "query-string"; |
| 4 | +import { DebounceInput } from "react-debounce-input"; |
| 5 | +import SearchResults from "./searchResults"; |
6 | 6 |
|
7 | 7 | export default class SearchForm extends React.Component { |
| 8 | + constructor(props) { |
| 9 | + super(props); |
8 | 10 |
|
9 | | - constructor( props ) { |
10 | | - super( props ); |
11 | | - |
12 | | - this.getResults = this.getResults.bind( this ); |
13 | | - this.getFormClass = this.getFormClass.bind( this ); |
| 11 | + this.getResults = this.getResults.bind(this); |
| 12 | + this.getFormClass = this.getFormClass.bind(this); |
14 | 13 |
|
15 | 14 | this.state = { |
16 | 15 | results: [], |
17 | 16 | loading: false, |
18 | 17 | searched: false, |
19 | 18 | lengthError: false, |
20 | | - empty: false, |
| 19 | + empty: false |
21 | 20 | }; |
22 | 21 | } |
23 | 22 |
|
24 | | - getResults( event ) { |
25 | | - |
| 23 | + getResults(event) { |
26 | 24 | const search = event.target.value; |
27 | 25 |
|
28 | | - if ( ! search ) { |
29 | | - this.setState( { |
| 26 | + if (!search) { |
| 27 | + this.setState({ |
30 | 28 | results: [], |
31 | 29 | loading: false, |
32 | 30 | searched: false, |
33 | 31 | lengthError: false, |
34 | | - empty: true, |
35 | | - } ); |
| 32 | + empty: true |
| 33 | + }); |
36 | 34 |
|
37 | 35 | return; |
38 | 36 | } |
39 | 37 |
|
40 | | - if ( search && wds_react_post_search.minimum_character_count <= search.length ) { |
41 | | - this.setState( { |
| 38 | + if ( |
| 39 | + search && |
| 40 | + wds_react_post_search.minimum_character_count <= search.length |
| 41 | + ) { |
| 42 | + this.setState({ |
42 | 43 | loading: true, |
43 | 44 | searched: true, |
44 | 45 | lengthError: false, |
45 | | - empty: false, |
46 | | - } ); |
| 46 | + empty: false |
| 47 | + }); |
47 | 48 |
|
48 | 49 | const postType = this.props.postType; |
49 | 50 |
|
50 | | - let url = wds_react_post_search.rest_search_posts.replace( '%s', search ); |
| 51 | + let url = wds_react_post_search.rest_search_posts.replace("%s", search); |
51 | 52 | let query = { |
52 | 53 | s: search |
53 | 54 | }; |
54 | 55 |
|
55 | | - if ( postType ) { |
| 56 | + if (postType) { |
56 | 57 | query.type = postType; |
57 | | - }; |
58 | | - |
59 | | - const queryString = qs.stringify( query ); |
60 | | - url = `${ url }?${ queryString }`; |
61 | | - |
62 | | - let json = fetch( url ) |
63 | | - .then( |
64 | | - response => { |
65 | | - return response.json() |
66 | | - } |
67 | | - ) |
68 | | - .then( |
69 | | - results => { |
70 | | - this.setState( { |
71 | | - results: results, |
72 | | - loading: false |
73 | | - } ); |
74 | | - } |
75 | | - ); |
| 58 | + } |
| 59 | + |
| 60 | + const queryString = qs.stringify(query); |
| 61 | + url = `${url}?${queryString}`; |
| 62 | + |
| 63 | + let json = fetch(url) |
| 64 | + .then(response => { |
| 65 | + return response.json(); |
| 66 | + }) |
| 67 | + .then(results => { |
| 68 | + this.setState({ |
| 69 | + results: results, |
| 70 | + loading: false |
| 71 | + }); |
| 72 | + }); |
76 | 73 | } else { |
77 | | - this.setState( { |
| 74 | + this.setState({ |
78 | 75 | results: [], |
79 | 76 | searched: false, |
80 | 77 | lengthError: true, |
81 | | - empty: false, |
82 | | - } ); |
| 78 | + empty: false |
| 79 | + }); |
83 | 80 | } |
84 | 81 | } |
85 | 82 |
|
86 | 83 | getFormClass() { |
87 | | - let className = 'search-form-input'; |
88 | | - |
89 | | - if ( this.state.results.length > 0 || this.state.loading || this.state.lengthError || this.state.searched ) { |
90 | | - className += ' search-form-performing-action'; |
| 84 | + let className = "search-form-input"; |
| 85 | + |
| 86 | + if ( |
| 87 | + this.state.results.length > 0 || |
| 88 | + this.state.loading || |
| 89 | + this.state.lengthError || |
| 90 | + this.state.searched |
| 91 | + ) { |
| 92 | + className += " search-form-performing-action"; |
91 | 93 | } |
92 | 94 |
|
93 | | - if ( this.state.empty ) { |
94 | | - className = 'search-form-input'; |
| 95 | + if (this.state.empty) { |
| 96 | + className = "search-form-input"; |
95 | 97 | } |
96 | 98 |
|
97 | 99 | return className; |
98 | 100 | } |
99 | 101 |
|
100 | 102 | render() { |
101 | 103 | return ( |
102 | | - <div className={ `${ this.getFormClass() }` }> |
103 | | - <DebounceInput debounceTimeout={ 300 } className="search-input" type="text" onChange={ this.getResults } placeholder={ wds_react_post_search.placeholder_text } /> |
104 | | - <SearchResults searched={ this.state.searched } loading={ this.state.loading } results={ this.state.results } lengthError={ this.state.lengthError } empty={ this.state.empty } /> |
| 104 | + <div className={`${this.getFormClass()}`}> |
| 105 | + <DebounceInput |
| 106 | + debounceTimeout={300} |
| 107 | + className="search-input" |
| 108 | + type="text" |
| 109 | + onChange={this.getResults} |
| 110 | + placeholder={wds_react_post_search.placeholder_text} |
| 111 | + /> |
| 112 | + <SearchResults |
| 113 | + searched={this.state.searched} |
| 114 | + loading={this.state.loading} |
| 115 | + results={this.state.results} |
| 116 | + lengthError={this.state.lengthError} |
| 117 | + empty={this.state.empty} |
| 118 | + /> |
105 | 119 | </div> |
106 | | - ) |
| 120 | + ); |
107 | 121 | } |
108 | 122 | } |
0 commit comments