-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.tsx
119 lines (114 loc) · 3.41 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import * as React from 'react';
import Button from 'react-bootstrap/Button';
import Form from 'react-bootstrap/Form';
import InputGroup from 'react-bootstrap/InputGroup';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import { ChangeEvent, useState } from 'react';
import { names, colors, createUrl } from './src/Generator';
import './style.css';
import 'bootstrap/dist/css/bootstrap.css';
function mapOptions(o: object) {
return Object.keys(o).map((k) => <option value={k}>{o[k]}</option>);
}
function ScanButton(props: {
setUid: React.Dispatch<React.SetStateAction<string>>;
}) {
const scan = () => {
const ndef = new NDEFReader();
ndef.scan().then(() => {
ndef.onreadingerror = (event) => {
console.log('Error:', event);
};
ndef.onreading = (event) => {
props.setUid(event.serialNumber);
};
});
};
return <Button onClick={scan}>Scan</Button>;
}
export default function App() {
const [fid, setFid] = useState<string>('16');
const [uid, setUid] = useState<string>('');
const [url, setUrl] = useState<string>();
const onTypeChange = (event: ChangeEvent<HTMLSelectElement>) => {
setFid(event.target.value);
};
const onUidChange = (event: ChangeEvent<HTMLInputElement>) => {
setUid(event.target.value);
};
const getUidFromClipboard = () => {
navigator.clipboard.readText().then((clipText) => {
setUid(clipText);
});
};
const copyUrlToClipboard = () => {
navigator.clipboard.writeText(url);
};
const generate = () => {
createUrl(Number.parseInt(fid), uid).then((v) => {
console.log(v);
setUrl(v);
});
};
return (
<React.Fragment>
<h1>Jooki URL Generator</h1>
<Form>
<p>Select the type and click generate to create the URL</p>
<Form.Group controlId="color" as={Row} className="mb-3">
<Form.Label column sm={1}>
Type
</Form.Label>
<Col>
<Form.Select value={fid} onChange={onTypeChange}>
{mapOptions(names)}
{mapOptions(colors)}
</Form.Select>
</Col>
</Form.Group>
<Form.Group controlId="UID" as={Row} className="mb-3">
<Form.Label column sm={1}>
UID
</Form.Label>
<Col>
<InputGroup>
<Form.Control value={uid} onChange={onUidChange} />
<Button
variant="secondary"
size="sm"
onClick={getUidFromClipboard}
>
Paste From Clipboard
</Button>
{window.NDEFReader && <ScanButton setUid={setUid} />}
</InputGroup>
</Col>
</Form.Group>
<Form.Group as={Row} className="mb-3">
<Col className="d-grid gap-2">
<Button variant="primary" onClick={generate} size="lg">
Generate
</Button>
</Col>
</Form.Group>
{url && (
<Form.Group as={Row}>
<Col>
<InputGroup>
<Form.Control value={url} readOnly />
<Button
variant="secondary"
size="sm"
onClick={copyUrlToClipboard}
>
Copy To Clipboard
</Button>
</InputGroup>
</Col>
</Form.Group>
)}
</Form>
</React.Fragment>
);
}