Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #8 from AnthonyHad/refactoring
Browse files Browse the repository at this point in the history
refactored code to look cleaner
zetabug authored Nov 3, 2022
2 parents ffbfee4 + 9964abd commit d1a83c2
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Fragment } from 'react';
import React, { useCallback } from 'react';
import { useState, useEffect } from 'react';

import Weather from './components/Weather';
@@ -28,37 +28,35 @@ function App() {
});
}

useEffect(() => {
const fetchData = useCallback(async (search, location) => {
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': '0173291af0msh62b3ca25953f210p13d732jsn66b4d9f97708',
'X-RapidAPI-Host': 'weatherapi-com.p.rapidapi.com',
},
};
const url = `https://weatherapi-com.p.rapidapi.com/current.json?q=${
search || location
}`;
const response = await fetch(url, options);
const responseData = await response.json();
return responseData;
}, []);

async function fetchdata() {
const url = `https://weatherapi-com.p.rapidapi.com/current.json?q=${
search || location
}`;
const response = await fetch(url, options).then((response) =>
response.json()
);
setPlace(response.location);
setData(response.current);
useEffect(() => {
fetchData(search, location).then((responseData) => {
setPlace(responseData.location);
setData(responseData.current);
setIsLoading(false);
}

fetchdata();
}, [search, location]);
});
}, [location, search, fetchData]);

let display = () => {
if (data) {
return <Weather place={place} data={data} />;
} else if (!data) {
return <p>no data found 😬</p>;
}
};
let display = data ? (
<Weather place={place} data={data} />
) : (
<p>no data found 😬</p>
);

return (
<>
@@ -81,7 +79,7 @@ function App() {
<br />
<br />
{isLoading && <LoadingIndicator />}
{display()}
{display}
</div>
<span className="credit">Ranvir@zetabug/github</span>
</>

0 comments on commit d1a83c2

Please sign in to comment.