swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 1 | const React = require("preact"); |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame^] | 2 | const { useState } = require('preact/hooks'); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 3 | |
| 4 | export const UnitsApp = () => { |
| 5 | const [hist, setHist] = useState([]); |
| 6 | const [query, setQuery] = useState(""); |
| 7 | const [to, setTo] = useState(""); |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame^] | 8 | const [unitSystem, setUnitSystem] = useState('si'); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 9 | |
| 10 | function doConversion(e) { |
| 11 | e.preventDefault(); |
| 12 | console.log("conversion"); |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame^] | 13 | |
| 14 | if (query == '') |
| 15 | return; |
| 16 | |
| 17 | console.log("units", unitSystem); |
| 18 | |
| 19 | convert(query, to, unitSystem).then(res => { |
| 20 | setHist([...hist, [query, to, res]]); |
| 21 | }); |
| 22 | |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 23 | setQuery(''); |
| 24 | setTo(''); |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | return ( |
swissChili | 16de322 | 2024-03-08 17:55:11 -0500 | [diff] [blame] | 28 | <> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 29 | <form class="query-row" onSubmit={doConversion}> |
| 30 | <input |
| 31 | type="text" |
| 32 | id="query" |
| 33 | value={query} |
| 34 | placeholder="From" |
| 35 | onChange={(val) => setQuery(val.target.value)} |
| 36 | ></input> |
| 37 | <input |
| 38 | type="text" |
| 39 | id="to" |
| 40 | value={to} |
| 41 | placeholder="To" |
| 42 | onChange={(val) => setTo(val.target.value)} |
| 43 | ></input> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame^] | 44 | |
swissChili | 16de322 | 2024-03-08 17:55:11 -0500 | [diff] [blame] | 45 | <input value="Convert" type="submit"></input> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 46 | </form> |
| 47 | |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame^] | 48 | <div class="table-container"> |
| 49 | <table> |
| 50 | <thead> |
| 51 | <tr><td style="width: 30%">From</td><td style="width: 15%">To</td><td style="width: 55%">Result</td></tr> |
| 52 | </thead> |
| 53 | {hist.map((entry) => ( |
| 54 | <tr>{entry.map(t => <td>{t}</td>)}</tr> |
| 55 | ))} |
| 56 | <tr><td></td></tr> |
| 57 | </table> |
| 58 | </div> |
| 59 | |
| 60 | <div class="bottom-menu"> |
| 61 | <span> |
| 62 | Units: |
| 63 | <select onChange={val => setUnitSystem(val.target.value)}> |
| 64 | <option value="si">SI</option> |
| 65 | <option value="gauss">CGS Gaussian</option> |
| 66 | <option value="esu">CGS ESU</option> |
| 67 | <option value="emu">CGS EMU</option> |
| 68 | <option value="lhu">CGS LHU</option> |
| 69 | </select> |
| 70 | </span> |
| 71 | |
| 72 | <button onClick={() => setHist([])}>Clear</button> |
| 73 | </div> |
| 74 | |
swissChili | 16de322 | 2024-03-08 17:55:11 -0500 | [diff] [blame] | 75 | </> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 76 | ); |
| 77 | }; |