I'm creating a bookReads project (like goodreads) where you can keep tracks of your books. I have a table of books and every book is assigned a shelf. Every row has a dropdown which is used to change the shelf of the current book.
My problem is when I change the shelf of one book, it changes the shelf of all the books. I looked here, but this only talked about one particular value and not a bunch of values. I'm not sure what am I doing wrong here.
class ListBooks extends Component {
state = {
query: '',
currentShelf: 'move',
expanded: false,
truncated: false,
shelfNames: ['currentlyReading', 'wantToRead', 'read'],
lines: 4
}
updateShelf = (event) => {
this.setState({currentShelf: event.target.value})
}
updateQuery(query){
this.setState({query: query})
}
render(){
let showingBooks
if(this.state.query){
const match = new RegExp(escapeRegExp(this.state.query), 'i')
showingBooks = this.props.books.filter((book) => match.test(book.title))
} else {
showingBooks = this.props.books
}
const {
children,
more,
less,
lines
} = this.props;
const {
expanded,
truncated
} = this.state;
showingBooks.sort(sortBy('title'))
return(
<div className="list-books">
<div className="list-books-title">
<h1>BookReads</h1>
</div>
<input type="text"
placeholder="Search by title or author"
value={this.state.query}
onChange={(event) => this.updateQuery(event.target.value)}
className="search-field"
/>
<div className="list-books-content">
<div>
<Table striped bordered hover>
<thead>
<tr>
<th className="book-header">Book</th>
<th className="book-header">Author</th>
<th className="book-header"> Shelf </th>
<th></th>
</tr>
</thead>
<tbody>{showingBooks.map((book)=>(
<tr key={book.id}>
<td className="bookShelfTable">
<div className="style">
<div className="book-cover" style={{ width: 128, height: 193, float: "left", marginRight: 10,
backgroundImage: "url(" + book.imageLinks.thumbnail + ")" }} />
<h4> {book.title} </h4>
<h6> {book.subtitle}</h6>
</div> <br />
<div>
{book.description}
</div>
</td>
<td>
<div> {book.authors} </div>
</td>
<td> {this.state.currentShelf}</td>
<td>
<div className="book-shelf-changer">
<select value={this.state.currentShelf} onChange={this.updateShelf}>
<option value="move">Move to...</option>
<option value="currentlyReading">Currently Reading</option>
<option value="wantToRead">Want to Read</option>
<option value="read">Read</option>
<option value="none">None</option>
</select>
<i className="fa fa-times" onClick={() => this.props.onDeleteBook(book)}/>
</div>
</td>
</tr>
))}
</tbody>
</Table>
</div>
</div>
</div>
)
}
}