2019-01-22 00:41:13 -08:00
|
|
|
import React, { Component } from "react";
|
|
|
|
|
2019-01-25 02:19:22 -08:00
|
|
|
import './App.scss';
|
|
|
|
|
2019-01-24 02:56:41 -08:00
|
|
|
const backendUrl = "https://kucinakobackend.ichigo.everydayimshuflin.com";
|
|
|
|
|
|
|
|
function makeRequest(queryString, jsonHandler) {
|
|
|
|
const effectiveUrl = `${backendUrl}/${queryString}`
|
2019-01-26 01:18:27 -08:00
|
|
|
fetch(`${effectiveUrl}`)
|
2019-01-24 02:56:41 -08:00
|
|
|
.then((resp) => {
|
|
|
|
return resp.json()
|
|
|
|
})
|
|
|
|
.then((json) => {
|
|
|
|
jsonHandler(json);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function testHandler(json) {
|
|
|
|
console.log("JSON");
|
|
|
|
console.log(json);
|
|
|
|
}
|
|
|
|
|
2019-01-26 01:34:35 -08:00
|
|
|
function Entry(props) {
|
|
|
|
const entry = props.entry;
|
|
|
|
return (
|
|
|
|
<div className="searchResult" key={ entry.id }>
|
|
|
|
<b>{ entry.sai }</b> - { entry.eng }
|
|
|
|
<br />
|
|
|
|
<span className="synclass">{ entry.syn_category }</span>
|
|
|
|
<br />
|
|
|
|
Type: { entry.morph_type }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-26 01:18:27 -08:00
|
|
|
class Results extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.content = this.content.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
content() {
|
|
|
|
const header = (
|
|
|
|
<div className="searchResultHeader">
|
|
|
|
Searched for <b>{ this.props.searchTerm }</b> search type: { this.props.searchType }
|
|
|
|
</div>);
|
2019-01-26 01:34:35 -08:00
|
|
|
const entries = this.props.searchResults.map(
|
|
|
|
(entry, idx) => <Entry entry={ entry } key= { entry.id } />
|
|
|
|
);
|
2019-01-26 01:18:27 -08:00
|
|
|
return [header].concat(entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const results = this.props.searchResults;
|
|
|
|
return(
|
|
|
|
<div className='results'>
|
|
|
|
{ results ? this.content() : "No results" }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 00:41:13 -08:00
|
|
|
class App extends Component {
|
2019-01-24 02:56:41 -08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.input = React.createRef();
|
2019-01-26 01:18:27 -08:00
|
|
|
this.searchEng = this.searchEng.bind(this);
|
|
|
|
this.searchSaimiar = this.searchSaimiar.bind(this);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
searchResults: null,
|
|
|
|
searchType: null,
|
|
|
|
searchTerm: null
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
searchSaimiar(evt) {
|
|
|
|
const searchTerm = this.input.current.value;
|
|
|
|
const request = `saimiar?sai=like.*${searchTerm}*`
|
|
|
|
makeRequest(request, (json) => {
|
|
|
|
const searchResults = json.length === 0 ? null : json;
|
|
|
|
this.setState({ searchResults, searchType: "saimiar", searchTerm });
|
|
|
|
});
|
2019-01-24 02:56:41 -08:00
|
|
|
}
|
|
|
|
|
2019-01-26 01:18:27 -08:00
|
|
|
searchEng(evt) {
|
|
|
|
const searchTerm = this.input.current.value;
|
|
|
|
const request = `saimiar?eng=like.*${searchTerm}*`
|
|
|
|
makeRequest(request, (json) => {
|
|
|
|
const searchResults = json.length === 0 ? null : json;
|
|
|
|
this.setState({ searchResults, searchType: "eng-saimiar", searchTerm });
|
|
|
|
});
|
2019-01-24 02:56:41 -08:00
|
|
|
}
|
|
|
|
|
2019-01-22 00:41:13 -08:00
|
|
|
render() {
|
|
|
|
return(
|
2019-01-25 02:19:22 -08:00
|
|
|
<main>
|
|
|
|
<div className='container'>
|
2019-01-26 01:18:27 -08:00
|
|
|
<div className='search'>
|
|
|
|
<h1>Kucinako</h1>
|
|
|
|
<div className='textInput'>
|
|
|
|
<input className='textInput' type="text" ref={ this.input } />
|
|
|
|
</div>
|
|
|
|
<br/>
|
|
|
|
<button onClick={ this.searchSaimiar } className="searchButton">Saimiar</button>
|
|
|
|
<button onClick={ this.searchEng } className="searchButton">English</button>
|
2019-01-25 02:19:22 -08:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-26 01:18:27 -08:00
|
|
|
<Results
|
|
|
|
searchResults={ this.state.searchResults }
|
|
|
|
searchTerm= { this.state.searchTerm }
|
|
|
|
searchType={ this.state.searchType }
|
|
|
|
/>
|
2019-01-25 02:19:22 -08:00
|
|
|
</main>
|
2019-01-22 00:41:13 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-26 01:18:27 -08:00
|
|
|
|
|
|
|
|
2019-01-22 00:41:13 -08:00
|
|
|
export default App;
|