From 783eee3799b5b85ccbd6e13a0a87dcc5c53093b5 Mon Sep 17 00:00:00 2001 From: z0ccc Date: Thu, 26 Aug 2021 14:10:24 -0400 Subject: [PATCH] Created fetchAPI function --- frontend/.eslintrc.js | 1 + frontend/src/components/ConnectionBlock.js | 14 +++----------- frontend/src/components/LocationBlock.js | 17 ++++------------- frontend/src/components/main.js | 17 +++++++++++++++++ 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/frontend/.eslintrc.js b/frontend/.eslintrc.js index 7f94ace..d2bcd7e 100644 --- a/frontend/.eslintrc.js +++ b/frontend/.eslintrc.js @@ -25,6 +25,7 @@ module.exports = { 'one-var': 'off', 'one-var-declaration-per-line': 'off', 'object-curly-newline': 'off', + 'implicit-arrow-linebreak': 'off', 'import/extensions': [ 'error', 'ignorePackages', diff --git a/frontend/src/components/ConnectionBlock.js b/frontend/src/components/ConnectionBlock.js index 6618042..cf22cbd 100644 --- a/frontend/src/components/ConnectionBlock.js +++ b/frontend/src/components/ConnectionBlock.js @@ -1,28 +1,20 @@ import { useState, useEffect } from 'react'; import ScanBlock from './ScanBlock'; import Table from './Table'; -import { getConnection } from './main'; +import { fetchAPI, getConnection } from './main'; const ConnectionBlock = () => { const [data, setData] = useState(''); const [display, setDisplay] = useState(''); useEffect(() => { - fetch('https://api.vytal.io/ip/') - .then((response) => response.json()) - .then((json) => { - setData(getConnection(json)); - setDisplay(1); - }) - .catch(() => { - setDisplay(0); - }); + fetchAPI(setData, setDisplay); }, []); return (

Connection

- {display === 1 && } + {display === 1 &&
} {display === 0 && (
Unable to fetch info. Adblock or content filter may have prevented diff --git a/frontend/src/components/LocationBlock.js b/frontend/src/components/LocationBlock.js index 7bcaa68..5768911 100644 --- a/frontend/src/components/LocationBlock.js +++ b/frontend/src/components/LocationBlock.js @@ -1,23 +1,14 @@ import { useState, useEffect } from 'react'; import ScanBlock from './ScanBlock'; import Table from './Table'; -import { getLocation } from './main'; +import { fetchAPI, getLocation, getMap } from './main'; const LocationBlock = () => { const [data, setData] = 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(() => { - fetch('https://api.vytal.io/ip/') - .then((response) => response.json()) - .then((json) => { - setData(getLocation(json)); - setDisplay(1); - }) - .catch(() => { - setDisplay(0); - }); + fetchAPI(setData, setDisplay); }, []); return ( @@ -25,8 +16,8 @@ const LocationBlock = () => {

Location

{display === 1 && ( <> - Map of current location -
+ Map of current location +
)} {display === 0 && ( diff --git a/frontend/src/components/main.js b/frontend/src/components/main.js index 2aa7ce2..9505126 100644 --- a/frontend/src/components/main.js +++ b/frontend/src/components/main.js @@ -3,7 +3,9 @@ import Bowser from 'bowser'; import axios from 'axios'; export { + fetchAPI, getLocation, + getMap, getConnection, detectTor, getSoftware, @@ -16,6 +18,18 @@ export { 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 data = [ { @@ -52,6 +66,9 @@ const getLocation = (json) => { 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 data = [ {