Skip to content

Commit 26beeea

Browse files
committed
Adicionado loading ao consultar API externa
1 parent ebb3d1c commit 26beeea

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"axios": "^0.25.0",
1010
"react": "^17.0.2",
1111
"react-dom": "^17.0.2",
12+
"react-loading": "^2.0.3",
1213
"react-scripts": "5.0.0",
1314
"sass": "^1.49.7",
1415
"sass-reset": "^1.0.4",

src/App.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import React, { Fragment, useState, useEffect } from 'react';
22
import { fetchWeather } from './api/fetchWeather';
3-
4-
3+
import ReactLoading from 'react-loading';
54

65
function App() {
76
const [location, setLocation] = useState(false);
87
const [query, setQuery] = useState('');
98
const [weather, setWeather] = useState({});
9+
const [loading, setLoading] = useState(false);
1010

1111
useEffect(() => {
1212
navigator.geolocation.getCurrentPosition((position) => {
1313
setLocation(true);
1414
const { latitude, longitude } = position.coords;
1515
const searchGeo = async (e) => {
16+
setLoading(true);
1617
const data = await fetchWeather(false, latitude, longitude);
18+
setLoading(false);
1719
setWeather(data);
1820
}
1921
searchGeo();
@@ -23,12 +25,14 @@ function App() {
2325

2426
const search = async (e) => {
2527
if (e.key === 'Enter') {
28+
setLoading(true);
2629
const data = await fetchWeather(query);
30+
setLoading(false);
2731
setWeather(data);
2832
setQuery('');
2933
}
3034
}
31-
35+
3236
return (
3337
<Fragment>
3438
<a href="https://github.com/raminhuk/react-weather" className="icon-github" target="_blanck">
@@ -41,12 +45,16 @@ function App() {
4145
<span className="form-search">
4246
<input type="text" className="search" placeholder="Digite uma cidade" value={query} onChange={(e) => setQuery(e.target.value)} onKeyPress={search} />
4347
</span>
48+
49+
{loading && (
50+
<ReactLoading color="#FFF" type='spin' />
51+
)}
4452
{weather.cod == 404 && (
4553
<p className="not">
4654
Cidade ou localização não encontrado
4755
</p>
4856
)}
49-
{(typeof weather.main != "undefined") ? (
57+
{(typeof weather.main != "undefined") && !loading ? (
5058
<div className="box-city">
5159
<h2 className="city-name">
5260
<span>{weather.name}</span>

0 commit comments

Comments
 (0)