-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Iltwats/add-counter
Add counter for each link usage
- Loading branch information
Showing
3 changed files
with
102 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Byte Size</title> | ||
<link rel="stylesheet" type="text/css" href="public/style.css" /> | ||
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> | ||
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro" rel="stylesheet"> | ||
</head> | ||
|
||
<body> | ||
<header> | ||
<img src="https://raw.githubusercontent.com/Iltwats/CDN/master/bytesize_logo.svg" class="logo" /> | ||
|
||
</header> | ||
<main id="main"> | ||
<div class="container"> | ||
|
||
<h1>Enter a URL</h1> | ||
|
||
<form id="form" autocomplete="off"> | ||
<input type="text" id="input" value=""> | ||
<button id="shorten">Shorten</button> | ||
</form> | ||
|
||
<div id="responseField"> | ||
|
||
</div> | ||
|
||
</div> | ||
</main> | ||
<script src='https://code.jquery.com/jquery-3.1.0.min.js'></script> | ||
<script src="public/main.js"></script> | ||
<script src="public/helperFunctions.js"></script> | ||
</body> | ||
</html> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Byte Size</title> | ||
<link rel="stylesheet" type="text/css" href="public/style.css" /> | ||
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> | ||
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro" rel="stylesheet"> | ||
</head> | ||
|
||
<body> | ||
<header> | ||
<img src="https://raw.githubusercontent.com/Iltwats/CDN/master/bytesize_logo.svg" class="logo" /> | ||
|
||
</header> | ||
<main id="main"> | ||
<div class="container"> | ||
|
||
<h1>Enter a URL</h1> | ||
|
||
<form id="form" autocomplete="off"> | ||
<input type="text" id="input" value=""> | ||
<button id="shorten">Shorten</button> | ||
</form> | ||
|
||
<div id="responseField"> | ||
<div id="counter"></div> | ||
</div> | ||
|
||
</div> | ||
</main> | ||
<script src='https://code.jquery.com/jquery-3.1.0.min.js'></script> | ||
<script src="public/main.js"></script> | ||
<script src="public/helperFunctions.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
// manipulate responseField to render a formatted and appropriate message | ||
const renderResponse = (res) => { | ||
if(res.errors){ | ||
responseField.innerHTML = "<p>Sorry, couldn't format your URL.</p><p>Try again.</p>" | ||
} else { | ||
responseField.innerHTML = `<p>Your shortened url is: </p><p> ${res.shortUrl} </p>` | ||
} | ||
} | ||
|
||
// manipulate responseField to render an unformatted response | ||
const renderRawResponse = (res) => { | ||
if(res.errors){ | ||
responseField.innerHTML = "<p>Sorry, couldn't format your URL.</p><p>Try again.</p>" | ||
} else { | ||
let structuredRes = JSON.stringify(res).replace(/,/g, ", \n") | ||
structuredRes = `<pre>${structuredRes}</pre>` | ||
|
||
responseField.innerHTML = `${structuredRes}` | ||
} | ||
} | ||
|
||
const updateCounter = () => { | ||
let counterElement = document.getElementById('counter'); | ||
let count = parseInt(counterElement.innerText) || 0; | ||
counterElement.innerText = count + 1; | ||
} | ||
|
||
// manipulate responseField to render a formatted and appropriate message | ||
const renderResponse = (res) => { | ||
updateCounter(); | ||
if(res.errors){ | ||
responseField.innerHTML = "<p>Sorry, couldn't format your URL.</p><p>Try again.</p>" | ||
} else { | ||
responseField.innerHTML = `<p>Your shortened url is: </p><p> ${res.shortUrl} </p>` | ||
} | ||
} | ||
|
||
// manipulate responseField to render an unformatted response | ||
const renderRawResponse = (res) => { | ||
updateCounter(); | ||
if(res.errors){ | ||
responseField.innerHTML = "<p>Sorry, couldn't format your URL.</p><p>Try again.</p>" | ||
} else { | ||
let structuredRes = JSON.stringify(res).replace(/,/g, ", \n") | ||
structuredRes = `<pre>${structuredRes}</pre>` | ||
|
||
responseField.innerHTML = `${structuredRes}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,38 @@ | ||
// Information to reach API | ||
const apiKey = 'f44beb51cb0b414ca4b1f203be60ff03'; | ||
const url = 'https://api.rebrandly.com/v1/links'; | ||
|
||
// Some page elements | ||
const inputField = document.querySelector('#input'); | ||
const shortenButton = document.querySelector('#shorten'); | ||
const responseField = document.querySelector('#responseField'); | ||
|
||
// AJAX functions | ||
const shortenUrl = () => { | ||
const urlToShorten = inputField.value; | ||
const data = JSON.stringify({destination: urlToShorten}); | ||
|
||
const xhr = new XMLHttpRequest(); | ||
xhr.responseType = 'json'; | ||
|
||
xhr.onreadystatechange = () => { | ||
if (xhr.readyState === XMLHttpRequest.DONE) { | ||
renderResponse(xhr.response); | ||
} | ||
} | ||
xhr.open('POST', url); | ||
xhr.setRequestHeader('Content-type', 'application/json'); | ||
xhr.setRequestHeader('apikey', apiKey); | ||
xhr.send(data); | ||
} | ||
|
||
|
||
// Clear page and call AJAX functions | ||
const displayShortUrl = (event) => { | ||
event.preventDefault(); | ||
while(responseField.firstChild){ | ||
responseField.removeChild(responseField.firstChild); | ||
} | ||
shortenUrl(); | ||
} | ||
|
||
shortenButton.addEventListener('click', displayShortUrl); | ||
const apiKey = 'f44beb51cb0b414ca4b1f203be60ff03'; | ||
const url = 'https://api.rebrandly.com/v1/links'; | ||
|
||
const inputField = document.querySelector('#input'); | ||
const shortenButton = document.querySelector('#shorten'); | ||
const responseField = document.querySelector('#responseField'); | ||
|
||
let counter = 0; | ||
|
||
const shortenUrl = () => { | ||
const urlToShorten = inputField.value; | ||
const data = JSON.stringify({destination: urlToShorten}); | ||
|
||
const xhr = new XMLHttpRequest(); | ||
xhr.responseType = 'json'; | ||
|
||
xhr.onreadystatechange = () => { | ||
if (xhr.readyState === XMLHttpRequest.DONE) { | ||
renderResponse(xhr.response); | ||
counter++; | ||
document.getElementById('counter').innerText = `Link usage count: ${counter}`; | ||
} | ||
} | ||
xhr.open('POST', url); | ||
xhr.setRequestHeader('Content-type', 'application/json'); | ||
xhr.setRequestHeader('apikey', apiKey); | ||
xhr.send(data); | ||
} | ||
|
||
const displayShortUrl = (event) => { | ||
event.preventDefault(); | ||
while(responseField.firstChild){ | ||
responseField.removeChild(responseField.firstChild); | ||
} | ||
shortenUrl(); | ||
} | ||
|
||
shortenButton.addEventListener('click', displayShortUrl); |