-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle.js
More file actions
154 lines (120 loc) · 6.76 KB
/
article.js
File metadata and controls
154 lines (120 loc) · 6.76 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
//show-hide comments
function addEventListenersForHideComments () {
Array.from(document.getElementsByClassName('comments-block-wrapper')).map((el) =>{
//cпрятать по умолчанию все комменты
el.querySelector('.comments-wrapper').classList.add('show-hide-comments');
//добавить ивентлистенер для хедеров коментов
el.querySelector('.comments-header').addEventListener('click', ()=> {
if(el.querySelector('.comments-header p span').textContent >= 1) {
//добавить/снять класс скрывающий комменты
el.querySelector('.comments-wrapper').classList.toggle('show-hide-comments');
//добавить/снять надпись show/hide comments
if(el.querySelector('.show-hide-comments-label').textContent == 'Show comments'){
el.querySelector('.show-hide-comments-label').textContent = 'Hide comments';
} else {
el.querySelector('.show-hide-comments-label').textContent = 'Show comments';
}
} else {
el.querySelector('.show-hide-comments-label').style.display = 'none';
}
});
})
}
//function to capitalise 1 leter of title Names
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function randomCommentNumber() {
return Math.floor((Math.random()*5))+1;
}
const wrapperForArticles = document.getElementById('content'); //const for Fetch output
//----------------------------------------------------------------------------FETCH JSON BLA BLA BLA--------------------------------------------------------------
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const postId = urlParams.get('post_id');
console.log(postId);
fetch('https://jsonplaceholder.typicode.com/posts/'+ postId)
.then(response => response.json())
.then((post) => {
let articleWrapper = document.createElement("div");
articleWrapper.classList.add("article-wrapper");
let articleContent = document.createElement("div");
articleContent.classList.add("article-content");
let articleHeader = document.createElement("h1");
articleHeader.classList.add("article-header");
let articleHeaderA = document.createElement('a');
articleHeaderA.classList.add("title-anc");
articleHeaderA.setAttribute("href", "article.html?post_id=" + post.id); //ALL MAGIC with send info about page ID
articleHeaderA.textContent = capitalizeFirstLetter(post.title);
let articleMaintext = document.createElement('p');
articleMaintext.classList.add("article-maintext");
articleMaintext.textContent = capitalizeFirstLetter(post.body+post.body+post.body+post.body+post.body);
let aboutArticle = document.createElement("div");
aboutArticle.classList.add("about-article");
let authorWrapper = document.createElement('div');
authorWrapper.classList.add("author-wrapper");
let authorA = document.createElement('a');
authorA.classList.add("author-" + post.userId);
authorA.classList.add("author");
authorA.setAttribute("href", "user.html?user_id=" + post.userId);
fetch('https://jsonplaceholder.typicode.com/users?id='+ post.userId)
.then(response => response.json())
.then((userInfo) => {
authorA.textContent = userInfo[0].name;
})
let commentsBlockWrapper = document.createElement("div");
commentsBlockWrapper.classList.add("comments-block-wrapper");
let commentsHeader = document.createElement("div");
commentsHeader.classList.add("comments-header");
let commentsHeaderP = document.createElement("p");
commentsHeaderP.textContent = "Comments ";
let commentsHeaderPSpan = document.createElement("span");
let showHideCommentsLabel = document.createElement("div");
showHideCommentsLabel.classList.add("show-hide-comments-label");
showHideCommentsLabel.textContent = "Show comments";
let commentsWrapper = document.createElement("div");
commentsWrapper.classList.add("comments-wrapper");
fetch('https://jsonplaceholder.typicode.com/posts/'+ post.id +'/comments')
.then(response => response.json())
.then((comments) => {
let nComments = randomCommentNumber();
commentsHeaderPSpan.textContent = nComments;
for(let i =0; i<nComments; i++){
let comment = document.createElement("div");
comment.classList.add("comment");
let aboutComment = document.createElement("div");
aboutComment.classList.add("about-comment");
let commentAuthor = document.createElement("p");
commentAuthor.classList.add("comment-author");
commentAuthor.textContent = capitalizeFirstLetter(comments[i].name);
let commentTime = document.createElement("p");
commentTime.classList.add("comment-time");
commentTime.textContent = comments[i].email;
let commentContent = document.createElement("div");
commentContent.classList.add("comment-content");
let commentText = document.createElement("p");
commentText.classList.add("comment-text");
commentText.textContent = comments[i].body;
commentsWrapper.append(comment);
comment.append(aboutComment);
aboutComment.append(commentAuthor, commentTime);
comment.append(commentContent);
commentContent.append(commentText);
}
})
wrapperForArticles.append(articleWrapper);
articleWrapper.append(articleContent);
articleContent.append(articleHeader);
articleHeader.append(articleHeaderA);
articleContent.append(articleMaintext);
articleWrapper.append(aboutArticle);
aboutArticle.append(authorWrapper);
authorWrapper.append(authorA);
articleWrapper.append(commentsBlockWrapper);
commentsBlockWrapper.append(commentsHeader);
commentsHeader.append(commentsHeaderP);
commentsHeaderP.append(commentsHeaderPSpan);
commentsHeader.append(showHideCommentsLabel);
commentsBlockWrapper.append(commentsWrapper);
addEventListenersForHideComments();
})