-
Notifications
You must be signed in to change notification settings - Fork 0
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 LarryIVC/hit-Api
Leaderborad - Hit Api
- Loading branch information
Showing
14 changed files
with
133 additions
and
359 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 +1 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<script defer src="/runtime.bundle.js"></script><script defer src="/index.bundle.js"></script></head> | ||
<body> | ||
<header class="container-width"> | ||
<h1>Leaderboard</h1> | ||
</header> | ||
<main class="container-width"> | ||
<article class="left"> | ||
<div class="header-left"> | ||
<h2>Recent scores</h2> | ||
<button>Refresh</button> | ||
</div> | ||
<div class="scores-list"> | ||
<ul> | ||
<li><span>Name: </span><span>100</span></li> | ||
<li><span>Name: </span><span>50</span></li> | ||
<li><span>Name: </span><span>10</span></li> | ||
<li><span>Name: </span><span>30</span></li> | ||
<li><span>Name: </span><span>100</span></li> | ||
<li><span>Name: </span><span>80</span></li> | ||
<li><span>Name: </span><span>60</span></li> | ||
</ul> | ||
</div> | ||
</article> | ||
<article class="right"> | ||
<h2>Add your score</h2> | ||
<form class="frm-add-score"> | ||
<label for="name"><input type="text" id="name" name="name" required placeholder="Your name"></label> | ||
<label for="score"><input type="text" id="score" name="score" required placeholder="Your score"></label> | ||
<div class="btn-cnt"> | ||
<button type="submit">Submit</button> | ||
</div> | ||
</form> | ||
</article> | ||
</main> | ||
</body> | ||
</html> | ||
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Leaderboard Scores - consuming Api</title><script defer="defer" src="/runtime.bundle.js"></script><script defer="defer" src="/index.bundle.js"></script></head><body><header class="container-width"><h1>Leaderboard</h1></header><main class="container-width"><article class="left"><div class="header-left"><h2>Recent scores</h2><button id="btn-refresh">Refresh</button></div><div class="scores-list"><ul id="ul-scores"></ul></div></article><article class="right"><h2>Add your score</h2><form class="frm-add-score" id="frm-add-score"><label for="name"><input id="name" name="name" required placeholder="Your name"></label> <label for="score"><input id="score" name="score" required placeholder="Your score"></label><div class="btn-cnt"><button type="submit">Submit</button></div></form></article></main></body></html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,2 +1,6 @@ | ||
// import _ from 'lodash'; | ||
import './index.css'; | ||
import App from './modules/App.js'; | ||
|
||
window.onload = () => { | ||
App(); | ||
}; |
File renamed without changes
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { API_URL, KEY } from './Vars.js'; | ||
|
||
// function to GET scores | ||
export const getGame = async () => { | ||
try { | ||
const response = await fetch(`${API_URL}games/${KEY}/scores/`); | ||
const json = await response.json(); | ||
return json; | ||
} catch (error) { | ||
return error; | ||
} | ||
}; | ||
|
||
// function to POST new score | ||
export const createScore = async (user, score) => { | ||
try { | ||
const response = await fetch(`${API_URL}games/${KEY}/scores/`, { | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
user, | ||
score, | ||
}), | ||
headers: { | ||
'Content-type': 'application/json; charset=UTF-8', | ||
}, | ||
}); | ||
const json = await response.json(); | ||
return json; | ||
} catch (error) { | ||
return error; | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { getGame, createScore } from './Api.js'; | ||
|
||
// Display Scores | ||
const renderList = async () => { | ||
const doc = document; | ||
const ul = doc.getElementById('ul-scores'); | ||
ul.innerHTML = ''; | ||
|
||
const game = await getGame(); | ||
const { result } = game; | ||
|
||
result.forEach((item) => { | ||
const li = doc.createElement('li'); | ||
const spanName = doc.createElement('span'); | ||
const spanScore = doc.createElement('span'); | ||
const { user, score } = item; | ||
spanName.textContent = `${user}:`; | ||
spanScore.textContent = `${score}`; | ||
li.appendChild(spanName); | ||
li.appendChild(spanScore); | ||
ul.appendChild(li); | ||
}); | ||
}; | ||
|
||
// Add new score | ||
const addScores = () => { | ||
const frmScores = document.getElementById('frm-add-score'); | ||
frmScores.addEventListener('submit', async (e) => { | ||
e.preventDefault(); | ||
const output = await createScore(frmScores.name.value, frmScores.score.value); | ||
renderList(); | ||
frmScores.name.value = ''; | ||
frmScores.score.value = ''; | ||
return output; | ||
}); | ||
}; | ||
|
||
// Refresh button | ||
const refresh = () => { | ||
const btnRefresh = document.getElementById('btn-refresh'); | ||
btnRefresh.addEventListener('click', () => { | ||
renderList(); | ||
}); | ||
}; | ||
|
||
// Principal function | ||
const App = () => { | ||
addScores(); | ||
renderList(); | ||
refresh(); | ||
}; | ||
|
||
export default App; |
Oops, something went wrong.