Created fetchAPI function

This commit is contained in:
z0ccc 2021-08-26 14:10:24 -04:00
parent 4eb65a0c91
commit 783eee3799
4 changed files with 25 additions and 24 deletions

View file

@ -25,6 +25,7 @@ module.exports = {
'one-var': 'off', 'one-var': 'off',
'one-var-declaration-per-line': 'off', 'one-var-declaration-per-line': 'off',
'object-curly-newline': 'off', 'object-curly-newline': 'off',
'implicit-arrow-linebreak': 'off',
'import/extensions': [ 'import/extensions': [
'error', 'error',
'ignorePackages', 'ignorePackages',

View file

@ -1,28 +1,20 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import ScanBlock from './ScanBlock'; import ScanBlock from './ScanBlock';
import Table from './Table'; import Table from './Table';
import { getConnection } from './main'; import { fetchAPI, getConnection } from './main';
const ConnectionBlock = () => { const ConnectionBlock = () => {
const [data, setData] = useState(''); const [data, setData] = useState('');
const [display, setDisplay] = useState(''); const [display, setDisplay] = useState('');
useEffect(() => { useEffect(() => {
fetch('https://api.vytal.io/ip/') fetchAPI(setData, setDisplay);
.then((response) => response.json())
.then((json) => {
setData(getConnection(json));
setDisplay(1);
})
.catch(() => {
setDisplay(0);
});
}, []); }, []);
return ( return (
<ScanBlock> <ScanBlock>
<h1>Connection</h1> <h1>Connection</h1>
{display === 1 && <Table data={data} />} {display === 1 && <Table data={getConnection(data)} />}
{display === 0 && ( {display === 0 && (
<div className="boxWrap"> <div className="boxWrap">
Unable to fetch info. Adblock or content filter may have prevented Unable to fetch info. Adblock or content filter may have prevented

View file

@ -1,23 +1,14 @@
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
import ScanBlock from './ScanBlock'; import ScanBlock from './ScanBlock';
import Table from './Table'; import Table from './Table';
import { getLocation } from './main'; import { fetchAPI, getLocation, getMap } from './main';
const LocationBlock = () => { const LocationBlock = () => {
const [data, setData] = useState([]); const [data, setData] = useState([]);
const [display, setDisplay] = useState(''); const [display, setDisplay] = useState('');
const mapUrl = `https://maps.googleapis.com/maps/api/staticmap?center=${data.lat},${data.lon}&markers=color:red%7Clabel:%7C${data.lat},${data.lon}&size=500x200&zoom=10&key=AIzaSyB-YN-X8PGBSPd7NOaQu4csVhgJUnF3ZGk`;
useEffect(() => { useEffect(() => {
fetch('https://api.vytal.io/ip/') fetchAPI(setData, setDisplay);
.then((response) => response.json())
.then((json) => {
setData(getLocation(json));
setDisplay(1);
})
.catch(() => {
setDisplay(0);
});
}, []); }, []);
return ( return (
@ -25,8 +16,8 @@ const LocationBlock = () => {
<h1>Location</h1> <h1>Location</h1>
{display === 1 && ( {display === 1 && (
<> <>
<img src={mapUrl} alt="Map of current location" /> <img src={getMap(data)} alt="Map of current location" />
<Table data={data} /> <Table data={getLocation(data)} />
</> </>
)} )}
{display === 0 && ( {display === 0 && (

View file

@ -3,7 +3,9 @@ import Bowser from 'bowser';
import axios from 'axios'; import axios from 'axios';
export { export {
fetchAPI,
getLocation, getLocation,
getMap,
getConnection, getConnection,
detectTor, detectTor,
getSoftware, getSoftware,
@ -16,6 +18,18 @@ export {
handleSave, handleSave,
}; };
const fetchAPI = (setData, setDisplay) => {
fetch('https://api.vytal.io/ip/')
.then((response) => response.json())
.then((json) => {
setData(json);
setDisplay(1);
})
.catch(() => {
setDisplay(0);
});
};
const getLocation = (json) => { const getLocation = (json) => {
const data = [ const data = [
{ {
@ -52,6 +66,9 @@ const getLocation = (json) => {
return data; return data;
}; };
const getMap = (data) =>
`https://maps.googleapis.com/maps/api/staticmap?center=${data.lat},${data.lon}&markers=color:red%7Clabel:%7C${data.lat},${data.lon}&size=500x200&zoom=10&key=AIzaSyB-YN-X8PGBSPd7NOaQu4csVhgJUnF3ZGk`;
const getConnection = (json) => { const getConnection = (json) => {
const data = [ const data = [
{ {