blob: 737a92fa0af26dd3a0ba0d9c4cb0682fa01fcfbe [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
swissChili281af442024-11-14 23:30:29 -050027 function afterEquals(str) {
28 let parts = str.split('=');
29 return parts[parts.length - 1];
30 }
31
swissChiliab615d82024-03-08 17:15:13 -050032 return (
swissChili16de3222024-03-08 17:55:11 -050033 <>
swissChili281af442024-11-14 23:30:29 -050034 <form class="query-row noselect" onSubmit={doConversion}>
swissChiliab615d82024-03-08 17:15:13 -050035 <input
36 type="text"
37 id="query"
38 value={query}
39 placeholder="From"
swissChili281af442024-11-14 23:30:29 -050040 class="noselect"
swissChiliab615d82024-03-08 17:15:13 -050041 onChange={(val) => setQuery(val.target.value)}
42 ></input>
43 <input
44 type="text"
45 id="to"
46 value={to}
47 placeholder="To"
swissChili281af442024-11-14 23:30:29 -050048 class="noselect"
swissChiliab615d82024-03-08 17:15:13 -050049 onChange={(val) => setTo(val.target.value)}
50 ></input>
swissChili4f0a0a82024-03-08 18:58:38 -050051
swissChili16de3222024-03-08 17:55:11 -050052 <input value="Convert" type="submit"></input>
swissChiliab615d82024-03-08 17:15:13 -050053 </form>
54
swissChili4f0a0a82024-03-08 18:58:38 -050055 <div class="table-container">
swissChili281af442024-11-14 23:30:29 -050056 <div class="table-title">
57 <span>Calculation</span>
58 <span>Result</span>
59 </div>
60 <div class="history">
swissChili4f0a0a82024-03-08 18:58:38 -050061 {hist.map((entry) => (
swissChili281af442024-11-14 23:30:29 -050062 <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>
swissChili4f0a0a82024-03-08 18:58:38 -050073 </div>
74
swissChili281af442024-11-14 23:30:29 -050075 <div class="bottom-menu noselect">
swissChili4f0a0a82024-03-08 18:58:38 -050076 <span>
77 Units: &nbsp;
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
swissChili16de3222024-03-08 17:55:11 -050090 </>
swissChiliab615d82024-03-08 17:15:13 -050091 );
92};