Skip to content

Commit

Permalink
Merge pull request #4 from RaviTejaM9602/Milestone2
Browse files Browse the repository at this point in the history
Hitting API Populate on Leader Board
  • Loading branch information
RaviTejaM9602 committed Feb 10, 2022
2 parents 45ade94 + f907f25 commit af5b1c0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 27 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ In the following link, you can view (Coming Soon)).

## Screenshot

In the following link, you can view (Coming Soon)).
![LeaderBoard_App](./images/LeaderBoard.png)
![LeaderBoard_App](./images/LeaderBoard2.png)




## Usage

Expand Down
16 changes: 13 additions & 3 deletions dist/index.bundle.js

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

6 changes: 4 additions & 2 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ <h1 id="header">LeaderBoard</h1>
<h2>Recent scores</h2>
<button type="button" id="refresh">Refresh</button>
</div>
<table id="items"></table>
<ul id="items">

</ul>
</div>
<form id="add-item">
<h2>Add your score</h2>
Expand All @@ -35,6 +37,6 @@ <h2>Add your score</h2>
<button type="submit">Submit</button>
</form>
</div>
<script defer="defer" src="main.js"></script>
<script defer="defer" src="index.js"></script>
</body>
</html>
Binary file added images/LeaderBoard.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/LeaderBoard2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 9 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import './style.css';
import { leaderboard, refreshLeaderboard } from './modules/refresh.js';

const leaderboard = document.querySelector('#items');
const FORM = document.querySelector('form');
const NAME = FORM.querySelector('input');
const SCORE = FORM.querySelector('input[type="number"]');
const REFRESH = document.querySelector('#refresh');

const refreshInput = () => {
NAME.value = '';
SCORE.value = '';
};

(() => {
FORM.addEventListener('submit', (e) => {
e.preventDefault();
fetch(
'https://us-central1-js-capstone-backend.cloudfunctions.net/api/games/oHFx2AoOEAVDqu2U4dC1/scores',
'https://us-central1-js-capstone-backend.cloudfunctions.net/api/games/BxXNm40rTdIjhpN9YGnO/scores',
{
method: 'POST',
headers: {
Expand All @@ -21,29 +26,12 @@ const REFRESH = document.querySelector('#refresh');
score: SCORE.value,
}),
},
refreshInput(),
);
});
})();

const refreshLeaderboard = async () => {
const response = await fetch(
'https://us-central1-js-capstone-backend.cloudfunctions.net/api/games/oHFx2AoOEAVDqu2U4dC1/scores',
);
const scoreText = await response.text();
const score = JSON.parse(scoreText);
score.result.forEach((player) => {
if (player.length === 0) {
leaderboard.style.border = 'none';
} else {
leaderboard.style.border = '2px solid rgb(36, 1, 1)';
leaderboard.innerHTML += `<li>
${player.user}: ${player.score}</td>
</li>`;
}
});
};

REFRESH.adudEventListener('click', () => {
REFRESH.addEventListener('click', () => {
leaderboard.innerHTML = '';
refreshLeaderboard();
});
19 changes: 19 additions & 0 deletions src/modules/refresh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const leaderboard = document.querySelector('#items');

export const refreshLeaderboard = async () => {
const response = await fetch(
'https://us-central1-js-capstone-backend.cloudfunctions.net/api/games/BxXNm40rTdIjhpN9YGnO/scores',
);
const scoreText = await response.text();
const score = JSON.parse(scoreText);
score.result.forEach((player) => {
if (player.length === 0) {
leaderboard.style.border = 'none';
} else {
leaderboard.style.border = '2px solid rgb(36, 1, 1)';
leaderboard.innerHTML += `<li>
${player.user} : ${player.score}</td>
</li>`;
}
});
};

0 comments on commit af5b1c0

Please sign in to comment.