-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASelect.js
134 lines (126 loc) · 4.03 KB
/
ASelect.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
import React, { Component } from 'react';
import './App.css';
import Select from 'react-select';
import _ from "lodash";
import AsyncSelect, { makeAsyncSelect } from 'react-select/async';
import { withRouter } from 'react-router-dom';
class ASelect extends Component {
constructor(props) {
super(props);
var aId = window.location.href;
var search_param2 = "";
if (aId.includes("search") === true)
{
aId = aId.split("?")[1];
console.log("Pt1 : ", aId);
var vars = aId.split("&");
console.log("Pt2 : ", vars);
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
console.log("Pt3 : ", pair);
if (pair[0] === "q")
{
search_param2 = pair[1];
console.log("Pt4 : ", search_param2);
}
}
}
console.log("What's now : ", window.location.href, search_param2, aId);
this.state = {
results: [],
search_param: null,
search_param2: search_param2,
valueVariable : ''
};
if (search_param2 !== "" && search_param2 !== null)
{
search_param2 = decodeURI(search_param2);
this.setState({valueVariable: {
label: search_param2,
value: search_param2
}
});
}
this.handleChange = this.handleChange.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
this.selectedVal = this.selectedVal.bind(this);
}
async handleChange (inputValue, callback) {
try {
var url_to_send = "https://api.cognitive.microsoft.com/bing/v7.0/suggestions?mkt=fr-FR&q="+inputValue;
const response = await fetch(url_to_send,
{
headers: {
"Ocp-Apim-Subscription-Key": "XXXX"
}
}
);
const options = await response.json();
const resultsRaw = options.suggestionGroups[0].searchSuggestions;
const data = resultsRaw.map(function(result){return {value:result.id, label:result.displayText}} );
this.setState({ results:data });
return callback(data);
} catch (error) {
console.error(`Error fetching search`, error);
}
}
componentWillUnmount() {
this.props.set_search("");
this.setState({ results:null });
}
handleInputChange(inputValue) {
this.setState({ results:this.state.results });
}
selectedVal(inputValue) {
var url_2 = "/search?q="+inputValue.label;
this.setState({search_param: inputValue.label});
this.props.set_search(inputValue.label);
this.setState({ results:null });
this.props.history.push(url_2);
}
onFocus() {
this.props.set_search(null);
this.setState({ results:null });
}
get_val(inputValue) {
return inputValue;
}
componentWillReceiveProps() {
var aId = window.location.href;
var search_val = "";
if (aId.includes("search") === true)
{
aId = aId.split("?")[1];
console.log("Pt11 : ", aId);
var vars = aId.split("&");
console.log("Pt22 : ", vars);
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
console.log("Pt33 : ", pair);
if (pair[0] === "q")
{
search_val = pair[1];
console.log("Pt44 : ", search_val);
}
}
}
if (search_val !== "" && search_val !== null)
{
search_val = decodeURI(search_val);
this.setState({search_param2: search_val,
valueVariable : {
label: search_val,
value: search_val
}});
}
else {
this.setState({valueVariable : ''});
}
}
render() {
return (
<AsyncSelect value={this.state.valueVariable} placeholder={"Enter keyword .."} cacheOptions = {false} options={this.state.results} loadOptions={_.debounce(this.handleChange, 1000)} onFocus={this.onFocus.bind(this)} onInputChange={_.debounce(this.handleInputChange,1000)} onChange={this.selectedVal} id="left-menu" />
);
}
}
export default withRouter(ASelect);