blob: 9c7b07c1fb990b858ebcbd49f3f953b15703fc56 [file] [log] [blame]
swissChiliab615d82024-03-08 17:15:13 -05001const React = require("preact");
swissChili4f0a0a82024-03-08 18:58:38 -05002const { useState } = require('preact/hooks');
swissChiliab615d82024-03-08 17:15:13 -05003
4export const UnitsApp = () => {
5 const [hist, setHist] = useState([]);
6 const [query, setQuery] = useState("");
7 const [to, setTo] = useState("");
swissChili4f0a0a82024-03-08 18:58:38 -05008 const [unitSystem, setUnitSystem] = useState('si');
swissChiliab615d82024-03-08 17:15:13 -05009
10 function doConversion(e) {
11 e.preventDefault();
12 console.log("conversion");
swissChili4f0a0a82024-03-08 18:58:38 -050013
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
swissChiliab615d82024-03-08 17:15:13 -050023 setQuery('');
24 setTo('');
swissChiliab615d82024-03-08 17:15:13 -050025 }
26
27 return (
swissChili16de3222024-03-08 17:55:11 -050028 <>
swissChiliab615d82024-03-08 17:15:13 -050029 <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>
swissChili4f0a0a82024-03-08 18:58:38 -050044
swissChili16de3222024-03-08 17:55:11 -050045 <input value="Convert" type="submit"></input>
swissChiliab615d82024-03-08 17:15:13 -050046 </form>
47
swissChili4f0a0a82024-03-08 18:58:38 -050048 <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: &nbsp;
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
swissChili16de3222024-03-08 17:55:11 -050075 </>
swissChiliab615d82024-03-08 17:15:13 -050076 );
77};