-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIPdetails.js
81 lines (71 loc) · 2.65 KB
/
IPdetails.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import React, { useEffect, useState } from 'react';
import { Paper, Typography } from '@mui/material';
function IPdetails() {
const [ipAddress, setIpAddress] = useState('');
const [location, setLocation] = useState(null);
const [loading, setLoading] = useState(true);
const [address, setAddress] = useState();
const [latitude, setlatitude] = useState();
const [longitude, setlongitude] = useState();
const apiKey = 'cd1c2a7f5c2541a6ac9fee043bd805a6';
useEffect(() => {
const fetchIpAddress = async () => {
try {
const response = await fetch('https://api.ipify.org?format=json');
const data = await response.json();
setIpAddress(data.ip);
setLoading(false);
} catch (error) {
console.error('Error fetching IP address:', error);
setLoading(false);
}
};
fetchIpAddress();
}, []);
useEffect(() => {
const fetchLocation = async () => {
if (ipAddress) {
try {
const response = await fetch(`https://ipapi.co/${ipAddress}/json/`);
const data = await response.json();
setLocation(data);
setlatitude(data.latitude);
setlongitude(data.longitude);
console.log("LOCATION:", data);
} catch (error) {
console.error('Error fetching location:', error);
}
}
};
fetchLocation();
}, [ipAddress]);
const fetchAddress = async () => {
try {
const response = await fetch(`https://api.opencagedata.com/geocode/v1/json?q=${latitude}+${longitude}&key=${apiKey}`);
const data = await response.json();
if (data.results && data.results.length > 0) {
const formattedAddress = data.results[0].formatted;
setAddress(formattedAddress);
console.log('Address:', formattedAddress);
} else {
console.log('No address found for the given coordinates.');
}
} catch (error) {
console.error('Error fetching address:', error);
}
};
fetchAddress();
return (
<div>
<p>IP Address: {loading ? 'Loading...' : ipAddress}</p>
{location && (
<div>
<Typography variant="subtitle1">Direccion: {address}</Typography>
<Typography variant="subtitle1">Región: {location.region}</Typography>
<Typography variant="subtitle1">Pais: {location.country_name}</Typography>
</div>
)}
</div>
);
}
export default IPdetails;