import Preact from "preact";
import { html } from "htm";
import { Consumer } from "./store.js";
import * as Tree from "./tree.js";
import React from "preact";
/** @typedef {(e:SubmitEvent|Event)=>void|null} handler */
/** @type {(props:{person:Store.Person })=>preact.VNode} */
const Person = (props) => {
const [, dispatch] = Consumer();
/** @type {handler} */ const handleClick = () => {
dispatch({ Key: "person-delete", Arg: props.person });
};
return html`
${props.person.Name}
${props.person.Age}
`;
};
/** @type {()=>preact.VNode} */
const PersonTable = () => {
const [state] = Consumer();
return html`People
${
state.People.length > 0
? html`
Delete
Name
Age
${state.People.map((p) => html`<${Person} person=${p}/>`)}
`
: html`
No people created
`
}
`;
};
/** @type {()=>preact.VNode} */
const PersonForm = () => {
const [, dispatch] = Consumer();
const [nameGet, nameSet] = Preact.useState("default name");
const [ageGet, ageSet] = Preact.useState(21);
/** @type {handler} */
const handleSubmit = (e) => {
e.preventDefault();
dispatch({ Key: "person-create", Arg: { Name: nameGet, Age: ageGet } });
};
/** @type {handler} */
const handleName = (e) => {
e.target && nameSet(/** @type {HTMLInputElement}*/ (e.target).value);
};
/** @type {handler} */
const handleAge = (e) => {
e.target &&
ageSet(parseInt(/** @type {HTMLInputElement}*/ (e.target).value));
};
return html`
Add Person
`;
};
/** @type {()=>preact.VNode} */
export default () => {
let [countGet, countSet] = Preact.useState(0);
let ref = Preact.useRef(null);
Tree.useAway(ref, () => {
countSet(countGet + 1);
});
return html`
Person Records ${countGet}
<${PersonForm}/>
<${PersonTable}/>
<${Tree.Branch}>
<${Tree.Button}/>
<${Tree.Menu} class="shadow-lg rounded-lg bg-white">
hey
<${Tree.Branch}>
<${Tree.Button} class="p-4 bg-red-500 text-white"/>
<${Tree.Menu}>
sup.
/>
/>
/>
/>
`;
};