Skip to content

Commit

Permalink
Desenvolvimento do Search e reset ao sair
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeESchmidt committed Sep 16, 2021
1 parent 62b28f0 commit e47d4d4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/components/Book/Book.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function Book({book}) {
return text.slice(0, limiter) + (text.length > limiter ? "..." : "");
}

if(!book.authors){
book.authors = ["Unknown Author"];
}

return (
<Grid item>
<Paper className={classes.paper}>
Expand Down
11 changes: 10 additions & 1 deletion src/components/Navigation/Navigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import NavigationPages from '../../data/NavigationPages';
import { Tabs, Tab, Grid, Button } from '@material-ui/core';
import { LibraryBooks, ImportContacts, Bookmarks, CheckCircle } from '@material-ui/icons';
import SearchInput from '../SearchInput';
import useSearch from '../../hooks/useSearch';

const useStyles = makeStyles((theme) => ({
link: {
Expand All @@ -20,17 +21,25 @@ function Navigation() {
const history = useHistory();
const [value, navValue] = useContent();

const search = useSearch();

const pages = NavigationPages.pages;
const pagesIcons = [<ImportContacts />, <Bookmarks />, <CheckCircle />];

const clearSearch = () => {
search[1].selected.alterarValor(null);
}

const handleChangeEvent = (event, newValue) => {
history.push(pages[newValue].link);
navValue.selected.alterarValor(newValue);
clearSearch();
};

const handleClickEvent = (event) => {
history.push('');
navValue.selected.alterarValor(0);
clearSearch();
};

const handleUseSearch = (newSelection) => {
Expand Down
32 changes: 30 additions & 2 deletions src/components/Search/Search.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
import React from 'react';
import React, { useContext, useEffect, useState } from 'react';
import Book from '../Book';
import Loading from '../Loading';
import { Grid, Container } from '@material-ui/core/';
import ApiContext from '../../contexts/ApiContext';
import useSearch from '../../hooks/useSearch';

function Search() {

const [value] = useSearch();

const context = useContext(ApiContext);

const [library, setLibrary] = useState(null);

useEffect(() => {
setLibrary(null);

context.controller.searchBooks(value, setLibrary);

}, [value, context]);

if (!library) {
return <Loading position="middle" padding={4}></Loading>;
}

return (
<>
<h1>{value}</h1>
<Container>
<Grid
container
direction="row"
alignItems="flex-start"
spacing={2}
>
{library.map((book) => <Book key={book.id} book={book}></Book>)}
</Grid>
</Container>
</>
);
}
Expand Down
12 changes: 6 additions & 6 deletions src/controllers/BooksController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBook, getMyBooks, searchBooks, updateBook } from '../api/library.js';
import { getMyBooks, searchBooks, updateBook } from '../api/library.js';
import NavigationPages from '../data/NavigationPages';

class BooksController {
Expand All @@ -19,10 +19,9 @@ class BooksController {
this._inscritos = this._inscritos.filter(f => f !== func);
}

getMyBooksFilterd(type, setDado, isDone) {
getMyBooksFilterd(type, setDado) {
getMyBooks().then(data => {
const myBooks = data.books.filter(book => book.shelf === type);
isDone = true;
setDado(myBooks);
});
}
Expand All @@ -34,9 +33,10 @@ class BooksController {

}

use() {
getBook();
searchBooks();
searchBooks(query, setDado){
searchBooks(query).then(data => {
setDado(data.books);
});
}
}

Expand Down

0 comments on commit e47d4d4

Please sign in to comment.