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 | |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame^] | 27 | function afterEquals(str) { |
| 28 | let parts = str.split('='); |
| 29 | return parts[parts.length - 1]; |
| 30 | } |
| 31 | |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 32 | return ( |
swissChili | 16de322 | 2024-03-08 17:55:11 -0500 | [diff] [blame] | 33 | <> |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame^] | 34 | <form class="query-row noselect" onSubmit={doConversion}> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 35 | <input |
| 36 | type="text" |
| 37 | id="query" |
| 38 | value={query} |
| 39 | placeholder="From" |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame^] | 40 | class="noselect" |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 41 | onChange={(val) => setQuery(val.target.value)} |
| 42 | ></input> |
| 43 | <input |
| 44 | type="text" |
| 45 | id="to" |
| 46 | value={to} |
| 47 | placeholder="To" |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame^] | 48 | class="noselect" |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 49 | onChange={(val) => setTo(val.target.value)} |
| 50 | ></input> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 51 | |
swissChili | 16de322 | 2024-03-08 17:55:11 -0500 | [diff] [blame] | 52 | <input value="Convert" type="submit"></input> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 53 | </form> |
| 54 | |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 55 | <div class="table-container"> |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame^] | 56 | <div class="table-title"> |
| 57 | <span>Calculation</span> |
| 58 | <span>Result</span> |
| 59 | </div> |
| 60 | <div class="history"> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 61 | {hist.map((entry) => ( |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame^] | 62 | <div class="entry"> |
| 63 | <span class="from">{entry[0]}</span> |
| 64 | { entry[1] !== "" ? <span class="to"> {entry[1]}</span> : <span />} |
| 65 | <span class="filler"> |
| 66 | <span class="equals noselect">=</span> |
| 67 | <span class="res">{afterEquals(entry[2])}</span> |
| 68 | </span> |
| 69 | </div> |
| 70 | )) |
| 71 | } |
| 72 | </div> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 73 | </div> |
| 74 | |
swissChili | 281af44 | 2024-11-14 23:30:29 -0500 | [diff] [blame^] | 75 | <div class="bottom-menu noselect"> |
swissChili | 4f0a0a8 | 2024-03-08 18:58:38 -0500 | [diff] [blame] | 76 | <span> |
| 77 | Units: |
| 78 | <select onChange={val => setUnitSystem(val.target.value)}> |
| 79 | <option value="si">SI</option> |
| 80 | <option value="gauss">CGS Gaussian</option> |
| 81 | <option value="esu">CGS ESU</option> |
| 82 | <option value="emu">CGS EMU</option> |
| 83 | <option value="lhu">CGS LHU</option> |
| 84 | </select> |
| 85 | </span> |
| 86 | |
| 87 | <button onClick={() => setHist([])}>Clear</button> |
| 88 | </div> |
| 89 | |
swissChili | 16de322 | 2024-03-08 17:55:11 -0500 | [diff] [blame] | 90 | </> |
swissChili | ab615d8 | 2024-03-08 17:15:13 -0500 | [diff] [blame] | 91 | ); |
| 92 | }; |