@@ -3,7 +3,7 @@ import logo from './assets/img/logo.png';
33import './App.css' ;
44// import tweets from './assets/json/tweets.json';
55import Tweet from './Tweet' ;
6- import { Routes , Route , Outlet , Link } from "react-router-dom" ;
6+ import { Routes , Route , Outlet , Link , useParams } from "react-router-dom" ;
77import {
88 SignedIn ,
99 SignedOut ,
@@ -38,7 +38,14 @@ function App() {
3838 } />
3939 < Route path = "about" element = { < About /> } />
4040 < Route path = "dashboard" element = { < Dashboard /> } />
41-
41+ < Route
42+ path = "/tweets/:id"
43+ loader = { ( { params } ) => {
44+ console . log ( params . id ) ; // 1, 2, 3...
45+ } }
46+ action = { ( { params } ) => { } }
47+ element = { < TweetPage /> }
48+ /> ;
4249 { /* Using path="*"" means "match anything", so this route
4350 acts like a catch-all for URLs that we don't have explicit
4451 routes for. */ }
@@ -49,6 +56,38 @@ function App() {
4956 ) ;
5057}
5158
59+ function TweetPage ( ) {
60+ let params = useParams ( ) ;
61+
62+ const [ tweetId , setTweetId ] = useState ( params . id )
63+ const [ tweet , setTweet ] = useState ( { } )
64+
65+ useEffect ( ( ) => {
66+ // IDEA: poner una condición para que pida los tweets cada 1, 5 o 10 min como mínimo
67+ const fetchTweetById = ( ) => {
68+ fetch ( "https://79.143.92.203:3000/api/cdm/tweets/" + tweetId )
69+ . then ( response => {
70+ return response . json ( )
71+ } )
72+ . then ( tweet => {
73+ setTweet ( tweet ) ;
74+ console . log ( tweet ) ;
75+ } )
76+ }
77+ fetchTweetById ( )
78+ } , [ ] ) ;
79+ return (
80+ < >
81+ < div style = { { color : "white" } } >
82+ < h2 > Tuit #{ params . id } </ h2 >
83+ { /* {tweet && <Tweet {...tweet} /> } */ }
84+ { tweet && Object . keys ( tweet ) . length > 0
85+ ? < Tweet { ...tweet } />
86+ : < div > < div className = "spinner" > </ div > < p > Cargando tweet...</ p > </ div > }
87+ </ div >
88+ </ >
89+ )
90+ }
5291function Layout ( ) {
5392 return (
5493 < >
@@ -132,7 +171,7 @@ function Home() {
132171
133172 useEffect ( ( ) => {
134173 // IDEA: poner una condición para que pida los tweets cada 1, 5 o 10 min como mínimo
135- const fetchUserData = ( ) => {
174+ const fetchTweets = ( ) => {
136175 fetch ( "https://79.143.92.203:3000/api/cdm/tweets" )
137176 . then ( response => {
138177 return response . json ( )
@@ -141,21 +180,27 @@ function Home() {
141180 setTweets ( tweets )
142181 } )
143182 }
144- fetchUserData ( )
183+ fetchTweets ( )
145184 } , [ ] ) ;
146185
147186 return (
148- < div style = { { paddingBottom : "10px" , marginBottom : "0" } } >
187+ < div style = { { paddingBottom : "10px" , marginBottom : "0" , backgroundColor : "#282c34" } } >
149188 < h2 > Home</ h2 >
150- < main >
151- < TweetForm />
189+ < main style = { { width : "100vw" } } >
190+ { /* TODO: Revisar T_T */ }
191+ { /* <TweetForm /> */ }
152192 < h2 > Últimos tweets</ h2 >
153- < div >
193+ < div style = { { width : "100%" } } >
154194 { /* Añadimos el operador && para que en caso de que no haya tweets la expresión no se ejecute -> el div aparece sin contenido */ }
155195
156196 { /* TODO: hablar de esto */ }
157197 { tweets && tweets . map ( ( { id, content, created_on, author} ) => (
158- < Tweet key = { id } id = { id } author = { author } content = { content } created_on = { created_on } />
198+ < Tweet
199+ key = { id }
200+ id = { id }
201+ author = { author }
202+ content = { content }
203+ created_on = { created_on } />
159204 ) ) }
160205 </ div >
161206 </ main >
0 commit comments