Skip to content

Commit

Permalink
This commit add searchInput, search and SearchedList Component html a…
Browse files Browse the repository at this point in the history
…nd functionality Removed getBooks and move function from Home component and placed in App component
  • Loading branch information
ayesha-ghani098 committed Aug 18, 2021
1 parent fd12e56 commit e0b0613
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 45 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"throttle-debounce": "^3.0.1",
"web-vitals": "^1.1.2"
},
"scripts": {
Expand Down
65 changes: 63 additions & 2 deletions src/App.js
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;
11 changes: 8 additions & 3 deletions src/components/search/SearchInput.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import React, { useState } from 'react';

const SearchInput = () => {
const SearchInput = (props) => {
const { search } = props;

const [value, setValue] = useState('');

const handleSearch = () => {};
const handleSearch = (e) => {
setValue(e.target.value);
search(value);
};

return (
<div className='search-books-input-wrapper'>
<input
type='text'
value={value}
// value={value}
onChange={handleSearch}
placeholder='Search by title or author'
autoFocus
Expand Down
32 changes: 30 additions & 2 deletions src/components/search/SearchedList.js
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;
37 changes: 4 additions & 33 deletions src/containers/Home.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,20 @@
import React, { useState, useEffect } from 'react';
import React from 'react';

// Importing link from react-router-dom to link search page
import { Link } from 'react-router-dom';

// Components
import BookShelf from '../components/book/BookShelf';

// Api
import * as BooksApi from '../api/booksApi';

// Book Shelves
const bookshelves = [
{ key: 'currentlyReading', name: 'Currently Reading' },
{ key: 'wantToRead', name: 'Want to Read' },
{ key: 'read', name: 'Read' },
];

const Home = () => {
const [books, setBooks] = 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));
}
};
const Home = (props) => {
const { books, move } = props;

return (
<div className='list-books'>
Expand All @@ -57,7 +28,7 @@ const Home = () => {
key={shelf.key}
shelf={shelf}
books={books}
move={updateShelf}
move={move}
/>
))}
</div>
Expand Down
25 changes: 23 additions & 2 deletions src/containers/Search.js
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;
23 changes: 20 additions & 3 deletions src/navigation/Navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@ import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import Home from '../containers/Home';
import Search from '../containers/Search';

const Navigation = () => {
const Navigation = (props) => {
const { books, move, search, reset, searchedBooks } = props;
return (
<Router>
<Switch>
<Route exact path='/' component={Home} />
<Route exact path='/search' component={Search} />
<Route
exact
path='/'
render={() => <Home books={books} move={move} />}
/>
<Route
exact
path='/search'
render={() => (
<Search
books={books}
move={move}
search={search}
reset={reset}
searchedBooks={searchedBooks}
/>
)}
/>
<Route>Error 404</Route>
</Switch>
</Router>
Expand Down

0 comments on commit e0b0613

Please sign in to comment.