Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementação da Lógica #1

Merged
merged 6 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<html lang="pt-BR">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/src/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<title>Livros - Beon</title>
</head>
<body>
<div id="root"></div>
<main id="root"></main>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
89 changes: 88 additions & 1 deletion package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
},
"dependencies": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
"react-dom": "^18.0.0",
"react-router-dom": "^6.3.0"
},
"devDependencies": {
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@vitejs/plugin-react": "^1.3.0",
"vite": "^2.9.9"
}
}
}
42 changes: 0 additions & 42 deletions src/App.css

This file was deleted.

172 changes: 134 additions & 38 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,141 @@
import { useState } from 'react'
import logo from './logo.svg'
import './App.css'
import { useEffect, useState } from 'react';
import { Link } from 'react-router-dom';
import { nanoid } from 'nanoid';

function App() {
const [count, setCount] = useState(0)
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [page, setPage] = useState(1);
const [searchField, setSearchField] = useState('');
const [search, setSearch] = useState(searchField);
const [filter, setFilter] = useState(`?_page=${page}`);
const [filterYearMin, setfilterYearMin] = useState(null);
const [filterYearMax, setfilterYearMax] = useState(null);

useEffect(() => {
fetch(
`http:https://localhost:4000/books` +
filter +
search +
(filterYearMin ? `&year_gte=${filterYearMin}` : ``) +
(filterYearMax ? `&year_lte=${filterYearMax}` : ``)
)
.then((res) => res.json())
.then((res) => {
setLoading(false);
setData(res);
});
}, [loading, filter, search, page]);

return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>Hello Vite + React!</p>
<p>
<button type="button" onClick={() => setCount((count) => count + 1)}>
count is: {count}
</button>
</p>
<p>
Edit <code>App.jsx</code> and save to test HMR updates.
</p>
<p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
{' | '}
<a
className="App-link"
href="https://vitejs.dev/guide/features.html"
target="_blank"
rel="noopener noreferrer"
>
Vite Docs
</a>
</p>
<>
<header className='App-header'>
<h1>Lista de Livros</h1>
<p>Você pode filtrar através do campo de pesquisa.</p>
<nav>
<div id='find'>
<input
type='text'
id='findbook'
placeholder='Pesquise por autor, país ou título'
value={searchField}
onChange={({ target }) => setSearchField(target.value)}
/>
<strong>Filtrar por Ano: </strong>
<input
onChange={({ target }) => setfilterYearMin(target.value)}
type='number'
max='2099'
step='1'
id='filterYearMin'
placeholder='min'
/>
<input
onChange={({ target }) => setfilterYearMax(target.value)}
type='number'
max='2099'
step='1'
id='filterYearMax'
placeholder='max'
/>
<button
type='submit'
onClick={() => {
setLoading(true);
setPage(1);
setFilter(`?_page=1`);
setSearch(`&q=${searchField}`);
}}
>
Pesquisar
</button>
</div>
<div>
<strong>{loading ? <p>Aguarde</p> : data.length} livros encontrados</strong>
</div>
</nav>
</header>
</div>
)
{loading ? (
<h1>Carregando...</h1>
) : (
<section>
<table>
<tbody>
<tr>
<th>Título</th>
<th>Autor</th>
<th>País</th>
<th>Ano</th>
<th>Ações</th>
</tr>
{data.map((book) => (
<tr key={nanoid()}>
<td key={nanoid()}>{book.title}</td>
<td key={nanoid()}>{book.author}</td>
<td key={nanoid()}>{book.country}</td>
<td key={nanoid()}>{book.year}</td>
<td key={nanoid()}>
<Link to='/detalhes' state={book}>
Detalhes
</Link>
</td>
</tr>
))}
</tbody>
</table>
<div>
<button
onClick={() => {
setFilter(`?_page=1`);
setPage(1);
}}
disabled={page <= 1 ? true : false}
>
Início
</button>
<button
onClick={() => {
setFilter(`?_page=${page + 1}`);
setPage((prev) => (prev += 1));
}}
disabled={data.length < 10 ? true : false}
>
Avançar
</button>
<button
onClick={() => {
setFilter(`?_page=${page - 1}`);
setPage((prev) => (prev <= 2 ? (prev = 1) : (prev -= 1)));
}}
disabled={page <= 1 ? true : false}
>
Voltar
</button>
</div>
</section>
)}
</>
);
}

export default App
export default App;
Loading