File tree Expand file tree Collapse file tree 2 files changed +42
-0
lines changed
app/javascript/components Expand file tree Collapse file tree 2 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 11import React from 'react' ;
22import { BrowserRouter as Router , Routes , Route } from 'react-router-dom' ;
33import BookList from './pages/BookList' ;
4+ import BookDetail from './pages/BookDetail' ;
45import AddBook from './pages/AddBook' ;
56import EditBook from './pages/EditBook' ;
67import Home from './pages/Home' ;
@@ -11,6 +12,7 @@ const App = () => {
1112 < Routes >
1213 < Route path = "/" element = { < Home /> } />
1314 < Route path = "/books" element = { < BookList /> } />
15+ < Route path = "/books/:id" element = { < BookDetail /> } />
1416 < Route path = "/books/new" element = { < AddBook /> } />
1517 < Route path = "/books/:id/edit" element = { < EditBook /> } />
1618 </ Routes >
Original file line number Diff line number Diff line change 1+ import React , { useEffect , useState } from 'react' ;
2+ import { useParams } from 'react-router-dom' ;
3+
4+ const BookDetail = ( ) => {
5+ const { id } = useParams ( ) ;
6+ const [ book , setBook ] = useState ( null ) ;
7+ const [ recommender , setRecommender ] = useState ( null ) ;
8+ const [ error , setError ] = useState ( null ) ;
9+
10+ useEffect ( ( ) => {
11+ const fetchBook = async ( ) => {
12+ const response = await fetch ( `http://localhost:3000/api/v1/books/${ id } ` ) ;
13+
14+ const bookData = await response . json ( ) ;
15+ setBook ( bookData . data ) ;
16+ const recommenderData = bookData . included . find (
17+ ( include ) => include . type === 'user'
18+ ) ;
19+ setRecommender ( recommenderData ) ;
20+ } ;
21+
22+ fetchBook ( ) ;
23+ } , [ id ] ) ;
24+
25+ return (
26+ < div className = "container my-5" >
27+ < h1 className = "text-center mb-4" > { book ?. attributes . title } </ h1 >
28+ < h4 className = "text-center text-muted" > { book ?. attributes . author } </ h4 >
29+ < p > { book ?. attributes . description || 'No description available.' } </ p >
30+ < hr />
31+ { recommender && (
32+ < p className = "text-muted" >
33+ Recommended by: { recommender ?. attributes . email }
34+ </ p >
35+ ) }
36+ </ div >
37+ ) ;
38+ } ;
39+
40+ export default BookDetail ;
You can’t perform that action at this time.
0 commit comments