-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
34 lines (29 loc) · 1.06 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { checkAuth, createRating, deleteRating, fetchJokes, getUser, logInLogOut } from './fetch-utils.js';
import { renderJoke, renderRatingDiv } from './render-utils.js';
checkAuth();
const jokeSection = document.getElementById('joke-section');
const signButton = document.getElementById('sign-up');
logInLogOut(signButton);
async function renderJokes() {
const jokes = await fetchJokes();
jokeSection.textContent = '';
for (const joke of jokes) {
const jokeEl = renderJoke(joke);
if (joke.user_id !== getUser().id) {
const ratingEl = renderRatingDiv(joke);
jokeEl.append(ratingEl);
ratingEl.addEventListener('click', async () => {
if (joke.ratings && joke.ratings.length > 0) {
await deleteRating(joke.id);
} else {
await createRating(joke.id);
}
renderJokes();
});
}
jokeSection.append(jokeEl);
}
}
window.addEventListener('load', async () => {
await renderJokes();
});