|
1 | | -import { showData } from './api.js'; |
| 1 | +import { showData, getLikes } from './api'; |
| 2 | +// involvement API for POST |
| 3 | +const url = 'https://us-central1-involvement-api.cloudfunctions.net/capstoneApi/apps/OX5sTwXK5eU2vy1wOpri/likes'; |
2 | 4 |
|
3 | | -/* const shows = ['La Casa de Papel', '1899', 'Dark', |
4 | | -'Stranger Things', 'Squid Game', 'Ginny & Georgia']; */ |
5 | | -// const shows =[327417, 384429, 334824, 305288, 383275, 393625] //Thetvdb ids for each show |
| 5 | +// const shows = [27436, 39749, 17861, 2993, 43687, 47549]; // TVmaze ids |
| 6 | +const tvShows = ['La Casa de Papel', '1899', 'Dark', 'Stranger Things', 'Squid Game', 'Ginny & Georgia']; |
| 7 | + |
| 8 | +const show = []; |
| 9 | + |
| 10 | +const showsCount = async () => { |
| 11 | + const shows = document.querySelectorAll('.article').length; |
| 12 | + const addCount = document.querySelector('.count-shows'); |
| 13 | + addCount.innerHTML = `Top binge-worthy shows (${shows})`; |
| 14 | +}; |
| 15 | + |
| 16 | +export const showInfo = async () => { |
| 17 | + for (let i = 0; i < tvShows.length; i += 1) { |
| 18 | + /*eslint-disable */ |
| 19 | + |
| 20 | + //suppress all warnings between comments |
| 21 | + const data = await showData([tvShows[i]]); |
| 22 | + |
| 23 | + /* eslint-enable */ |
| 24 | + show.push(data); |
| 25 | + } |
| 26 | + return show; |
| 27 | +}; |
| 28 | + |
| 29 | +const currentLikes = async () => { |
| 30 | + const likes = await getLikes(); |
| 31 | + const show = await showInfo(); |
| 32 | + for (let i = 0; i < likes.length; i += 1) { |
| 33 | + for (let x = 0; x < show.length; x += 1) { |
| 34 | + if (Number(likes[i].item_id) === show[x].id) { |
| 35 | + show[x].likes = likes[i].likes; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return show; |
| 40 | +}; |
| 41 | + |
| 42 | +const addLike = async (likeBtn) => { |
| 43 | + await fetch(url, { |
| 44 | + method: 'POST', |
| 45 | + body: JSON.stringify({ item_id: likeBtn.id }), |
| 46 | + headers: { |
| 47 | + 'Content-Type': 'application/json', |
| 48 | + }, |
| 49 | + }); |
| 50 | + const counter = document.getElementById(`${likeBtn.id}counter`); |
| 51 | + const likes = counter.innerHTML.split(' '); |
| 52 | + likes[0] = Number(likes[0]) + 1; |
| 53 | + counter.innerHTML = likes.join(' '); |
| 54 | +}; |
| 55 | + |
| 56 | +const likeBtn = () => { |
| 57 | + const btnLike = document.querySelectorAll('.like'); |
| 58 | + btnLike.forEach((btn) => { |
| 59 | + btn.addEventListener('click', () => { |
| 60 | + addLike(btn); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}; |
6 | 64 |
|
7 | | -const shows = [27436, 39749, 17861, 2993, 43687, 47549]; // TVmaze ids |
8 | 65 | const displayShows = async () => { |
| 66 | + const show = await currentLikes(); |
9 | 67 | const container = document.querySelector('.container'); |
10 | | - shows.forEach(async (e) => { |
11 | | - const show = await showData(e); |
| 68 | + container.innerHTML = ''; |
| 69 | + for (let i = 0; i < show.length; i += 1) { |
12 | 70 | container.innerHTML += ` |
13 | | - <article id='${show.id}'> |
14 | | - <img src=${show.image.medium}> |
15 | | - <div class="title"> |
16 | | - <h2>${show.name}</h2> |
17 | | - <i class="fa-sharp fa-regular fa-heart"></i> |
| 71 | + <article class="article"> |
| 72 | + <img src=${show[i].image.medium}> |
| 73 | + <div class="title" id="${show[i].id}"> |
| 74 | + <h2>${show[i].name}</h2> |
| 75 | + <div class="likes"> |
| 76 | + <i class="fa-sharp fa-regular fa-heart like" id="${show[i].id}"></i> |
| 77 | + <p id="${show[i].id}counter">${show[i].likes || 0} likes</p> |
| 78 | + </div> |
18 | 79 | </div> |
19 | 80 | <button class="comment">Comments</button> |
20 | 81 | </article> |
21 | 82 | `; |
22 | | - }); |
| 83 | + } |
| 84 | + likeBtn(); |
| 85 | + showsCount(); |
23 | 86 | }; |
24 | 87 |
|
25 | | -displayShows(); |
| 88 | +export default displayShows; |
0 commit comments