2021-01-07 09:58:29 +02:00
|
|
|
import React, { useState } from 'react';
|
2020-08-01 02:43:48 -07:00
|
|
|
import {
|
2021-01-07 09:58:29 +02:00
|
|
|
Button,
|
2020-08-01 02:43:48 -07:00
|
|
|
Dialog,
|
2021-01-07 09:58:29 +02:00
|
|
|
DialogActions,
|
2020-08-01 02:43:48 -07:00
|
|
|
DialogTitle,
|
|
|
|
|
DialogContent,
|
2021-01-07 09:58:29 +02:00
|
|
|
FormControlLabel,
|
|
|
|
|
Switch,
|
2020-08-01 02:43:48 -07:00
|
|
|
TextField,
|
|
|
|
|
} from "@material-ui/core";
|
2021-01-07 09:58:29 +02:00
|
|
|
import { useHistory } from "react-router-dom";
|
2020-08-01 02:43:48 -07:00
|
|
|
|
|
|
|
|
// styles
|
2021-01-07 09:58:29 +02:00
|
|
|
import {makeStyles} from '@material-ui/core/styles';
|
2020-08-01 02:43:48 -07:00
|
|
|
|
|
|
|
|
import {
|
2020-10-19 13:08:51 -04:00
|
|
|
createPeerRequest,
|
2020-10-19 11:43:08 -04:00
|
|
|
} from "../../squeakclient/requests"
|
2020-08-01 02:43:48 -07:00
|
|
|
|
2021-01-07 09:58:29 +02:00
|
|
|
const useStyles = makeStyles((theme) => ({
|
|
|
|
|
form: {
|
|
|
|
|
margin: 'auto',
|
|
|
|
|
width: 'fit-content',
|
|
|
|
|
'& .MuiDialogContent-root': {
|
|
|
|
|
overflow: `hidden`
|
|
|
|
|
},
|
|
|
|
|
'& .MuiTextField-root': {
|
|
|
|
|
margin: theme.spacing(1),
|
|
|
|
|
},
|
|
|
|
|
'& .MuiDialogActions-root': {
|
|
|
|
|
padding: `1rem`,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
formControlLabel: {
|
|
|
|
|
position: `absolute`,
|
|
|
|
|
left: `2rem`,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const portDefaultValue = '0';
|
2020-08-01 02:43:48 -07:00
|
|
|
|
2020-08-03 04:20:05 -07:00
|
|
|
export default function CreatePeerDialog({
|
2020-08-01 02:43:48 -07:00
|
|
|
open,
|
|
|
|
|
handleClose,
|
|
|
|
|
...props
|
|
|
|
|
}) {
|
2021-01-07 09:58:29 +02:00
|
|
|
const classes = useStyles();
|
2020-08-01 02:43:48 -07:00
|
|
|
const history = useHistory();
|
|
|
|
|
|
2021-01-07 09:58:29 +02:00
|
|
|
const [peerName, setPeerName] = useState('');
|
|
|
|
|
const [host, setHost] = useState('');
|
|
|
|
|
const [port, setPort] = useState('');
|
|
|
|
|
const [customPortChecked, setCustomPortChecked] = React.useState(false)
|
2020-08-01 02:43:48 -07:00
|
|
|
|
2020-08-04 21:46:17 -07:00
|
|
|
const resetFields = () => {
|
2021-01-07 09:58:29 +02:00
|
|
|
setPeerName('');
|
2020-08-04 21:46:17 -07:00
|
|
|
setHost('');
|
2021-01-07 09:58:29 +02:00
|
|
|
setPort(portDefaultValue);
|
|
|
|
|
setCustomPortChecked(false);
|
2020-08-04 21:46:17 -07:00
|
|
|
};
|
|
|
|
|
|
2020-08-03 04:20:05 -07:00
|
|
|
const handleChangePeerName = (event) => {
|
2021-01-07 09:58:29 +02:00
|
|
|
setPeerName(event.target.value);
|
2020-08-01 02:43:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleChangeHost = (event) => {
|
|
|
|
|
setHost(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-07 09:58:29 +02:00
|
|
|
const handleChangeCustomPortChecked = (event) => {
|
|
|
|
|
setPort(
|
|
|
|
|
event.target.checked ? '' : portDefaultValue
|
|
|
|
|
);
|
|
|
|
|
setCustomPortChecked(event.target.checked);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 15:07:15 -07:00
|
|
|
const handleChangePort = (event) => {
|
|
|
|
|
setPort(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-19 13:08:51 -04:00
|
|
|
const createPeer = (peerName, host, port) => {
|
|
|
|
|
createPeerRequest(peerName, host, port, (response) => {
|
2020-08-03 04:35:29 -07:00
|
|
|
goToPeerPage(response.getPeerId());
|
2020-08-01 02:43:48 -07:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-03 04:35:29 -07:00
|
|
|
const goToPeerPage = (peerId) => {
|
|
|
|
|
history.push("/app/peer/" + peerId);
|
2020-08-01 02:43:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function handleSubmit(event) {
|
|
|
|
|
event.preventDefault();
|
2020-08-03 04:20:05 -07:00
|
|
|
console.log( 'peerName:', peerName);
|
2020-08-01 02:43:48 -07:00
|
|
|
console.log( 'host:', host);
|
2020-08-01 15:07:15 -07:00
|
|
|
console.log( 'port:', port);
|
2020-08-01 02:43:48 -07:00
|
|
|
if (!host) {
|
|
|
|
|
alert('Host cannot be empty.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-01 15:07:15 -07:00
|
|
|
if (!port) {
|
|
|
|
|
alert('Port cannot be empty.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-10-19 13:08:51 -04:00
|
|
|
createPeer(peerName, host, port);
|
2020-08-01 02:43:48 -07:00
|
|
|
handleClose();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 04:20:05 -07:00
|
|
|
function CreatePeerNameInput() {
|
2020-08-01 02:43:48 -07:00
|
|
|
return (
|
|
|
|
|
<TextField
|
2021-01-07 09:58:29 +02:00
|
|
|
required
|
|
|
|
|
variant="outlined"
|
2020-08-03 04:20:05 -07:00
|
|
|
label="Peer Name"
|
2020-08-01 02:43:48 -07:00
|
|
|
autoFocus
|
2020-08-03 04:20:05 -07:00
|
|
|
value={peerName}
|
|
|
|
|
onChange={handleChangePeerName}
|
2020-08-01 02:43:48 -07:00
|
|
|
fullWidth
|
|
|
|
|
inputProps={{ maxLength: 64 }}
|
2021-01-07 09:58:29 +02:00
|
|
|
margin="normal"
|
2020-08-01 02:43:48 -07:00
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CreateHostInput() {
|
|
|
|
|
return (
|
2021-01-07 09:58:29 +02:00
|
|
|
<TextField
|
|
|
|
|
required
|
|
|
|
|
variant="outlined"
|
|
|
|
|
label="Host"
|
|
|
|
|
value={host}
|
|
|
|
|
onChange={handleChangeHost}
|
|
|
|
|
inputProps={{ maxLength: 128 }}
|
|
|
|
|
margin="normal"
|
2020-08-01 02:43:48 -07:00
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 15:07:15 -07:00
|
|
|
function CreatePortInput() {
|
|
|
|
|
return (
|
2021-01-07 09:58:29 +02:00
|
|
|
<TextField
|
|
|
|
|
required={customPortChecked}
|
|
|
|
|
variant="outlined"
|
|
|
|
|
label="Port"
|
|
|
|
|
value={customPortChecked ? port : ''}
|
|
|
|
|
onChange={handleChangePort}
|
|
|
|
|
inputProps={{ maxLength: 8 }}
|
|
|
|
|
disabled={!customPortChecked}
|
|
|
|
|
margin="normal"
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function CustomPortSwitch() {
|
|
|
|
|
return (
|
|
|
|
|
<FormControlLabel
|
|
|
|
|
className={classes.formControlLabel}
|
|
|
|
|
control={
|
|
|
|
|
<Switch
|
|
|
|
|
checked={customPortChecked}
|
|
|
|
|
onChange={handleChangeCustomPortChecked}
|
|
|
|
|
name="use-custom-port"
|
|
|
|
|
size="small"
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
label="Use custom port"
|
2020-08-01 15:07:15 -07:00
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 09:58:29 +02:00
|
|
|
|
2020-08-01 02:43:48 -07:00
|
|
|
function CancelButton() {
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={handleClose}
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="secondary"
|
|
|
|
|
>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 04:20:05 -07:00
|
|
|
function CreatePeerButton() {
|
2020-08-01 02:43:48 -07:00
|
|
|
return (
|
|
|
|
|
<Button
|
2021-01-07 09:58:29 +02:00
|
|
|
type="submit"
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
className={classes.button}
|
|
|
|
|
>
|
|
|
|
|
Create Peer
|
|
|
|
|
</Button>
|
2020-08-01 02:43:48 -07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2021-01-07 09:58:29 +02:00
|
|
|
<Dialog
|
|
|
|
|
open={open}
|
|
|
|
|
onEnter={resetFields}
|
|
|
|
|
onClose={handleClose}
|
|
|
|
|
aria-labelledby="form-dialog-title"
|
|
|
|
|
maxWidth="sm"
|
|
|
|
|
>
|
|
|
|
|
<DialogTitle id="form-dialog-title">
|
|
|
|
|
Create Peer
|
|
|
|
|
</DialogTitle>
|
|
|
|
|
<form
|
|
|
|
|
className={classes.form}
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
noValidate
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
>
|
|
|
|
|
<DialogContent>
|
|
|
|
|
{CreatePeerNameInput()}
|
|
|
|
|
{CreateHostInput()}
|
|
|
|
|
{CreatePortInput()}
|
|
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
|
|
|
|
{CustomPortSwitch()}
|
|
|
|
|
{CancelButton()}
|
|
|
|
|
{CreatePeerButton()}
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</form>
|
2020-08-01 02:43:48 -07:00
|
|
|
</Dialog>
|
|
|
|
|
)
|
|
|
|
|
}
|