From 0b7ee6b3c63ec8610a5e0b4feb8178a0269f463a Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Wed, 15 Sep 2021 00:33:29 -0700 Subject: [PATCH] Code reorg --- src/App.tsx | 51 ++++--------------------------------------------- src/dbtypes.ts | 14 +++++++++++++- src/requests.ts | 35 ++++++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 49 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 5bc6d31..5788e6b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,9 +1,9 @@ import React, {useState} from "react"; import "./App.scss"; -import {SaiEntryProps, JutEntryProps, ElesuEntryProps, TukEntryProps} from "./dbtypes"; +import {SaiEntryProps, JutEntryProps, ElesuEntryProps, TukEntryProps, Conlang, SearchDirection} from "./dbtypes"; import {SaiEntry, JutEntry, ElesuEntry, TukEntry} from "./Entries"; -import {backendUrl, setPassword} from "./requests"; +import {setPassword, searchEntry} from "./requests"; const PasswordDialog = (_props) => { const [password, setPasswordStr] = useState(""); @@ -27,18 +27,6 @@ const PasswordDialog = (_props) => { ); }; -enum Conlang { - Saimiar = "saimiar", - Elesu = "elesu", - Tukvaysi = "tukvaysi", - Juteyuji = "juteyuji", -} - -enum SearchDirection { - ToConlang, - ToEnglish -} - const renderConlang = (conlang: Conlang): string => { if (conlang === Conlang.Saimiar) { return "Saimiar"; @@ -57,37 +45,6 @@ const renderConlang = (conlang: Conlang): string => { } }; -function buildRequest(searchTerm: string, conlang: Conlang, direction: SearchDirection, jsonHandler: (json: Object) => void) { - const specForConlang = { - [Conlang.Saimiar]: "sai", - [Conlang.Juteyuji]: "jut", - [Conlang.Tukvaysi]: "tuk", - [Conlang.Elesu]: "elesu", - }; - - const offset = 0; - const limit = 20; - - const conlangDb = conlang.toString(); - const conlangSpec = specForConlang[conlang]; - const field = direction === SearchDirection.ToConlang ? "eng" : conlangSpec; - - const params = new URLSearchParams([ - [field, `like.*${searchTerm}*`], - ["order", conlangSpec], - ["limit", limit], - ["offset", offset], - ] as string[][]); - - const effectiveUri = `${backendUrl}/${conlangDb}?${params}`; - - fetch(`${effectiveUri}`) - .then((resp) => resp.json()) - .then((json) => { - jsonHandler(json); - }); -} - interface EntryProps { conlang: Conlang; entry: SaiEntryProps | JutEntryProps | ElesuEntryProps | TukEntryProps; @@ -169,7 +126,7 @@ const App = (_props) => { return; } - buildRequest(searchTerm, conlang, SearchDirection.ToEnglish, (json) => { + searchEntry(searchTerm, conlang, SearchDirection.ToEnglish, (json) => { setSearchResults(json); setSearchTerm(searchTerm); setDirection(SearchDirection.ToEnglish); @@ -183,7 +140,7 @@ const App = (_props) => { setSearchTerm(null); setDirection(null); } else { - buildRequest(searchTerm, conlang, SearchDirection.ToConlang, (json) => { + searchEntry(searchTerm, conlang, SearchDirection.ToConlang, (json) => { setSearchResults(json); setSearchTerm(searchTerm); setDirection(SearchDirection.ToConlang); diff --git a/src/dbtypes.ts b/src/dbtypes.ts index 90de04d..6fa554b 100644 --- a/src/dbtypes.ts +++ b/src/dbtypes.ts @@ -1,3 +1,15 @@ +enum Conlang { + Saimiar = "saimiar", + Elesu = "elesu", + Tukvaysi = "tukvaysi", + Juteyuji = "juteyuji", +} + +enum SearchDirection { + ToConlang, + ToEnglish +} + interface SaiEntryProps { id: number; sai: string; @@ -35,4 +47,4 @@ interface TukEntryProps { notes: string; } -export {SaiEntryProps, JutEntryProps, ElesuEntryProps, TukEntryProps}; +export {SaiEntryProps, JutEntryProps, ElesuEntryProps, TukEntryProps, Conlang, SearchDirection}; diff --git a/src/requests.ts b/src/requests.ts index 52a40d1..556c41b 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -1,5 +1,7 @@ import jwt from "jsonwebtoken"; +import {Conlang, SearchDirection} from "./dbtypes"; + const backendUrl = "https://kucinakobackend.ichigo.everydayimshuflin.com"; const getPassword = (): string | null => window.sessionStorage.getItem("password"); @@ -31,4 +33,35 @@ const updateEntry = (conlangDb: string, id: number, english: string) => { fetch(request).then((resp) => console.log(resp)); }; -export {backendUrl, updateEntry, getPassword, setPassword}; +function searchEntry(searchTerm: string, conlang: Conlang, direction: SearchDirection, jsonHandler: (json: Object) => void) { + const specForConlang = { + [Conlang.Saimiar]: "sai", + [Conlang.Juteyuji]: "jut", + [Conlang.Tukvaysi]: "tuk", + [Conlang.Elesu]: "elesu", + }; + + const offset = 0; + const limit = 20; + + const conlangDb = conlang.toString(); + const conlangSpec = specForConlang[conlang]; + const field = direction === SearchDirection.ToConlang ? "eng" : conlangSpec; + + const params = new URLSearchParams([ + [field, `like.*${searchTerm}*`], + ["order", conlangSpec], + ["limit", limit], + ["offset", offset], + ] as string[][]); + + const effectiveUri = `${backendUrl}/${conlangDb}?${params}`; + + fetch(`${effectiveUri}`) + .then((resp) => resp.json()) + .then((json) => { + jsonHandler(json); + }); +} + +export {backendUrl, updateEntry, getPassword, setPassword, searchEntry};