import React, { Component } from "react";
import './App.scss';
const backendUrl = "https://kucinakobackend.ichigo.everydayimshuflin.com";
function makeRequest(queryString, jsonHandler) {
const effectiveUrl = `${backendUrl}/${queryString}`
fetch(`${effectiveUrl}`)
.then((resp) => {
return resp.json()
})
.then((json) => {
jsonHandler(json);
});
}
function testHandler(json) {
console.log("JSON");
console.log(json);
}
function Entry(props) {
const entry = props.entry;
return (
{ entry.sai } - { entry.eng }
{ entry.syn_category }
{ entry.morph_type ? `\t\t${entry.morph_type}` : null }
);
}
class Results extends Component {
constructor(props) {
super(props);
this.content = this.content.bind(this);
}
content() {
const num = this.props.searchResults.length;
const header = (
Searched for { this.props.searchTerm } search type: { this.props.searchType }, found { num } result(s)
);
const entries = this.props.searchResults.map(
(entry, idx) =>
);
return [header].concat(entries);
}
render() {
const results = this.props.searchResults;
return(
{ results ? this.content() : "No search" }
);
}
}
class App extends Component {
constructor(props) {
super(props);
this.input = React.createRef();
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}*`
if (searchTerm === "") {
this.setState({ searchResults: null, searchTerm: null, searchType: null });
} else {
makeRequest(request, (json) => {
this.setState({ searchResults: json, searchType: "saimiar", searchTerm });
});
}
}
searchEng(evt) {
const searchTerm = this.input.current.value;
const request = `saimiar?eng=like.*${searchTerm}*`
if (searchTerm === "") {
this.setState({ searchResults: null, searchTerm: null, searchType: null });
} else {
makeRequest(request, (json) => {
this.setState({ searchResults: json, searchType: "eng-saimiar", searchTerm });
});
}
}
render() {
return(
);
}
}
export default App;