Skip to content

Instantly share code, notes, and snippets.

@mt9304
Last active October 17, 2018 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mt9304/266b22788e8a7f6144c026abffbc9441 to your computer and use it in GitHub Desktop.
Save mt9304/266b22788e8a7f6144c026abffbc9441 to your computer and use it in GitHub Desktop.
class Book extends React.Component {
constructor(props) {
super(props);
this.state = props;
}
handleDelete(e) {
if (confirm('Are you sure you want to delete book: ' + this.state.book.name+'?')) {
var self = this;
var bookId = this.state.book.id;
e.preventDefault();
$.ajax({
method: 'DELETE',
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
url: '/api/books/' + bookId,
success: function(data) {
location.reload();
}.bind(this),
error: function(xhr, status, error) {
alert('Cannot delete requested record. ');
}
});
} else {
console.log('Cancelled delete action. ');
}
}
render() {
var bookLink = "/api/books/1=" + this.state.book.id;
return (
<tr>
<td><a className="ta-white" href={bookLink}>{this.state.book.title}</a></td>
<td>{this.state.book.author}</td>
<td>{this.state.book.description}</td>
<td>{this.state.book.pages}</td>
<td>{this.state.book.published}</td>
<td>
<a className="btn btn-danger btn-xs"
onClick={this.handleDelete.bind(this)} >
Delete</a>
</td>
</tr>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment