From 4a73e9983a45f999a44d8c0d0e88b8735f32c967 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Wed, 15 Sep 2021 01:30:40 -0700 Subject: [PATCH] Generalize entry logic into EntryBase --- src/Entries.tsx | 98 ++++++++++++++++++++++++++++--------------------- src/requests.ts | 4 +- 2 files changed, 59 insertions(+), 43 deletions(-) diff --git a/src/Entries.tsx b/src/Entries.tsx index a8396b6..d67cf43 100644 --- a/src/Entries.tsx +++ b/src/Entries.tsx @@ -1,18 +1,21 @@ import React, {useState} from "react"; +import {Conlang} from "./dbtypes"; import {updateEntry, getPassword} from "./requests"; import {declineSaimiar} from "./saimiar_morphology"; import {SaiEntryProps, JutEntryProps, ElesuEntryProps, TukEntryProps} from "./dbtypes"; -const SaiEntry = (props: {entry: SaiEntryProps}) => { - const {entry} = props; - console.log(entry); +interface BaseProps { + id: number; + conlang: Conlang; + conlangEntry: string; + english: string; + langSpecific: React.ReactNode; +} +const EntryBase = (props: BaseProps) => { const [editing, setEditing] = useState(false); - const [english, setEnglish] = useState(entry.eng); - - const synCategory = entry.syn_category; - const isNominal = synCategory === "nominal"; + const [english, setEnglish] = useState(props.english); const mainEntryStyle = { display: "flex", @@ -27,56 +30,69 @@ const SaiEntry = (props: {entry: SaiEntryProps}) => { minWidth: "20%", }; - const edit = (evt) => { - evt.preventDefault(); - setEditing(true); + const save = () => { + updateEntry(props.conlang, props.id, english); + }; + + const engTranslation = editing ? setEnglish(evt.target.value) }/> + : english; + + const EditControls = ({onSave}: { onSave: () => void }) => { + const cancel = () => setEditing(false); + const edit = (evt) => { + evt.preventDefault(); + setEditing(true); + }; + + if (!getPassword()) { + return null; + } + + return (editing ? ( + + + ) + : Edit); }; const expand = (evt) => { evt.preventDefault(); }; - const save = () => { - updateEntry("saimiar", entry.id, english); - }; - - const cancel = () => { - setEditing(false); - }; - - const engTranslation = editing ? setEnglish(evt.target.value) }/> - : english; - - let editButton = null; - if (getPassword()) { - editButton = editing ? ( - - - ) - : Edit; - } - return ( -
+
- { entry.sai } - { engTranslation } + { props.conlangEntry } { engTranslation } Expand - { editButton } - -
-
- - { entry.syn_category } - { entry.morph_type ? `\t\t${entry.morph_type}` : null } -
- { isNominal ? formatMorphology(entry) : null } +
+ { props.langSpecific }
); }; +const SaiEntry = (props: {entry: SaiEntryProps}) => { + const {entry} = props; + + const synCategory = entry.syn_category; + const isNominal = synCategory === "nominal"; + + const langSpecific = ( +
+ + { entry.syn_category } + { entry.morph_type ? `\t\t${entry.morph_type}` : null } +
+ { isNominal ? formatMorphology(entry) : null } +
+
+ ); + + return ; +}; + function formatMorphology(entry) { const decl = declineSaimiar(entry); if (!decl) { diff --git a/src/requests.ts b/src/requests.ts index 556c41b..6e9c7dd 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -16,8 +16,8 @@ const makeAuthorizationHeader = (key: string): string => { return `Bearer ${token}`; }; -const updateEntry = (conlangDb: string, id: number, english: string) => { - const url = `${backendUrl}/${conlangDb}?id=eq.${id}`; +const updateEntry = (conlang: Conlang, id: number, english: string) => { + const url = `${backendUrl}/${conlang.toString()}?id=eq.${id}`; const request = new Request(url, { method: "PATCH",