Generalize entry logic into EntryBase

This commit is contained in:
Greg Shuflin 2021-09-15 01:30:40 -07:00
parent 0b7ee6b3c6
commit 4a73e9983a
2 changed files with 59 additions and 43 deletions

View File

@ -1,18 +1,21 @@
import React, {useState} from "react"; import React, {useState} from "react";
import {Conlang} from "./dbtypes";
import {updateEntry, getPassword} from "./requests"; import {updateEntry, getPassword} from "./requests";
import {declineSaimiar} from "./saimiar_morphology"; import {declineSaimiar} from "./saimiar_morphology";
import {SaiEntryProps, JutEntryProps, ElesuEntryProps, TukEntryProps} from "./dbtypes"; import {SaiEntryProps, JutEntryProps, ElesuEntryProps, TukEntryProps} from "./dbtypes";
const SaiEntry = (props: {entry: SaiEntryProps}) => { interface BaseProps {
const {entry} = props; id: number;
console.log(entry); conlang: Conlang;
conlangEntry: string;
english: string;
langSpecific: React.ReactNode;
}
const EntryBase = (props: BaseProps) => {
const [editing, setEditing] = useState(false); const [editing, setEditing] = useState(false);
const [english, setEnglish] = useState(entry.eng); const [english, setEnglish] = useState(props.english);
const synCategory = entry.syn_category;
const isNominal = synCategory === "nominal";
const mainEntryStyle = { const mainEntryStyle = {
display: "flex", display: "flex",
@ -27,44 +30,56 @@ const SaiEntry = (props: {entry: SaiEntryProps}) => {
minWidth: "20%", minWidth: "20%",
}; };
const save = () => {
updateEntry(props.conlang, props.id, english);
};
const engTranslation = editing ? <input type="text" value={ english } onChange={ (evt) => setEnglish(evt.target.value) }/>
: english;
const EditControls = ({onSave}: { onSave: () => void }) => {
const cancel = () => setEditing(false);
const edit = (evt) => { const edit = (evt) => {
evt.preventDefault(); evt.preventDefault();
setEditing(true); setEditing(true);
}; };
if (!getPassword()) {
return null;
}
return (editing ? (<span>
<button onClick={ onSave }>Save</button>
<button onClick={ cancel }>Cancel</button>
</span>)
: <a href="" onClick={edit}>Edit</a>);
};
const expand = (evt) => { const expand = (evt) => {
evt.preventDefault(); evt.preventDefault();
}; };
const save = () => {
updateEntry("saimiar", entry.id, english);
};
const cancel = () => {
setEditing(false);
};
const engTranslation = editing ? <input type="text" value={ english } onChange={ (evt) => setEnglish(evt.target.value) }/>
: english;
let editButton = null;
if (getPassword()) {
editButton = editing ? (<span>
<button onClick={ save }>Save</button>
<button onClick={ cancel }>Cancel</button>
</span>)
: <a href="" onClick={edit}>Edit</a>;
}
return ( return (
<div className="searchResult" key={ entry.id }> <div className="searchResult" key={ props.id }>
<div style={mainEntryStyle}> <div style={mainEntryStyle}>
<span><b>{ entry.sai }</b> - { engTranslation }</span> <span><b>{ props.conlangEntry }</b> { engTranslation }</span>
<span style={controlStyle}> <span style={controlStyle}>
<a href="" onClick={expand}>Expand</a> <a href="" onClick={expand}>Expand</a>
{ editButton } <EditControls onSave={save} />
</span> </span>
</div> </div>
{ props.langSpecific }
</div>
);
};
const SaiEntry = (props: {entry: SaiEntryProps}) => {
const {entry} = props;
const synCategory = entry.syn_category;
const isNominal = synCategory === "nominal";
const langSpecific = (
<div> <div>
<span className="synclass"> <span className="synclass">
<i>{ entry.syn_category }</i> <i>{ entry.syn_category }</i>
@ -73,8 +88,9 @@ const SaiEntry = (props: {entry: SaiEntryProps}) => {
{ isNominal ? formatMorphology(entry) : null } { isNominal ? formatMorphology(entry) : null }
</span> </span>
</div> </div>
</div>
); );
return <EntryBase id={entry.id} conlang={Conlang.Saimiar} conlangEntry={entry.sai} english={entry.eng} langSpecific={langSpecific} />;
}; };
function formatMorphology(entry) { function formatMorphology(entry) {

View File

@ -16,8 +16,8 @@ const makeAuthorizationHeader = (key: string): string => {
return `Bearer ${token}`; return `Bearer ${token}`;
}; };
const updateEntry = (conlangDb: string, id: number, english: string) => { const updateEntry = (conlang: Conlang, id: number, english: string) => {
const url = `${backendUrl}/${conlangDb}?id=eq.${id}`; const url = `${backendUrl}/${conlang.toString()}?id=eq.${id}`;
const request = new Request(url, { const request = new Request(url, {
method: "PATCH", method: "PATCH",