Code reorg

This commit is contained in:
Greg Shuflin 2021-09-15 00:33:29 -07:00
parent 198bb6128a
commit 0b7ee6b3c6
3 changed files with 51 additions and 49 deletions

View File

@ -1,9 +1,9 @@
import React, {useState} from "react"; import React, {useState} from "react";
import "./App.scss"; 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 {SaiEntry, JutEntry, ElesuEntry, TukEntry} from "./Entries";
import {backendUrl, setPassword} from "./requests"; import {setPassword, searchEntry} from "./requests";
const PasswordDialog = (_props) => { const PasswordDialog = (_props) => {
const [password, setPasswordStr] = useState(""); const [password, setPasswordStr] = useState("");
@ -27,18 +27,6 @@ const PasswordDialog = (_props) => {
</dialog>); </dialog>);
}; };
enum Conlang {
Saimiar = "saimiar",
Elesu = "elesu",
Tukvaysi = "tukvaysi",
Juteyuji = "juteyuji",
}
enum SearchDirection {
ToConlang,
ToEnglish
}
const renderConlang = (conlang: Conlang): string => { const renderConlang = (conlang: Conlang): string => {
if (conlang === Conlang.Saimiar) { if (conlang === Conlang.Saimiar) {
return "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 { interface EntryProps {
conlang: Conlang; conlang: Conlang;
entry: SaiEntryProps | JutEntryProps | ElesuEntryProps | TukEntryProps; entry: SaiEntryProps | JutEntryProps | ElesuEntryProps | TukEntryProps;
@ -169,7 +126,7 @@ const App = (_props) => {
return; return;
} }
buildRequest(searchTerm, conlang, SearchDirection.ToEnglish, (json) => { searchEntry(searchTerm, conlang, SearchDirection.ToEnglish, (json) => {
setSearchResults(json); setSearchResults(json);
setSearchTerm(searchTerm); setSearchTerm(searchTerm);
setDirection(SearchDirection.ToEnglish); setDirection(SearchDirection.ToEnglish);
@ -183,7 +140,7 @@ const App = (_props) => {
setSearchTerm(null); setSearchTerm(null);
setDirection(null); setDirection(null);
} else { } else {
buildRequest(searchTerm, conlang, SearchDirection.ToConlang, (json) => { searchEntry(searchTerm, conlang, SearchDirection.ToConlang, (json) => {
setSearchResults(json); setSearchResults(json);
setSearchTerm(searchTerm); setSearchTerm(searchTerm);
setDirection(SearchDirection.ToConlang); setDirection(SearchDirection.ToConlang);

View File

@ -1,3 +1,15 @@
enum Conlang {
Saimiar = "saimiar",
Elesu = "elesu",
Tukvaysi = "tukvaysi",
Juteyuji = "juteyuji",
}
enum SearchDirection {
ToConlang,
ToEnglish
}
interface SaiEntryProps { interface SaiEntryProps {
id: number; id: number;
sai: string; sai: string;
@ -35,4 +47,4 @@ interface TukEntryProps {
notes: string; notes: string;
} }
export {SaiEntryProps, JutEntryProps, ElesuEntryProps, TukEntryProps}; export {SaiEntryProps, JutEntryProps, ElesuEntryProps, TukEntryProps, Conlang, SearchDirection};

View File

@ -1,5 +1,7 @@
import jwt from "jsonwebtoken"; import jwt from "jsonwebtoken";
import {Conlang, SearchDirection} from "./dbtypes";
const backendUrl = "https://kucinakobackend.ichigo.everydayimshuflin.com"; const backendUrl = "https://kucinakobackend.ichigo.everydayimshuflin.com";
const getPassword = (): string | null => window.sessionStorage.getItem("password"); 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)); 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};