import { createEffect, createResource, createSignal, ErrorBoundary, For, Show, } from "solid-js"; import { openDatabase } from "./data"; import Graph from "./Graph"; type Unit = "kg" | "lb"; export default function App() { const [database] = createResource(openDatabase); const [entries, { refetch }] = createResource(database, async (database) => { const entries = await database.getAll("entries"); console.debug("entries", entries); return entries; }); async function handleSubmit( event: SubmitEvent & { currentTarget: HTMLFormElement } ) { event.preventDefault(); console.debug("handleSubmit", event); const input = event.currentTarget.elements.namedItem("weight"); if (!(input instanceof HTMLInputElement)) throw new Error("Expected input element"); const weight = input.valueAsNumber; const currentDatabase = database(); const now = new Date(); currentDatabase?.add( "entries", { timestamp: now, weight, }, now.getTime() ); event.currentTarget.reset(); refetch(); } async function remove(date: Date) { const currentDatabase = database(); if (!currentDatabase) throw new Error("Expected database to be ready"); currentDatabase.delete("entries", date.getTime()); refetch(); } const [unit, setUnit] = createSignal("kg"); function format(weight: number, unit: Unit) { if (unit === "kg") return weight; // If 1lb = 0.45359237kg // Then divide both sides by 45359237 to get 1/45359237lb and 0.00000001kg // Multiply both sides by 100_000_000 to get 2.2046226218lbs = 1kg // 1/45359237*100_000_000 return weight * 2.2046226218; } let weightInput: HTMLInputElement | undefined; createEffect(() => { // Need to access unit here to register it as a effect dependency const currentUnit = unit(); if (!weightInput?.valueAsNumber) return; weightInput.valueAsNumber = currentUnit === "kg" ? weightInput.valueAsNumber / 2.2046226218 : weightInput.valueAsNumber * 2.2046226218; }); const [main, setMain] = createSignal(); const width = () => { const element = main(); if (element === undefined) return; const clientWidth = element.clientWidth; const styles = getComputedStyle(element); return ( clientWidth - parseFloat(styles.paddingLeft) - parseFloat(styles.paddingRight) ); }; const properties = () => { const currentWidth = width(); const currentEntries = entries(); if (currentWidth === undefined || currentEntries === undefined) return; return { width: () => currentWidth, entries: () => currentEntries }; }; return (

Waight

(

Error creating graph

Error
{error.message}
{error}
)} > {(properties) => }
    {(entry) => (
  1. {format(entry.weight, unit()).toLocaleString(undefined, { maximumFractionDigits: 2, style: unit() === "kg" ? "unit" : "currency", currency: unit() === "lb" ? "gbp" : undefined, unit: unit() === "kg" ? "kilogram" : undefined, })} {/* {unit() === "kg" ? "㎏" : "£"} */}

  2. )}
{/*
Unit
*/}
); }