diff --git a/website/src/components/AddDevice.tsx b/website/src/components/AddDevice.tsx index 0ab1264b..6158064a 100644 --- a/website/src/components/AddDevice.tsx +++ b/website/src/components/AddDevice.tsx @@ -1,35 +1,65 @@ import React from 'react'; -import Card from '@material-ui/core/Card'; -import CardHeader from '@material-ui/core/CardHeader'; -import CardContent from '@material-ui/core/CardContent'; + import AddIcon from '@material-ui/icons/Add'; -import CardActions from '@material-ui/core/CardActions'; import Button from '@material-ui/core/Button'; -import TextField from '@material-ui/core/TextField'; +import Container from '@material-ui/core/Container'; import Dialog from '@material-ui/core/Dialog'; import DialogTitle from '@material-ui/core/DialogTitle'; import DialogContent from '@material-ui/core/DialogContent'; import DialogActions from '@material-ui/core/DialogActions'; +import Fab from '@material-ui/core/Fab'; +import FormControl from '@material-ui/core/FormControl'; +import FormHelperText from '@material-ui/core/FormHelperText'; +import Grid from '@material-ui/core/Grid'; +import Input from '@material-ui/core/Input'; +import InputLabel from '@material-ui/core/InputLabel'; +import Paper from '@material-ui/core/Paper'; +import Typography from '@material-ui/core/Typography'; + import qrcode from 'qrcode'; -import { view } from 'react-easy-state'; import { box_keyPair } from 'tweetnacl-ts'; import { codeBlock } from 'common-tags'; -import { FormHelperText } from '@material-ui/core'; +import { makeStyles } from '@material-ui/core/styles'; + import { GetConnected } from './GetConnected'; import { IDevice, AppState } from '../Store'; -class AddDevice extends React.Component { - state = { - name: '', - open: false, - qrCodeUri: '', - configFileUri: '', - error: '', +const useStyles = makeStyles(theme => ({ + hidden: { + display: 'none', + }, + button: { + margin: theme.spacing(1), + }, + fabButton: { + position: 'absolute', + margin: '0 auto', + left: 0, + right: 0, + }, + paper: { + padding: theme.spacing(2), + }, +})); + +export default function AddDevice() { + const classes = useStyles(); + const [formOpen, setFormOpen] = React.useState(false); + const [dialogOpen, setDialogOpen] = React.useState(false); + const [error, setError] = React.useState(''); + const [name, setName] = React.useState(''); + const [qrCodeUri, setQrCodeUri] = React.useState(''); + const [configFileUri, setConfigFileUri] = React.useState(''); + + var closeForm = () => { + setFormOpen(false); + setName(''); }; - onAdd = async (event: React.FormEvent) => { + var addDevice = async (event: React.FormEvent) => { event.preventDefault(); + const keypair = box_keyPair(); const b64PublicKey = window.btoa(String.fromCharCode(...new Uint8Array(keypair.publicKey) as any)); const b64PrivateKey = window.btoa(String.fromCharCode(...new Uint8Array(keypair.secretKey) as any)); @@ -37,12 +67,12 @@ class AddDevice extends React.Component { const res = await fetch('/api/devices', { method: 'POST', body: JSON.stringify({ - name: this.state.name, + name: name, publicKey: b64PublicKey, }), }); if (res.status >= 400) { - this.setState({ error: await res.text() }); + setError(await res.text()); return; } const { device } = await res.json() as { device: IDevice }; @@ -61,64 +91,78 @@ class AddDevice extends React.Component { Endpoint = ${device.endpoint || `${window.location.hostname}:51820`} `; - this.setState({ - open: true, - qrCodeUri: await qrcode.toDataURL(configFile), - configFileUri: URL.createObjectURL(new Blob([configFile])), - }); - } + setQrCodeUri(await qrcode.toDataURL(configFile)); + setConfigFileUri(URL.createObjectURL(new Blob([configFile]))); - render() { - return ( -
- - - - this.setState({ name: event.currentTarget.value })} - style={{ marginTop: -30, marginBottom: 8 }} - fullWidth - /> - {this.state.error !== '' && {this.state.error}} - - - - - Get Connected - - - - - - - - - -
- ); - } -} + closeForm(); + setDialogOpen(true); + }; -export default view(AddDevice); + return ( + + + + + + + + + + + Get Connected + + + + + + + + + ); +} \ No newline at end of file