esp8266-react-framework/interface/src/forms/ManageUsersForm.js

217 lines
6.2 KiB
JavaScript
Raw Normal View History

import React, { Fragment } from 'react';
2019-05-21 22:34:48 +00:00
import PropTypes from 'prop-types';
2019-05-25 08:45:49 +00:00
import { ValidatorForm } from 'react-material-ui-form-validator';
2019-05-21 22:34:48 +00:00
import { withStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableFooter from '@material-ui/core/TableFooter';
2019-05-21 22:34:48 +00:00
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Box from '@material-ui/core/Box';
2019-05-21 22:34:48 +00:00
import EditIcon from '@material-ui/icons/Edit';
import DeleteIcon from '@material-ui/icons/Delete';
2019-05-25 08:45:49 +00:00
import CloseIcon from '@material-ui/icons/Close';
import CheckIcon from '@material-ui/icons/Check';
import IconButton from '@material-ui/core/IconButton';
import SaveIcon from '@material-ui/icons/Save';
import PersonAddIcon from '@material-ui/icons/PersonAdd';
import UserForm from './UserForm';
2019-05-25 08:45:49 +00:00
import { withAuthenticationContext } from '../authentication/Context';
2019-05-21 22:34:48 +00:00
const styles = theme => ({
button: {
2019-06-02 18:01:06 +00:00
marginRight: theme.spacing(2),
marginTop: theme.spacing(2),
2019-05-21 22:34:48 +00:00
},
table: {
2019-06-02 18:01:06 +00:00
'& td, & th': { padding: theme.spacing(0.5) }
}
2019-05-21 22:34:48 +00:00
});
function compareUsers(a, b) {
if (a.username < b.username) {
return -1;
}
if (a.username > b.username) {
return 1;
}
return 0;
}
2019-05-21 22:34:48 +00:00
class ManageUsersForm extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
createUser = () => {
this.setState({
creating: true,
user: {
username: "",
password: "",
2019-05-25 08:45:49 +00:00
admin: true
}
});
};
uniqueUsername = username => {
return !this.props.userData.users.find(u => u.username === username);
}
2019-05-25 08:45:49 +00:00
noAdminConfigured = () => {
return !this.props.userData.users.find(u => u.admin);
}
removeUser = user => {
const { userData } = this.props;
const users = userData.users.filter(u => u.username !== user.username);
this.props.setData({ ...userData, users });
}
startEditingUser = user => {
this.setState({
creating: false,
user
});
};
cancelEditingUser = () => {
this.setState({
user: undefined
});
}
doneEditingUser = () => {
const { user } = this.state;
const { userData } = this.props;
const users = userData.users.filter(u => u.username !== user.username);
users.push(user);
this.props.setData({ ...userData, users });
this.setState({
user: undefined
});
};
handleUserValueChange = name => event => {
const { user } = this.state;
2019-05-25 08:45:49 +00:00
this.setState({
user: {
...user, [name]: event.target.value
}
});
};
2019-05-25 08:45:49 +00:00
handleUserCheckboxChange = name => event => {
const { user } = this.state;
this.setState({
user: {
...user, [name]: event.target.checked
}
});
}
onSubmit = () => {
this.props.onSubmit();
this.props.authenticationContext.refresh();
2019-05-25 08:45:49 +00:00
}
2019-05-21 22:34:48 +00:00
render() {
2019-08-09 17:21:28 +00:00
const { classes, userData, onReset } = this.props;
const { user, creating } = this.state;
2019-05-21 22:34:48 +00:00
return (
2019-08-09 17:21:28 +00:00
<Fragment>
<ValidatorForm onSubmit={this.onSubmit}>
<Table className={classes.table}>
<TableHead>
<TableRow>
<TableCell>Username</TableCell>
<TableCell align="center">Admin?</TableCell>
<TableCell />
</TableRow>
</TableHead>
<TableBody>
{userData.users.sort(compareUsers).map(user => (
<TableRow key={user.username}>
<TableCell component="th" scope="row">
{user.username}
</TableCell>
<TableCell align="center">
{
user.admin ? <CheckIcon /> : <CloseIcon />
}
</TableCell>
<TableCell align="center">
<IconButton aria-label="Delete" onClick={() => this.removeUser(user)}>
<DeleteIcon />
</IconButton>
<IconButton aria-label="Edit" onClick={() => this.startEditingUser(user)}>
<EditIcon />
</IconButton>
</TableCell>
</TableRow>
))}
</TableBody>
<TableFooter>
<TableRow>
<TableCell colSpan={2} />
<TableCell align="center">
<Button startIcon={<PersonAddIcon />} variant="contained" color="secondary" onClick={this.createUser}>
2019-08-09 17:21:28 +00:00
Add User
</Button>
</TableCell>
</TableRow>
</TableFooter>
</Table>
{
this.noAdminConfigured() &&
<Typography component="div" variant="body1">
<Box bgcolor="error.main" color="error.contrastText" p={2} mt={2} mb={2}>
You must have at least one admin user configured.
</Box>
</Typography>
2019-08-09 17:21:28 +00:00
}
<Button startIcon={<SaveIcon />} variant="contained" color="primary" className={classes.button} type="submit" disabled={this.noAdminConfigured()}>
2019-08-09 17:21:28 +00:00
Save
</Button>
<Button variant="contained" color="secondary" className={classes.button} onClick={onReset}>
Reset
</Button>
</ValidatorForm>
{
user &&
<UserForm
user={user}
creating={creating}
onDoneEditing={this.doneEditingUser}
onCancelEditing={this.cancelEditingUser}
handleValueChange={this.handleUserValueChange}
handleCheckboxChange={this.handleUserCheckboxChange}
uniqueUsername={this.uniqueUsername}
/>
}
</Fragment>
2019-05-21 22:34:48 +00:00
);
}
2019-05-21 22:34:48 +00:00
}
ManageUsersForm.propTypes = {
classes: PropTypes.object.isRequired,
userData: PropTypes.object,
2019-05-21 22:34:48 +00:00
onSubmit: PropTypes.func.isRequired,
onReset: PropTypes.func.isRequired,
setData: PropTypes.func.isRequired,
handleValueChange: PropTypes.func.isRequired,
authenticationContext: PropTypes.object.isRequired,
2019-05-21 22:34:48 +00:00
};
2019-05-25 08:45:49 +00:00
export default withAuthenticationContext(withStyles(styles)(ManageUsersForm));