Fix race?

This commit is contained in:
Dominic Zimmer 2025-05-03 18:08:46 +02:00
parent 7438573c49
commit b42097f4b8

View File

@ -29,67 +29,70 @@ export const App : React.FC = () => {
const formSubmit = async () => {
const newst = addEntry(store, {variant: "eats", date: new Date().toString(), item: input} );
console.log("newstore is", newst);
await API.write(JSON.stringify(store))
const fromserver = await API.read();
setStore({...fromserver} );
setInput("");
};
const [input, setInput] = useState<string>("");
API.write(JSON.stringify(store)).then(async () => {
setTimeout(async () => {
const fromserver = await API.read();
setStore({...fromserver} );
setInput("");
}, 150);
})
};
const [input, setInput] = useState<string>("");
return (
<div className="App">
<h1>SHULNEFF</h1>
<div className="toprow">
<div className="eats">
<h2 onContextMenu={(e) => {e.preventDefault(); setShowAPIField(!showAPIField);}}>I ate ...</h2>
{showAPIField?
<input placeholder='API Token' onChange={(e) => {
localStorage.setItem("apikey", e.target.value);
}}/>
:null}
<form>
<div>
<input type="text" placeholder='Stinking Fish' onChange={(e) => {
setInput(e.target.value);}
} onKeyDown={async (e) =>{
if (e.key === "Enter") {
await formSubmit();
}
}} value={input} />
<button
onClick={formSubmit}
disabled={input.trim().length < 2}
>Submit</button>
</div>
</form>
</div>
<div className="reacts">
<button
onClick={ async () => {
const newst = addEntry(store, {variant: "allergy", date: new Date().toString()} );
await API.write(JSON.stringify(store))
const fromserver = await API.read();
setStore({...fromserver} );
}}
><h3>Allergy Alert!</h3></button>
</div>
</div>
<hr/>
{store ?
<div className={"entry-table"}>
<RenderTable API={API} store={store} setStore={setStore} />
</div>
:null}
<hr/>
<details name='foo'>
<summary>Raw Store</summary>
<pre>
{JSON.stringify(store, null, 4)}
</pre>
</details>
</div>
);
return (
<div className="App">
<h1>SHULNEFF</h1>
<div className="toprow">
<div className="eats">
<h2 onContextMenu={(e) => {e.preventDefault(); setShowAPIField(!showAPIField);}}>I ate ...</h2>
{showAPIField?
<input placeholder='API Token' onChange={(e) => {
localStorage.setItem("apikey", e.target.value);
}}/>
:null}
<form>
<div>
<input type="text" placeholder='Stinking Fish' onChange={(e) => {
setInput(e.target.value);}
} onKeyDown={async (e) =>{
if (e.key === "Enter") {
await formSubmit();
}
}} value={input} />
<button
onClick={formSubmit}
disabled={input.trim().length < 2}
>Submit</button>
</div>
</form>
</div>
<div className="reacts">
<button
onClick={ async () => {
const newst = addEntry(store, {variant: "allergy", date: new Date().toString()} );
await API.write(JSON.stringify(store))
const fromserver = await API.read();
setStore({...fromserver} );
}}
><h3>Allergy Alert!</h3></button>
</div>
</div>
<hr/>
{store ?
<div className={"entry-table"}>
<RenderTable API={API} store={store} setStore={setStore} />
</div>
:null}
<hr/>
<details name='foo'>
<summary>Raw Store</summary>
<pre>
{JSON.stringify(store, null, 4)}
</pre>
</details>
</div>
);
};
const DATEFORMAT = "YYYY MM DD HH:mm";
@ -139,10 +142,12 @@ export const RenderTable : React.FC<{API: APIEndPoint, store: Store, setStore: (
if (isSelected) {
const newst = removeEntry(store, item );
console.log("newstore is", newst);
await API.write(JSON.stringify(store))
const fromserver = await API.read();
setStore({...fromserver} );
}
API.write(JSON.stringify(store)).then(async () => {
setTimeout(async () => {
const fromserver = await API.read();
setStore({...fromserver} );
}, 150);
}
}}
key={index} className="reacts"><td>{datestring}</td>
{isSelected ?