Repository navigation
Expand file tree
/
Copy pathpost.js
More file actions
65 lines (46 loc) · 1.96 KB
/
Copy pathpost.js
File metadata and controls
65 lines (46 loc) · 1.96 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
import renderNavigation from './navigation.js';
import { createElement, fetchData, getUrlParam, renderAllComments, renderSinglePost,renderSingleComment } from './functions.js';
async function init(){
const postId = getUrlParam('post_id')
const post = await fetchData(`https://jsonplaceholder.typicode.com/posts/${postId}?_expand=user&_embed=comments`)
let postWrapper = document.querySelector('#post-wrapper');
let editButtonLink = createElement('a','Edit Post', 'edit-post-button');
editButtonLink.href ='./edit-post.html?post_id=' + postId;
let postContent = renderSinglePost(post);
let postComments = renderAllComments (post);
let otherPosts = otherPostsList(post)
postWrapper.append(postContent, otherPosts, postComments, editButtonLink);
let createCommentForm = document.querySelector('#create-comment-form');
createCommentForm.addEventListener('submit', async (event) => {
event.preventDefault();
let name = event.target.elements.name;
let email = event.target.elements.email.value;
let body = event.target.elements.body.value;
const res = await fetch('https://jsonplaceholder.typicode.com/comments', {
method: 'POST',
body: JSON.stringify({
name,
email,
body,
postId
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
let createdComment = await res.json();
let singleCommentElement = renderSingleComment(createdComment);
let commentsList = postComments.querySelector('.comments-list');
commentsList.append(singleCommentElement);
event.target.reset();
})
}
function otherPostsList(post){
let moreAuthorPostsElement = document.createElement('a');
moreAuthorPostsElement.classList.add('more-posts');
moreAuthorPostsElement.href = './posts.html?user_id=' + post.user.id;
moreAuthorPostsElement.textContent = `Other posts of ${post.user.name}`;
return moreAuthorPostsElement;
}
// renderSingleComment()
init ()