Skip to content

Instantly share code, notes, and snippets.

@SilencerWeb
Created December 8, 2017 05:30
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 SilencerWeb/e89c7ca503e706ef539c845d5acf6f46 to your computer and use it in GitHub Desktop.
Save SilencerWeb/e89c7ca503e706ef539c845d5acf6f46 to your computer and use it in GitHub Desktop.
import React from 'react';
import Form from '../../components/Form';
import BooksList from '../../components/BooksList';
class App extends React.Component {
state = {
books: [
{
title: 'JavaScript and jQuery',
author: 'Дэвид Сойер Макфарланд',
year: '2017',
image: 'https://pp.userapi.com/c840636/v840636793/30188/3ekVfx6ywNA.jpg'
}
],
editBook: {},
showForm: false
};
showForm = () => {
this.setState({
showForm: true
});
};
hideForm = () => {
this.setState({
showForm: false
});
};
addBook = (book) => {
let books = [ ...this.state.books ];
books.push(book);
this.setState({
books: books,
editBook: {}
});
this.hideForm();
};
editBook = (book) => {
this.setState({
editBook: book
});
this.showForm();
};
removeBook = (removeBook) => {
let books = [ ...this.state.books ];
let bookIndex = books.findIndex((book) => {
return book.title === removeBook.title && book.author === removeBook.author;
});
books.splice(bookIndex, 1);
this.setState({
books: books
});
};
render() {
let form = this.state.showForm ?
<Form
book={ this.state.editBook }
hide={ this.hideForm }
add={ this.addBook }
/> : null;
let button = !this.state.showForm ?
<button
className="btn btn-primary"
onClick={ this.showForm }>
Добавить книгу
</button> : null;
let booksList = this.state.books ?
<BooksList
books={ this.state.books }
edit={ this.editBook }
remove={ this.removeBook }
/> : null;
return (
<div>
<h1>Книжная полка</h1>
{ form }
{ button }
{ booksList }
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment