Skip to content

Instantly share code, notes, and snippets.

@amandeepmittal
Created November 4, 2018 16:07
Show Gist options
  • Save amandeepmittal/b9581ee2b832c28b136413d31ba2968c to your computer and use it in GitHub Desktop.
Save amandeepmittal/b9581ee2b832c28b136413d31ba2968c to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import IconButton from '@material-ui/core/IconButton';
import Button from '@material-ui/core//Button';
import DialogTitle from '@material-ui/core/DialogTitle';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogContent from '@material-ui/core/DialogContent';
import Dialog from '@material-ui/core/Dialog';
import Delete from '@material-ui/icons/Delete';
import auth from '../auth/auth-helper';
import { deleteUser } from '../../utils/api-user';
import { Redirect, Link } from 'react-router-dom';
class DeleteUser extends Component {
state = {
redirect: false,
open: false
};
clickButton = () => {
this.setState({ open: true });
};
deleteAccount = () => {
const jwt = auth.isAuthenticated();
deleteUser(
{
userId: this.props.userId
},
{ t: jwt.token }
).then(data => {
if (data.error) {
console.log(data.error);
} else {
auth.signout(() => console.log('deleted'));
this.setState({ redirect: true });
}
});
};
handleRequestClose = () => {
this.setState({ open: false });
};
render() {
const redirect = this.state.redirect;
if (redirect) {
return <Redirect to="/" />;
}
return (
<span>
<IconButton
aria-label="Delete"
onClick={this.clickButton}
color="secondary"
>
<Delete />
</IconButton>
<Dialog open={this.state.open} onClose={this.handleRequestClose}>
<DialogTitle>{'Delete Account'}</DialogTitle>
<DialogContent>
<DialogContentText>
Confirm to delete your account.
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={this.handleRequestClose} color="primary">
Cancel
</Button>
<Button
onClick={this.deleteAccount}
color="secondary"
autoFocus="autoFocus"
>
Confirm
</Button>
</DialogActions>
</Dialog>
</span>
);
}
}
export default DeleteUser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment