2020-07-31 04:12:40 -07:00
|
|
|
import React, {useState, useEffect} from 'react';
|
|
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogTitle,
|
|
|
|
|
DialogContent,
|
|
|
|
|
TextField,
|
|
|
|
|
DialogActions,
|
|
|
|
|
Button,
|
|
|
|
|
} from "@material-ui/core";
|
|
|
|
|
import {useHistory} from "react-router-dom";
|
|
|
|
|
|
|
|
|
|
// styles
|
|
|
|
|
import useStyles from "./styles";
|
|
|
|
|
|
|
|
|
|
import {
|
2020-10-19 13:08:51 -04:00
|
|
|
createContactProfileRequest,
|
2020-10-19 11:43:08 -04:00
|
|
|
} from "../../squeakclient/requests"
|
2020-07-31 04:12:40 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function CreateContactProfileDialog({
|
2021-01-24 07:20:42 +02:00
|
|
|
open,
|
|
|
|
|
handleClose,
|
|
|
|
|
initialAddress='',
|
|
|
|
|
...props
|
|
|
|
|
}) {
|
2020-07-31 04:12:40 -07:00
|
|
|
var classes = useStyles();
|
|
|
|
|
const history = useHistory();
|
|
|
|
|
|
|
|
|
|
var [profileName, setProfileName] = useState('');
|
2020-08-04 20:44:33 -07:00
|
|
|
var [address, setAddress] = useState(initialAddress);
|
2020-07-31 04:12:40 -07:00
|
|
|
|
2020-08-04 21:46:17 -07:00
|
|
|
const resetFields = () => {
|
|
|
|
|
setProfileName('');
|
|
|
|
|
setAddress(initialAddress);
|
|
|
|
|
};
|
|
|
|
|
|
2020-07-31 04:12:40 -07:00
|
|
|
const handleChangeProfileName = (event) => {
|
|
|
|
|
setProfileName(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleChangeAddress = (event) => {
|
|
|
|
|
setAddress(event.target.value);
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-19 13:08:51 -04:00
|
|
|
const handleResponse = (response) => {
|
|
|
|
|
goToProfilePage(response.getProfileId());
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleErr = (err) => {
|
2021-01-24 07:20:42 +02:00
|
|
|
alert('Error creating contact profile: ' + err);
|
2020-10-19 13:08:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createContactProfile = (profileName, squeakAddress) => {
|
|
|
|
|
createContactProfileRequest(profileName, squeakAddress, handleResponse, handleErr);
|
2020-07-31 04:12:40 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const goToProfilePage = (profileId) => {
|
|
|
|
|
history.push("/app/profile/" + profileId);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function handleSubmit(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
console.log( 'profileName:', profileName);
|
|
|
|
|
console.log( 'address:', address);
|
|
|
|
|
if (!profileName) {
|
|
|
|
|
alert('Profile Name cannot be empty.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!address) {
|
|
|
|
|
alert('Address Name cannot be empty.');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-10-19 13:08:51 -04:00
|
|
|
createContactProfile(profileName, address);
|
2020-07-31 04:12:40 -07:00
|
|
|
handleClose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CreateContactProfileNameInput() {
|
|
|
|
|
return (
|
|
|
|
|
<TextField
|
|
|
|
|
id="standard-textarea"
|
|
|
|
|
label="Profile Name"
|
|
|
|
|
required
|
|
|
|
|
autoFocus
|
|
|
|
|
value={profileName}
|
|
|
|
|
onChange={handleChangeProfileName}
|
|
|
|
|
fullWidth
|
|
|
|
|
inputProps={{ maxLength: 64 }}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CreateContactAddressInput() {
|
|
|
|
|
return (
|
|
|
|
|
<TextField required
|
2021-01-24 07:20:42 +02:00
|
|
|
id="standard-textarea"
|
|
|
|
|
label="Address"
|
|
|
|
|
required
|
|
|
|
|
value={address}
|
|
|
|
|
onChange={handleChangeAddress}
|
|
|
|
|
inputProps={{ maxLength: 35 }}
|
2020-07-31 04:12:40 -07:00
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CancelButton() {
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={handleClose}
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="secondary"
|
|
|
|
|
>
|
|
|
|
|
Cancel
|
|
|
|
|
</Button>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function CreateContactProfilButton() {
|
|
|
|
|
return (
|
|
|
|
|
<Button
|
2021-01-24 07:20:42 +02:00
|
|
|
type="submit"
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="primary"
|
|
|
|
|
className={classes.button}
|
|
|
|
|
>
|
|
|
|
|
Create Contact Profile
|
|
|
|
|
</Button>
|
2020-07-31 04:12:40 -07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2020-08-04 21:46:17 -07:00
|
|
|
<Dialog open={open} onEnter={resetFields} onClose={handleClose} aria-labelledby="form-dialog-title">
|
2021-01-24 07:20:42 +02:00
|
|
|
<DialogTitle id="form-dialog-title">Create Contact Profile</DialogTitle>
|
|
|
|
|
<form className={classes.root} onSubmit={handleSubmit} noValidate autoComplete="off">
|
|
|
|
|
<DialogContent>
|
|
|
|
|
{CreateContactProfileNameInput()}
|
|
|
|
|
{CreateContactAddressInput()}
|
|
|
|
|
</DialogContent>
|
|
|
|
|
<DialogActions>
|
|
|
|
|
{CancelButton()}
|
|
|
|
|
{CreateContactProfilButton()}
|
|
|
|
|
</DialogActions>
|
|
|
|
|
</form>
|
2020-07-31 04:12:40 -07:00
|
|
|
</Dialog>
|
|
|
|
|
)
|
|
|
|
|
}
|