Skip to content

Commit fa83728

Browse files
authored
Merge pull request #2 from braincee/api-url
Leaderboard API Usage
2 parents 5d42f2a + 2168b7e commit fa83728

File tree

10 files changed

+122
-67
lines changed

10 files changed

+122
-67
lines changed

MIT.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Copyright 2021, [STEPHEN ANNOR]
2+
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this [LEADERBOARD PROJECT] and associated documentation files, to deal in the [LEADERBOARD PROJECT] without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the [LEADERBOARD PROJECT], and to permit persons to whom the [LEADERBOARD PROJECT] is furnished to do so, subject to the following conditions:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the [LEADERBOARD PROJECT].
7+
8+
THE [LEADERBOARD PROJECT] IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE [LEADERBOARD PROJECT] OR THE USE OR OTHER DEALINGS IN THE [LEADERBOARD PROJECT].

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ To get a local copy up and running follow these simple example steps.
3333
```
3434

3535

36-
## Screenshots of Todo List
36+
## Screenshots of Leaderboard
3737

3838
![]()
3939

dist/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ <h1 class="header">Leaderboard</h1>
1212
<main>
1313
<section class="board-1">
1414
<div class="recent-goals">
15-
<h2>Recent Goals</h2>
16-
<button type="button">Refresh</button>
15+
<h2>Recent Scores</h2>
16+
<button type="button" class="refresh">Refresh</button>
1717
</div>
1818

1919
<ul class="table">
@@ -22,11 +22,11 @@ <h2>Recent Goals</h2>
2222
</section>
2323
<section class="board-2">
2424
<div class="table">
25-
<h2>Add the goals</h2>
25+
<h2>Add Persons Score</h2>
2626
</div>
2727
<form action="#" class="name-goal">
28-
<input type="text" name="name" placeholder="Player name">
29-
<input type="number" name="goals" placeholder="Player goals">
28+
<input type="text" name="name" placeholder="Person's name">
29+
<input type="number" name="score" placeholder="Person's score">
3030
<button type="submit" class="submit">Submit</button>
3131
</form>
3232
</section>

dist/main.js

Lines changed: 19 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ <h1 class="header">Leaderboard</h1>
1111
</header>
1212
<main>
1313
<section class="board-1">
14-
<div class="recent-goals">
15-
<h2>Recent Goals</h2>
16-
<button type="button">Refresh</button>
14+
<div class="recent-score">
15+
<h2>Recent Scores</h2>
16+
<button type="button" class="refresh">Refresh</button>
1717
</div>
1818

1919
<ul class="table">
@@ -22,11 +22,11 @@ <h2>Recent Goals</h2>
2222
</section>
2323
<section class="board-2">
2424
<div class="table">
25-
<h2>Add the goals</h2>
25+
<h2>Add Persons Score</h2>
2626
</div>
27-
<form action="#" class="name-goal">
28-
<input type="text" name="name" placeholder="Player name">
29-
<input type="number" name="goals" placeholder="Player goals">
27+
<form action="#" class="name-score">
28+
<input type="text" name="name" placeholder="Person's name">
29+
<input type="number" name="score" placeholder="Person's score">
3030
<button type="submit" class="submit">Submit</button>
3131
</form>
3232
</section>

src/index.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
import './style.css';
2-
import playerGoals from './modules/goals.js';
32

4-
document.addEventListener('DOMContentLoaded', playerGoals);
3+
import { newScore } from './modules/api_url.js';
4+
import outcomeScores from './modules/scores.js';
5+
6+
const form = document.querySelector('.name-score');
7+
8+
const newScoreApi = () => {
9+
const name = form.elements.name.value;
10+
const score = form.elements.score.value;
11+
if (score !== '' && name !== '') {
12+
newScore(name, score);
13+
}
14+
};
15+
16+
document.addEventListener('click', (e) => {
17+
if (e.target.className === 'submit') {
18+
e.preventDefault();
19+
newScoreApi();
20+
} else if (e.target.className === 'refresh') {
21+
const persons = document.querySelector('.board-1 .table');
22+
while (persons.firstChild) {
23+
persons.removeChild(persons.firstChild);
24+
}
25+
outcomeScores();
26+
}
27+
});
28+
29+
document.addEventListener('DOMContentLoaded', () => { outcomeScores(); });

src/modules/api_url.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const apiUrl = 'https://us-central1-js-capstone-backend.cloudfunctions.net/api';
2+
3+
export const newGame = async () => {
4+
const response = { name: 'My new Game' };
5+
const result = await fetch(`${apiUrl}/games/`, {
6+
method: 'POST',
7+
headers: {
8+
'Content-Type': 'application/json',
9+
},
10+
body: JSON.stringify(response),
11+
});
12+
await result.json();
13+
};
14+
15+
let apiUrlScores;
16+
17+
export const urlScores = async () => {
18+
const result = await fetch(`${apiUrl}/games/uFmmrkKjMO7S5RsdkjfE/scores`);
19+
apiUrlScores = await result.json();
20+
return apiUrlScores;
21+
};
22+
23+
export const newScore = async (user, score) => {
24+
const response = { user, score };
25+
const result = await fetch(`${apiUrl}/games/uFmmrkKjMO7S5RsdkjfE/scores`, {
26+
method: 'POST',
27+
headers: {
28+
'Content-Type': 'application/json',
29+
},
30+
body: JSON.stringify(response),
31+
});
32+
return result.json();
33+
};

src/modules/goals.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/modules/scores.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { urlScores } from './api_url.js';
2+
3+
const persons = document.querySelector('.board-1 .table');
4+
5+
const outcomeScores = async () => {
6+
const newScore = await urlScores();
7+
newScore.result.forEach((score) => {
8+
const person = document.createElement('li');
9+
const span = document.createElement('span');
10+
span.textContent = score.score;
11+
person.className = 'table-records';
12+
person.textContent = `${score.user}:`;
13+
person.appendChild(span);
14+
persons.appendChild(person);
15+
});
16+
};
17+
18+
export default outcomeScores;

src/style.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ main {
2020
gap: 2rem;
2121
}
2222

23-
.board-1 .recent-goals {
23+
.board-1 .recent-score {
2424
display: flex;
2525
gap: 2rem;
2626
padding: 2rem;
2727
}
2828

29-
.board-1 .recent-goals h2,
30-
.board-2 .recent-goals h2 {
29+
.board-1 .recent-score h2,
30+
.board-2 .recent-score h2 {
3131
padding: 0;
3232
margin: 0;
3333
}
@@ -42,7 +42,7 @@ main {
4242
box-shadow: 1px 1px rgb(102, 99, 99);
4343
}
4444

45-
.board-1 .recent-goals button {
45+
.board-1 .recent-score button {
4646
width: 6rem;
4747
height: 2rem;
4848
padding: 5px;

0 commit comments

Comments
 (0)