Skip to content

Commit

Permalink
Merge pull request #34 from ZuhRa-HashiMi/comments-layout-functions
Browse files Browse the repository at this point in the history
Comments layout functions
  • Loading branch information
ZuhRa-HashiMi committed Dec 3, 2022
2 parents 6236861 + 86132b0 commit d8c9925
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"eol-last": "off",
"import/extensions": [ 1, {
"js": "always", "json": "always"
}]
}],
"import/no-cycle": 0
},
"ignorePatterns": [
"dist/",
Expand Down
3 changes: 2 additions & 1 deletion src/apis/food.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable import/no-cycle */
import paginate from '../index.js';

let meals = [];
/* eslint-disable-next-line */
export let meals = [];
let currentPosts = [];
let currentPage = 1;
const postsPerPage = 10;
Expand Down
Empty file removed src/apis/index.js
Empty file.
34 changes: 34 additions & 0 deletions src/apis/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable consistent-return */
export const postComment = async (post) => {
try {
await fetch(
'https://us-central1-involvement-api.cloudfunctions.net/capstoneApi/apps/XkZb08sfqWimSB3Sqtb3/comments',
{
method: 'POST',
body: JSON.stringify(post),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
},
);
} catch (err) {
// console.error(err);
}
};

export const getComment = async (id) => {
try {
const response = await fetch(
`https://us-central1-involvement-api.cloudfunctions.net/capstoneApi/apps/ofEMqPseJBxt6GrJYVpl/comments?item_id=${id}`,
{
method: 'GET',
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
},
);
return await response.json();
} catch (err) {
// console.log(err)
}
};
2 changes: 1 addition & 1 deletion src/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ Ensure the default browser behavior of the `hidden` attribute.
}

.md\:max-w-full {
max-width: 100%;
max-width: 60%;
}

.md\:flex-row {
Expand Down
Empty file removed src/compnents.js
Empty file.
4 changes: 4 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@

<div class="meal-details-content"></div>
</div>

<div class="comments-modal"></div>

</main>

<div
Expand Down Expand Up @@ -200,6 +203,7 @@ <h2 class="text-sm font-bold text-stone-900">Contact</h2>
</div>
</footer>
</div>

</body>
</html>

Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import './base.css';
import './style.css';
import logo from './yummly.svg';
import { getLike } from './apis/likes.js';

/* eslint-disable import/no-cycle */
import {
getCurrentPosts,
Expand All @@ -11,9 +12,9 @@ import {
handlePrevBtn,
handlePageBtn,
} from './apis/food.js';
import template from './template.js';
import defaultTemplate from './defaultTemplate.js';
import modalPopUp from './modal/index.js';
import template from './modules/template.js';
import defaultTemplate from './modules/defaultTemplate.js';
import modalPopUp from './modules/comments.js';
// get img tag and set src attribute
const myLogo = document.querySelector('.logoYummy');
myLogo.setAttribute('src', logo);
Expand Down
Empty file removed src/modal/index.js
Empty file.
86 changes: 86 additions & 0 deletions src/modules/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { postComment } from '../apis/requests.js';

export default async (modal, meal) => {
modal.innerHTML = `
<div class="recipe-details-image w-full mobile-div relative">
<img
alt=${meal.strMeal}
src=${meal.strMealThumb}
class="recipe-image w-11/12 mx-auto md:max-w-full rounded-lg md:rounded-lg mobile"
/>
</div>
</div>
<h2 class="comment-count-header">Commment (0) </h2>
<ul class="comment-display">
</ul>
<h2 class="comment-header">Add Comment</h2>
<form>
<input type="text" class="username" placeholder = "Your name" required/>
<textarea placeholder="Your comment" class="comment" required></textarea>
<input type="submit" class="submitBtn" value="Comment" />
</form>
</div>
</div>`;

modal.parentElement.classList.add('showRecipe');
const btn = document.querySelector('.submitBtn');
const username = document.querySelector('.username');
const comment = document.querySelector('.comment');
const commentCount = document.querySelector('.comment-count-header');
const ul = document.querySelector('.comment-display');
btn.addEventListener('click', async (e) => {
e.preventDefault();

if (username.value.trim() === '') return;
if (comment.value.trim() === '') return;

const userVal = username.value;
const commentVal = comment.value;
const id = meal.idMeal;
const post = { item_id: id, username: userVal, comment: commentVal };
btn.value = 'Processing...';
btn.disabled = true;
await postComment(post);
const response = await fetch(
`https://us-central1-involvement-api.cloudfunctions.net/capstoneApi/apps/XkZb08sfqWimSB3Sqtb3/comments?item_id=${id}`,
);
const comments = await response.json();

ul.innerHTML = '';
comments.forEach((comPost) => {
ul.innerHTML += `
<li><p>${comPost.creation_date}</p><p>${comPost.username}</p><p>${comPost.comment}</p></li>
`;
commentCount.innerHTML = '';

commentCount.innerHTML = `Comments (${comments.length})`;
btn.value = 'Comment';
btn.disabled = false;
username.value = '';
comment.value = '';
});
});

const response = await fetch(
`https://us-central1-involvement-api.cloudfunctions.net/capstoneApi/apps/XkZb08sfqWimSB3Sqtb3/comments?item_id=${meal.idMeal}`,
);
const comments = await response.json();

if (!comments.length) return;

comments.forEach((comPost) => {
ul.innerHTML += `
<li><p>${comPost.creation_date}</p><p>${comPost.username}:</p><p>"${comPost.comment}"</p></li>
`;
commentCount.innerHTML = '';

commentCount.innerHTML = `Comments (${comments.length})`;
});
};
4 changes: 2 additions & 2 deletions src/defaultTemplate.js → src/modules/defaultTemplate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/no-cycle */
import { getMealsLength } from './apis/food.js';
import { getMealsLength } from '../apis/food.js';

export default async (defaultFood, mealsEl, getLike, resultHeading) => {
const meals = await defaultFood;
Expand Down Expand Up @@ -31,7 +31,7 @@ export default async (defaultFood, mealsEl, getLike, resultHeading) => {
</div>
<div class="comment-like">
<button class="meal-btn" data-mealID="${
<button class="meal-btn comments-btn" data-mealID="${
meal.idMeal
}">Comments<svg class="meal-svg" width="3" height="6" viewBox="0 0 3 6" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style=""><path d="M0 0L3 3L0 6"></path></svg>
</button>
Expand Down
2 changes: 1 addition & 1 deletion src/template.js → src/modules/template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/no-cycle */
import { getMealsLength } from './apis/food.js';
import { getMealsLength } from '../apis/food.js';

export default async (searchFood, mealsEl, resultHeading) => {
// Change HTML
Expand Down
Loading

0 comments on commit d8c9925

Please sign in to comment.