Skip to content

Commit

Permalink
Merge pull request #2 from LarryIVC/hit-Api
Browse files Browse the repository at this point in the history
Leaderborad - Hit Api
  • Loading branch information
LarryIVC authored Apr 21, 2023
2 parents 95808f7 + 6acc3dd commit bf95011
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 359 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<a name="readme-top"></a>

<div align="center" style="text-align: center">
<img src = "logo.png">
<img src = "src/logo.png">
<h1><b>Leaderboard Scores</b></h1>
</div>

Expand Down Expand Up @@ -56,9 +56,13 @@
**Enlist skill sets and tech stack**

- Add template for Leaderboard
- Setup Webpack
- Apply GitFlow
- Setup `Webpack`
- Apply `GitFlow`
- Verified Linters
- Read and write to an API
- Implement `Form` for add Scores
- Use Arrow functions
- Using `async` and `await` `Javascript` features.

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down
128 changes: 1 addition & 127 deletions dist/index.bundle.js

Large diffs are not rendered by default.

43 changes: 1 addition & 42 deletions dist/index.html
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>
176 changes: 1 addition & 175 deletions dist/runtime.bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Project that consume api Leaderboard service",
"main": "./src/index.js",
"private": true,
"type":"module",
"scripts": {
"build": "webpack --mode=production",
"dev": "webpack --mode=development",
Expand Down
2 changes: 1 addition & 1 deletion src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ main {
padding: 10px;
}

.scores-list ul {
#ul-scores {
display: flex;
flex-direction: column;
gap: 5px;
Expand Down
15 changes: 5 additions & 10 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Leaderboard Scores - consuming Api</title>
</head>
<body>
<header class="container-width">
Expand All @@ -13,23 +14,17 @@ <h1>Leaderboard</h1>
<article class="left">
<div class="header-left">
<h2>Recent scores</h2>
<button>Refresh</button>
<button id="btn-refresh">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 id="ul-scores">
<!-- Render information from Api -->
</ul>
</div>
</article>
<article class="right">
<h2>Add your score</h2>
<form class="frm-add-score">
<form class="frm-add-score" id="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">
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
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
32 changes: 32 additions & 0 deletions src/modules/Api.js
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;
}
};
53 changes: 53 additions & 0 deletions src/modules/App.js
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;
Loading

0 comments on commit bf95011

Please sign in to comment.