Skip to content

Commit df146a3

Browse files
committed
Add: fetch data by geolocation
1 parent a259f13 commit df146a3

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ This project was bootstrapped with [Create React App](https://github.com/faceboo
2323

2424
- Weekly weather report
2525
- Information bubble
26-
- Fetch by location
26+
- ~~Fetch by location~~
2727
- ~~Autocomplete input~~
2828
- Responsive design
2929

src/components/utils/ApiContext.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ function ApiProvider({ children }) {
1414
const [searchText, setSearchText] = useState("");
1515
const [alert, setAlert] = useState(false);
1616
const [error, setError] = useState(null);
17-
17+
const [lat, setLat] = useState(0);
18+
const [long, setLong] = useState(0);
19+
console.log(lat,long)
1820
// you can change &lang parameter for your language
21+
1922
const apiKey = process.env.REACT_APP_API_KEY;
2023
const apiUrl = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q="${searchQuery}"&dt=2023-05-04&lang=tr&aqi=yes`;
24+
const geoUrl = `https://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${lat},${long}&dt=2023-05-04&lang=tr&aqi=yes`;
2125
const searchApiUrl = `https://api.weatherapi.com/v1/search.json?key=${apiKey}&q="${searchText}"`;
2226

27+
2328
const fetchData = () => {
2429
const options = {
2530
method: "GET",
@@ -48,6 +53,7 @@ function ApiProvider({ children }) {
4853
}, 1000);
4954
}, []);
5055

56+
5157
const fetchSearch = async () => {
5258
try {
5359
const res = await axios.get(searchApiUrl);
@@ -62,6 +68,17 @@ function ApiProvider({ children }) {
6268
useEffect(() => {
6369
fetchSearch();
6470
}, [searchApiUrl]);
71+
72+
const fetchLocation = async () => {
73+
try {
74+
const res = await axios.get(geoUrl);
75+
setData(res.data);
76+
} catch (err) {
77+
setAlert(true);
78+
setError(err);
79+
console.clear();
80+
}
81+
};
6582

6683
return (
6784
<ApiContext.Provider
@@ -79,6 +96,11 @@ function ApiProvider({ children }) {
7996
setAlert,
8097
error,
8198
setError,
99+
setLat,
100+
setLong,
101+
fetchLocation,
102+
lat,
103+
long
82104
}}
83105
>
84106
{children}

src/components/utils/Search.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useState,useEffect } from "react";
22
import { useApiContext } from "./ApiContext";
33

44
import { Input } from "@material-tailwind/react";
@@ -10,6 +10,12 @@ const Search = () => {
1010
searchData,
1111
searchText,
1212
setSearchText,
13+
setLat,
14+
setLong,
15+
fetchLocation,
16+
setError,
17+
lat,
18+
long
1319
} = useApiContext();
1420

1521
const [isTyping, setIsTyping] = useState(false);
@@ -48,12 +54,34 @@ const Search = () => {
4854
fetchData();
4955
}
5056
};
57+
const getLocation = (e) =>{
58+
e.preventDefault();
59+
try {
60+
navigator.geolocation.getCurrentPosition((position) => {
61+
setLat(position.coords.latitude);
62+
setLong(position.coords.longitude);
63+
if(lat && long != 0 ){
64+
fetchLocation()
65+
}
66+
})
67+
}
68+
catch (err) {
69+
setError(err)
70+
}
71+
72+
}
73+
useEffect(() => {
74+
if(lat && long != 0 ){
75+
fetchLocation()
76+
}
77+
}, [lat,long])
5178

79+
5280
return (
5381
<div>
5482
<div className="mt-10 flex align-center">
55-
<a className="self-center">
56-
<i className="ri-map-pin-2-line ri-xl text-white mr-8 ml-20"></i>
83+
<a className="self-center cursor-pointer" onClick={getLocation}>
84+
<i className="ri-map-pin-2-line ri-xl text-white mr-8 ml-20 hover:text-indigo-600"></i>
5785
</a>
5886
<Input
5987
variant="standard"

0 commit comments

Comments
 (0)