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" 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, ...props }) { const classes = useStyles(); const history = useHistory(); const [peerName, setPeerName] = useState(''); const [host, setHost] = useState(''); const [port, setPort] = useState(''); const [customPortChecked, setCustomPortChecked] = React.useState(false) const resetFields = () => { setPeerName(''); setHost(''); setPort(portDefaultValue); setCustomPortChecked(false); }; 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 createPeer = (peerName, host, port) => { createPeerRequest(peerName, host, port, (response) => { goToPeerPage(response.getPeerId()); }); }; const goToPeerPage = (peerId) => { history.push("/app/peer/" + peerId); }; 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 CancelButton() { return ( ) } function CreatePeerButton() { return ( ) } return ( Create Peer
{CreatePeerNameInput()} {CreateHostInput()} {CreatePortInput()} {CustomPortSwitch()} {CancelButton()} {CreatePeerButton()}
) }