-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmealList.js
More file actions
182 lines (156 loc) · 6.27 KB
/
mealList.js
File metadata and controls
182 lines (156 loc) · 6.27 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* eslint-disable no-await-in-loop */
import createLike from './createLike';
import getLikes from './getLikes';
import mealCounter from './mealCounter';
import commentCounter from './commentCounter';
const mainDiv = document.querySelector('#food-items');
const mealDetailsContent = document.querySelector('.meal-details-content');
const closeBtn = document.querySelector('.close-btn');
const likesCount = (target, likesArray, numOfLikes) => {
likesArray.forEach((obj) => {
if (obj.item_id === target.id) {
numOfLikes.innerHTML = `${obj.likes} likes `;
}
});
};
const mealList = async (data) => {
const mealCount = document.querySelector('#home-count');
mealCount.innerHTML = mealCounter(data);
for (let i = 0; i <= data.length - 1; i += 1) {
const foodDiv = document.createElement('div');
foodDiv.classList.add('col-lg-4');
const listItem = document.createElement('div');
listItem.id = data[i].idMeal;
foodDiv.setAttribute('data-id', `${data[i].idMeal}`);
listItem.classList.add('meal-item', 'text-center', 'card', 'shadow', 'mb-4', 'border-0');
const itemImage = document.createElement('div');
itemImage.classList.add('meal-img');
const image = document.createElement('img');
image.src = data[i].strMealThumb;
itemImage.appendChild(image);
listItem.appendChild(itemImage);
const itemText = document.createElement('div');
itemText.classList.add('meal-name', 'd-flex', 'pt-3', 'justify-content-center', 'align-items-center');
const mealName = document.createElement('h3');
mealName.classList.add('fs-6', 'me-2', 'pt-1');
mealName.innerHTML = data[i].strMeal;
const likeBtn = document.createElement('i');
likeBtn.classList.add('bi', 'bi-heart', 'like-btn', 'text-danger');
likeBtn.id = data[i].idMeal;
itemText.appendChild(mealName);
itemText.appendChild(likeBtn);
listItem.appendChild(itemText);
const mealLikes = document.createElement('div');
const numOfLikes = document.createElement('small');
numOfLikes.innerHTML = '0 likes';
mealLikes.appendChild(numOfLikes);
listItem.appendChild(mealLikes);
const commentContainer = document.createElement('div');
commentContainer.classList.add('comment', 'mb-4', 'mt-2');
const button = document.createElement('button');
button.classList.add('btn', 'btn-warning', 'rounded-pill', 'px-4', 'shadow', 'comment-btn');
button.setAttribute('id', `${data[i].idMeal}`);
button.setAttribute('type', 'button');
button.setAttribute('data-bs-toggle', 'modal');
button.setAttribute('data-bs-target', '#staticBackdrop');
button.innerHTML = 'comments';
commentContainer.appendChild(button);
listItem.appendChild(commentContainer);
foodDiv.appendChild(listItem);
mainDiv.appendChild(foodDiv);
const likesArray = await getLikes();
likesCount(likeBtn, likesArray, numOfLikes);
likeBtn.addEventListener('click', async (e) => {
await createLike(likeBtn.id);
const newLikes = await getLikes();
likesCount(e.target, newLikes, numOfLikes);
});
}
};
const postComment = async (data) => {
const url = 'https://us-central1-involvement-api.cloudfunctions.net/capstoneApi/apps/8WhiXHgGMaGrsfo6vYsR/comments';
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify(data),
});
return response.ok;
};
const getComment = async (item) => {
const url = `https://us-central1-involvement-api.cloudfunctions.net/capstoneApi/apps/8WhiXHgGMaGrsfo6vYsR/comments?item_id=${item.idMeal}`;
let myComment = await fetch(url).then((response) => response.json());
const ul = document.querySelector('#list-comment');
ul.innerHTML = '';
const h3 = document.querySelector('.comment-count');
h3.innerHTML = `Comments(${commentCounter(myComment)})`;
if (!myComment.length) myComment = [];
myComment.forEach((comment) => {
ul.innerHTML += `
<li class="d-flex justify-content-start align-items-center">
<p class="me-3">${comment.creation_date}</p>
<p class="me-3">${comment.username}</p>
<p>${comment.comment}</p>
</li>
`;
});
};
const mealModal = async (meal) => {
[meal] = meal;
mealDetailsContent.innerHTML = `
<h2 class = "recipe-title">${meal.strMeal}</h2>
<p class = "recipe-category">${meal.strCategory}</p>
<div class = "recipe-instruct">
<h3>Instructions:</h3>
<p>${meal.strInstructions}</p>
</div>
<div class = "recipe-meal-img">
<img src = "${meal.strMealThumb}" alt = "">
</div>
<h3 class="m-3 comment-count"></h3>
<div class="d-flex justify-content-center align-items-center">
<ul id="list-comment" class="list-unstyled">
</ul>
</div>
<h3 class="m-3">Add a comment</h3>
<form autocomplete="off" class="w-50 mx-auto">
<input type="text" class="form-control w-75 mx-auto mb-2" id="commentator" placeholder="Your name">
<textarea id="comment" name="comment" placeholder="Your comment..."></textarea>
<button type="button" class="btn btn-secondary commentBtn">Comment</button>
</form>
`;
mealDetailsContent.parentElement.classList.add('showComment');
const commentBtn = document.querySelector('.commentBtn');
commentBtn.addEventListener('click', () => {
const username = document.querySelector('#commentator').value;
const comment = document.querySelector('#comment').value;
const itemId = meal.idMeal;
const newData = {
item_id: itemId,
username,
comment,
};
postComment(newData);
document.querySelector('#commentator').value = '';
document.querySelector('#comment').value = '';
setTimeout(() => {
getComment(meal);
}, 1000);
});
getComment(meal);
};
const getMeal = async (e) => {
e.preventDefault();
if (e.target.classList.contains('comment-btn')) {
const mealItem = e.target.parentElement.parentElement.parentElement;
const url = `https://www.themealdb.com/api/json/v1/1/lookup.php?i=${mealItem.dataset.id}`;
const response = await fetch(url).then((response) => response.json()).then((data) => data);
mealModal(response.meals);
}
};
mainDiv.addEventListener('click', getMeal);
closeBtn.addEventListener('click', () => {
mealDetailsContent.parentElement.classList.remove('showComment');
});
export default mealList;