-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit add searchInput, search and SearchedList Component html a…
…nd functionality Removed getBooks and move function from Home component and placed in App component
- Loading branch information
1 parent
fd12e56
commit e0b0613
Showing
8 changed files
with
154 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,74 @@ | ||
import React from 'react'; | ||
import React, { useState, useEffect } from 'react'; | ||
|
||
import { debounce } from 'throttle-debounce'; | ||
|
||
// Styling | ||
import './App.css'; | ||
|
||
// Api | ||
import * as BooksApi from './api/booksApi'; | ||
|
||
// Importing Navigation which contain our app links | ||
import Navigation from './navigation/Navigation'; | ||
|
||
const App = () => { | ||
return <Navigation />; | ||
const [books, setBooks] = useState([]); | ||
const [searchbooks, setSearchBooks] = useState([]); | ||
|
||
// Calling Api and fetching all books | ||
useEffect(() => { | ||
const fetchApi = async () => { | ||
await BooksApi.getAll() | ||
.then((book) => { | ||
setBooks(book); | ||
}) | ||
.catch((err) => console.log(err)); | ||
}; | ||
fetchApi(); | ||
}, []); | ||
|
||
// Move books on different shelf function | ||
const updateShelf = (book, shelf) => { | ||
console.log('Move to =>', book, shelf); | ||
BooksApi.update(book, shelf).catch((err) => { | ||
console.log(err); | ||
}); | ||
if (shelf === 'none') { | ||
setBooks(books.filter((b) => b.id !== book.id)); | ||
} else { | ||
book.shelf = shelf; | ||
setBooks(books.filter((b) => b.id !== book.id).concat(book)); | ||
} | ||
}; | ||
|
||
// Search books function | ||
const searchBooks = debounce(200, false, (query) => { | ||
if (query.length > 0) { | ||
BooksApi.search(query).then((book) => { | ||
if (book.error) { | ||
setSearchBooks([]); | ||
} else { | ||
setSearchBooks(book); | ||
} | ||
}); | ||
} else { | ||
setSearchBooks([]); | ||
} | ||
}); | ||
|
||
const resetSearch = () => { | ||
setSearchBooks([]); | ||
}; | ||
|
||
return ( | ||
<Navigation | ||
books={books} | ||
move={updateShelf} | ||
search={searchBooks} | ||
searchedBooks={searchbooks} | ||
reset={resetSearch} | ||
/> | ||
); | ||
}; | ||
|
||
export default App; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,35 @@ | ||
import React from 'react'; | ||
|
||
const SearchedList = () => { | ||
return <div></div>; | ||
// Components | ||
import Book from '../book/Book'; | ||
|
||
const SearchedList = (props) => { | ||
const { searchedBooks, books, move } = props; | ||
|
||
const searchedlist = searchedBooks.map((book) => { | ||
books.map((b) => { | ||
if (b.id === book.id) { | ||
book.shelf = b.shelf; | ||
} | ||
return b; | ||
}); | ||
return book; | ||
}); | ||
|
||
return ( | ||
<div className='search-books-results'> | ||
<ol className='books-grid'> | ||
{searchedlist.map((book) => ( | ||
<Book | ||
key={book.id} | ||
book={book} | ||
shelf={book.shelf ? book.shelf : 'none'} | ||
move={move} | ||
/> | ||
))} | ||
</ol> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchedList; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
import React from 'react'; | ||
|
||
const Search = ({ navigation }) => { | ||
return <div></div>; | ||
// Importing link from react-router-dom to link home page | ||
import { Link } from 'react-router-dom'; | ||
|
||
// Components | ||
import SearchedList from '../components/search/SearchedList'; | ||
import SearchInput from '../components/search/SearchInput'; | ||
|
||
const Search = (props) => { | ||
const { search, books, move, reset, searchedBooks } = props; | ||
|
||
return ( | ||
<div className='search-books'> | ||
<div className='search-books-bar'> | ||
<Link to='/'> | ||
<button className='close-search' onClick={reset}> | ||
Close | ||
</button> | ||
</Link> | ||
<SearchInput search={search} /> | ||
</div> | ||
<SearchedList searchedBooks={searchedBooks} books={books} move={move} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Search; |
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