-
-
Save mt9304/266b22788e8a7f6144c026abffbc9441 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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