From 610820524174decb9bf8ff6996880a224d0d7a31 Mon Sep 17 00:00:00 2001 From: Ari Zilnik Date: Wed, 26 Sep 2018 15:54:35 -0400 Subject: [PATCH 1/3] Added ability to localize based on user location --- docs/index.html | 8 +++++--- docs/location.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 docs/location.js diff --git a/docs/index.html b/docs/index.html index e8f4b84..55d0653 100644 --- a/docs/index.html +++ b/docs/index.html @@ -38,7 +38,7 @@

the planet.

- We have open positions for technical product designers & design leaders in San Francisco, Seattle, and elsewhere. + We have open positions for technical product designers & design leaders in San Francisco, Seattle, and elsewhere.

We use PCs, Macs, Figma, Sketch, GitHub, JavaScript, ZEIT, and other modern tools to design, prototype, and build the future @@ -116,12 +116,14 @@

document.documentElement.dataset.loaded = true }, 1000) } - addEventListener('load', markDocumentAsLoaded); + + + - \ No newline at end of file + diff --git a/docs/location.js b/docs/location.js new file mode 100644 index 0000000..a6d97ab --- /dev/null +++ b/docs/location.js @@ -0,0 +1,33 @@ +// Object to store location information based on user's IP +// elsewhere is the default +let appData = { + city: "", + country: "", + countryname: "elsewhere" +}; + +// Gets user location, sets the appData object, and renders the user's city onto the page +function getLocation() { + const api = + "http://api.ipstack.com/24.52.253.12?access_key=8fa1dab1f671740a6e295f8674fb1ca6"; + fetch(api) + .then(dataWrappedByPromise => dataWrappedByPromise.json()) + .then(APIdata => { + appData.country = APIdata.country_code; // Set the country + appData.countryname = APIdata.country_name; // Set the country name + appData.city = APIdata.city; // Set the city + }) + .then(detectLocation => { + changeLocation(); + }); +} + +// Gets the user's location from the appData object and renders it on-screen +function changeLocation() { + // Check to see that city is not empty, otherwise render user's city on-screen + let local = document.getElementById("location"); + appData.city == "" ? "" : (local.innerHTML = appData.city); +} + +// Run the getLocation function +getLocation(); From b0592a9850cb6b431b10411b87abdaff6bea8996 Mon Sep 17 00:00:00 2001 From: Ari Zilnik Date: Tue, 9 Oct 2018 17:32:36 -0400 Subject: [PATCH 2/3] Removed API key --- docs/location.js | 9 +++++++-- example.env | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/location.js b/docs/location.js index a6d97ab..66e15ee 100644 --- a/docs/location.js +++ b/docs/location.js @@ -1,3 +1,7 @@ +var request = require("request"); + +request(example.env.LOCATION_KEY); + // Object to store location information based on user's IP // elsewhere is the default let appData = { @@ -8,8 +12,9 @@ let appData = { // Gets user location, sets the appData object, and renders the user's city onto the page function getLocation() { - const api = - "http://api.ipstack.com/24.52.253.12?access_key=8fa1dab1f671740a6e295f8674fb1ca6"; + const key = YOUR_ACCESS_KEY; // @Microsoft, this line needs to be replaced with your API key for IPStack. + const api = `http://api.ipstack.com/24.52.253.12?access_key=${key}`; + fetch(api) .then(dataWrappedByPromise => dataWrappedByPromise.json()) .then(APIdata => { diff --git a/example.env b/example.env index 369fd57..7b0ca5e 100644 --- a/example.env +++ b/example.env @@ -1,2 +1,3 @@ # Github access token https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ -ACCESS_TOKEN=*************************** \ No newline at end of file +ACCESS_TOKEN=*************************** +LOCATION_KEY=8fa1dab1f671740a6e295f8674fb1ca6 From 1924dc7492e3c211c17c722c79a44f339a817f05 Mon Sep 17 00:00:00 2001 From: Ari Zilnik Date: Tue, 9 Oct 2018 17:37:33 -0400 Subject: [PATCH 3/3] code cleanup --- docs/location.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/docs/location.js b/docs/location.js index 66e15ee..d7f8505 100644 --- a/docs/location.js +++ b/docs/location.js @@ -1,7 +1,3 @@ -var request = require("request"); - -request(example.env.LOCATION_KEY); - // Object to store location information based on user's IP // elsewhere is the default let appData = {