From 3a261bef957a568eb4e86e7db37d390351806a40 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 12 Sep 2021 22:12:39 -0700 Subject: [PATCH] Convert App to functional component --- src/App.tsx | 145 ++++++++++++++++++++++++------------------------- src/dbtypes.ts | 2 +- 2 files changed, 71 insertions(+), 76 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index e5afcec..d9d35e1 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,8 +1,8 @@ -import React, {Component} from 'react'; +import React, {useState} from 'react'; import './App.scss'; import {declineSaimiar} from './saimiar_morphology'; -import {SaiEntryProps, JutEntryProps } from './dbtypes'; +import {SaiEntryProps, JutEntryProps} from './dbtypes'; const backendUrl = 'https://kucinakobackend.ichigo.everydayimshuflin.com'; @@ -145,113 +145,108 @@ const Results = (props: ResultsProps) => { ); }; -interface App { - [x: string]: any; -} +const App = (_props) => { + const [searchResults, setSearchResults] = useState(null); + const [conlang, setConlang] = useState(Conlang.Saimiar); + const [direction, setDirection] = useState(null); + const [searchTerm, setSearchTerm] = useState(null); -class App extends Component { - constructor(props) { - super(props); - this.input = React.createRef(); - this.handleLangChange = this.handleLangChange.bind(this); + const [searchBoxInput, setSearchBoxInput] = useState(''); - this.searchEng = this.searchEng.bind(this); - this.searchSaimiar = this.searchSaimiar.bind(this); - this.searchJuteyuji = this.searchJuteyuji.bind(this); - this.searchConlang = this.searchConlang.bind(this); - - this.state = { - searchResults: null, - conlang: Conlang.Saimiar, - direction: null, - searchTerm: null, - }; - } - - searchConlang(_evt) { - const searchTerm = this.input.current.value; - const {conlang} = this.state; + const searchConlang = (_evt) => { + const searchTerm = searchBoxInput; if (conlang === Conlang.Saimiar) { - this.searchSaimiar(searchTerm); + searchSaimiar(searchTerm); } else if (conlang === Conlang.Juteyuji) { - this.searchJuteyuji(searchTerm); + searchJuteyuji(searchTerm); } else { console.error(`Conlang ${conlang} not supported`); } - } + }; - searchSaimiar(searchTerm: string) { + const searchSaimiar = (searchTerm: string) => { const request = `saimiar?sai=like.*${searchTerm}*`; if (searchTerm === '') { - this.setState({searchResults: null, searchTerm: null, direction: null}); + setSearchResults(null); + setSearchTerm(null); + setDirection(null); } else { makeRequest(request, (json) => { - this.setState({searchResults: json, searchTerm, direction: SearchDirection.ToEnglish}); + setSearchResults(json); + setSearchTerm(searchTerm); + setDirection(SearchDirection.ToEnglish); }); } - } + }; - searchJuteyuji(searchTerm: string) { + const searchJuteyuji = (searchTerm: string) => { const request = `juteyuji?jut=like.*${searchTerm}*`; if (searchTerm === '') { - this.setState({searchResults: null, searchTerm: null, direction: null}); + setSearchResults(null); + setSearchTerm(null); + setDirection(null); } else { makeRequest(request, (json) => { - this.setState({searchResults: json, searchTerm, direction: SearchDirection.ToEnglish}); + setSearchResults(json); + setSearchTerm(searchTerm); + setDirection(SearchDirection.ToEnglish); }); } - } + }; - searchEng(_evt) { - const searchTerm = this.input.current.value; - const {conlang} = this.state; + const searchEng = (_evt) => { + const searchTerm = searchBoxInput; const request = `${conlang}?eng=like.*${searchTerm}*`; if (searchTerm === '') { - this.setState({searchResults: null, searchTerm: null}); + setSearchResults(null); + setSearchTerm(null); } else { makeRequest(request, (json) => { - this.setState({searchResults: json, searchTerm, direction: SearchDirection.ToConlang}); + setSearchResults(json); + setSearchTerm(searchTerm); + setDirection(SearchDirection.ToConlang); }); } - } + }; - handleLangChange(evt) { + const handleLangChange = (evt) => { const conlang: Conlang = evt.target.value as Conlang; console.log('Conlang in handlelangchange', conlang); - this.setState({conlang, searchResults: null}); - } + setConlang(conlang); + setSearchResults(null); + }; - render() { - const conlangs = [Conlang.Saimiar, Conlang.Elesu, Conlang.Tukvaysi, Conlang.Juteyuji]; - const langSelectDropdown = ( - - ); + const conlangs = [Conlang.Saimiar, Conlang.Elesu, Conlang.Tukvaysi, Conlang.Juteyuji]; + const langSelectDropdown = ( + + ); - return ( -
-
-
-

Kucinako

-
- -
-
- { langSelectDropdown } - - + return ( +
+
+
+

Kucinako

+
+ { + setSearchBoxInput(evt.target.value); + } } />
- +
+ { langSelectDropdown } + +
-
- ); - } -} + +
+
+ ); +}; export default App; diff --git a/src/dbtypes.ts b/src/dbtypes.ts index 649d8a1..5063916 100644 --- a/src/dbtypes.ts +++ b/src/dbtypes.ts @@ -14,4 +14,4 @@ interface JutEntryProps { gender: string; } -export { SaiEntryProps, JutEntryProps }; +export {SaiEntryProps, JutEntryProps};