Skip to content

Commit fd6ff00

Browse files
committed
TweetPage - fetch a servidor que pide Tweet por id
1 parent c9cc428 commit fd6ff00

2 files changed

Lines changed: 83 additions & 9 deletions

File tree

twitter-clone-2/src/App.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,35 @@ main > h2 {
113113
.tweet-link{
114114
text-decoration: none;
115115
}
116+
.outlet{
117+
background-color: #282c34;
118+
display: flex;
119+
flex-direction: column;
120+
justify-content: center;
121+
align-items: center;
122+
height: 75vh;
123+
}
124+
125+
/* Spinner */
126+
.spinner {
127+
border: 4px solid rgba(0, 0, 0, 0.1);
128+
border-left-color: #3498db;
129+
border-radius: 50%;
130+
width: 30px;
131+
height: 30px;
132+
animation: spin 1s infinite linear;
133+
margin: 0 auto;
134+
margin-top: 10px;
135+
}
136+
137+
@keyframes spin {
138+
0% {
139+
transform: rotate(0deg);
140+
}
141+
100% {
142+
transform: rotate(360deg);
143+
}
144+
}
116145

117146
/* .layout{
118147
border: 5px solid red;

twitter-clone-2/src/App.js

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import logo from './assets/img/logo.png';
33
import './App.css';
44
// import tweets from './assets/json/tweets.json';
55
import Tweet from './Tweet';
6-
import { Routes, Route, Outlet, Link } from "react-router-dom";
6+
import { Routes, Route, Outlet, Link, useParams } from "react-router-dom";
77
import {
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+
}
5291
function 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

Comments
 (0)