Skip to content

Commit

Permalink
Merge pull request #2 from Iltwats/add-counter
Browse files Browse the repository at this point in the history
Add counter for each link usage
  • Loading branch information
Iltwats authored Jul 10, 2024
2 parents f5b3f25 + 4af93b6 commit 1fe15ba
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 96 deletions.
72 changes: 36 additions & 36 deletions index.html
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>
49 changes: 28 additions & 21 deletions public/helperFunctions.js
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}`
}
}
77 changes: 38 additions & 39 deletions public/main.js
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);

0 comments on commit 1fe15ba

Please sign in to comment.