import Preact from "preact"; import { html } from "htm"; import { Consumer } from "./store.js"; /** @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 () => { return html`

Person Records

<${PersonForm}/> <${PersonTable}/>
`; };