import React, { useState } from 'react';
import {
Button,
Dialog,
DialogActions,
DialogTitle,
DialogContent,
FormControlLabel,
Switch,
TextField,
} from '@material-ui/core';
import { useHistory } from 'react-router-dom';
// styles
import { makeStyles } from '@material-ui/core/styles';
import {
createPeerRequest,
} from '../../squeakclient/requests';
import {
goToPeerPage,
} from '../../navigation/navigation';
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';
export default function CreatePeerDialog({
open,
handleClose,
initialHost = '',
initialPort = '',
...props
}) {
const classes = useStyles();
const history = useHistory();
const [peerName, setPeerName] = useState('');
const [host, setHost] = useState('');
const [port, setPort] = useState('');
const [customPortChecked, setCustomPortChecked] = useState(false);
const [useTorChecked, setUseTorChecked] = useState(false);
const resetFields = () => {
setPeerName('');
setHost('');
if (initialHost) {
setHost(initialHost);
}
setPort(portDefaultValue);
setCustomPortChecked(false);
if (initialPort) {
setPort(initialPort);
setCustomPortChecked(true);
}
};
const handleChangePeerName = (event) => {
setPeerName(event.target.value);
};
const handleChangeHost = (event) => {
setHost(event.target.value);
};
const handleChangeCustomPortChecked = (event) => {
setPort(
event.target.checked ? '' : portDefaultValue,
);
setCustomPortChecked(event.target.checked);
};
const handleChangePort = (event) => {
setPort(event.target.value);
};
const handleChangeUseTorChecked = (event) => {
setUseTorChecked(event.target.checked);
};
const createPeer = (peerName, host, port) => {
createPeerRequest(peerName, host, port, useTorChecked, (response) => {
goToPeerPage(history, response.getPeerId());
});
};
function handleSubmit(event) {
event.preventDefault();
console.log('peerName:', peerName);
console.log('host:', host);
console.log('port:', port);
if (!host) {
alert('Host cannot be empty.');
return;
}
if (!port) {
alert('Port cannot be empty.');
return;
}
createPeer(peerName, host, port);
handleClose();
}
function CreatePeerNameInput() {
return (
);
}
function CreateHostInput() {
return (
);
}
function CreatePortInput() {
return (
);
}
function CustomPortSwitch() {
return (
)}
label="Use custom port"
/>
);
}
function UseTorSwitch() {
return (
)}
label="Use Tor"
/>
);
}
function CancelButton() {
return (
);
}
function CreatePeerButton() {
return (
);
}
return (
);
}