Skip to content

Commit ba49807

Browse files
authored
Merge pull request #78 from 740fernando/feature/react
Feature/react
2 parents d150fd4 + ba27904 commit ba49807

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

09 - Consumindo a API com React/client/src/pages/Books/index.js

+36-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,41 @@ export default function Books(){
1717

1818
const history = useHistory();
1919

20+
async function logout(){
21+
localStorage.clear();
22+
history.push('/');
23+
}
24+
25+
async function editBook(id){
26+
try{
27+
history.push(`book/new/${id}`)
28+
}catch(error){
29+
alert('Edit failed! Try again.')
30+
}
31+
}
32+
33+
async function deleteBook(id){
34+
try{
35+
await api.delete(`api/book/v1/${id}`, {
36+
headers: {
37+
Authorization : `Bearer ${accessToken}`
38+
}
39+
})
40+
setBooks(books.filter(book => book.id !== id))
41+
}catch(err){
42+
alert('Delete failed! Try again.');
43+
}
44+
}
45+
2046
useEffect(()=>{
2147
api.get("api/book/v1",{
2248
headers:{
2349
Authorization: `Bearer ${accessToken}`
50+
},
51+
params: {
52+
page: 1,
53+
limit: 4,
54+
direction: 'asc'
2455
}
2556
}).then(response =>{
2657
setBooks(response.data._embedded.bookVoes)
@@ -32,16 +63,16 @@ export default function Books(){
3263
<header>
3364
<img src={logo} alt="Erudio"/>
3465
<span>Welcome, <strong> {username.toUpperCase()} </strong></span>
35-
<Link className="button" to="book/new/">Add New Book</Link>
36-
<button type="button">
66+
<Link className="button" to="book/new/0">Add New Book</Link>
67+
<button onClick={logout} type="button">
3768
<FiPower size={18} color="#251FC5" />
3869
</button>
3970
</header>
4071

4172
<h1>Registered Books</h1>
4273
<ul>
4374
{books.map(book => (
44-
<li>
75+
<li key={book.id}>
4576
<strong>Title:</strong>
4677
<p>{book.title}</p>
4778
<strong>Author:</strong>
@@ -51,11 +82,11 @@ export default function Books(){
5182
<strong>Release Date:</strong>
5283
<p>{Intl.DateTimeFormat('pt-BR').format(new Date(book.launchDate))}</p>
5384

54-
<button type='button'>
85+
<button onClick={()=> editBook(book.id)} type='button'>
5586
<FiEdit size={20} color='#251FC5'/>
5687
</button>
5788

58-
<button type='button'>
89+
<button onClick={() => deleteBook(book.id)} type='button'>
5990
<FiTrash2 size={20} color='#251FC5'/>
6091
</button>
6192
</li>

09 - Consumindo a API com React/client/src/pages/NewBook/index.js

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useState } from "react";
2-
import { useHistory, Link} from "react-router-dom";
1+
import React, { useState, useEffect } from "react";
2+
import { useHistory, Link, useParams} from "react-router-dom";
33
import {FiArrowLeft} from 'react-icons/fi';
44

55
import './styles.css';
@@ -10,17 +10,44 @@ import api from "../../services/api";
1010

1111
export default function NewBook(){
1212

13-
const [id, setPassword] = useState(null);
13+
const [id, setId] = useState(null);
1414
const [author, setAuthor] = useState('');
1515
const [launchDate, setLaunchDate] = useState('');
1616
const [price, setPrice] = useState('');
1717
const [title, setTitle] = useState('');
1818

19+
const {bookId} = useParams();
20+
1921
const username =localStorage.getItem('username');
2022
const accessToken =localStorage.getItem('accessToken');
2123

2224
const history = useHistory();
2325

26+
async function loadBook() {
27+
try{
28+
const response = await api.get(`api/book/v1/${bookId}`, {
29+
headers: {
30+
Authorization: `Bearer ${accessToken}`
31+
}
32+
})
33+
let adjustedDate = response.data.launchDate.split("T", 10)[0];
34+
35+
setId(response.data.id);
36+
setTitle(response.data.title);
37+
setAuthor(response.data.author);
38+
setPrice(response.data.price);
39+
setLaunchDate(adjustedDate)
40+
}catch(error) {
41+
alert('Error recovering book! Try again')
42+
history.push('/books');
43+
}
44+
}
45+
46+
useEffect(() => {
47+
if (bookId === '0') return;
48+
else loadBook();
49+
})
50+
2451
async function createNewBook(e){
2552
e.preventDefault();
2653

@@ -57,7 +84,7 @@ export default function NewBook(){
5784
<section className="form">
5885
<img src={logo} alt="Ntt"/>
5986
<h1>Add New Book</h1>
60-
<p>Enter the book information and click 'Add' !</p>
87+
<p>Enter the book information and click 'Add' ! ### {bookId}</p>
6188
<Link className="back-link" to="/books">
6289
<FiArrowLeft size={16} color="#251fc5" />
6390
Home

09 - Consumindo a API com React/client/src/routes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function Routes(){
1111
<Switch>
1212
<Route path='/' exact component={Login} />
1313
<Route path='/books' component={Books} />
14-
<Route path='/book/new' component={NewBook} />
14+
<Route path='/book/new/:bookId' component={NewBook} />
1515
</Switch>
1616
</BrowserRouter>
1717
);

0 commit comments

Comments
 (0)