-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
324 lines (274 loc) · 8.59 KB
/
api.js
File metadata and controls
324 lines (274 loc) · 8.59 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"use strict";
const feed_div = document.querySelector("#feed");
const contactList_ul = document.querySelector("#contact-list > ul");
const popularTags = document.querySelector("#tags");
const tagsPage = document.querySelector("#tagsPage");
const socials = document.querySelector("#profiles");
const getFullProfile = document.querySelector("#fullProfile");
// APP ID
const appId = "6105ab02b5d16d17954c0f6a";
// GET USER PROFILE
async function getUserData(id) {
try {
const endPoint = `https://dummyapi.io/data/api/user/${id}`;
const xhr = new XMLHttpRequest();
xhr.open("GET", endPoint);
xhr.setRequestHeader("app-id", appId);
xhr.onload = function () {
if (xhr.status == 200) {
const data = JSON.parse(xhr.responseText);
// Profil Bilgilerini Renderla
// Div Alanını Sil Ve Tekrar Doldur
feed_div.innerHTML = "";
feed_div.innerHTML = `<div class="profileSection">
<table class="socialTable">
<tr>
<th>Profile Details Of: <i>${
data.firstName + " " + data.lastName
}</i></th>
</tr>
<tr>
<td class="fullProfileSection"><img class="fullProfileImg" src="${
data.picture
}">
<ul>
<li><strong>First Name:</strong> ${data.firstName}</li>
<li><strong>Last Name:</strong> ${data.lastName}</li>
<li><strong>Email:</strong> ${data.email}</li>
<li><strong>Gender:</strong> ${data.gender}</li>
<li><strong>Birthdate:</strong> ${normalizeISODateT(
data.dateOfBirth
)}</li>
<li><strong>Phone:</strong> ${data.phone}</li>
<li><strong>Location:</strong> ${
data.location.country +
", " +
data.location.city +
"-" +
data.location.state +
" | " +
data.location.street
}</li>
<li><strong>Time Zone:</strong> ${data.location.timezone}</li>
<li><strong>Register Date:</strong> ${normalizeISODateT(
data.registerDate
)}</li>
</ul>
</td>
</tr>
</table>
</div>
<div class="returnToHome">
<a href="index.html">Return To Feed</a>
</div>`;
console.log(data);
}
};
xhr.send();
} catch (e) {
console.log(e);
}
}
// USER ID CEKME
function setUserId(id) {
return function () {
getUserData(id);
};
}
// ISODate Formatını Tarihe Cevirme Func (Detaylı)
function normalizeISODateD(isoDate) {
const newDate = isoDate.split("T");
const time = newDate[1].split(":");
return time[0] + ":" + time[1] + " " + newDate[0];
}
// ISODate Formatını Tarihe Cevirme Func (Sadece Tarih)
function normalizeISODateT(isoDate) {
const newDate = isoDate.split("T");
return newDate[0];
}
// GET POST API
function getPosts() {
const endPoint = "https://dummyapi.io/data/api/post?limit=30";
const xhr = new XMLHttpRequest();
xhr.open("GET", endPoint);
xhr.setRequestHeader("app-id", appId);
xhr.onload = function () {
if (xhr.status == 200) {
const data = JSON.parse(xhr.responseText);
const posts = data.data;
const postsHTML = posts.map((post, index) => {
if (!post) {
return "";
}
return `
<div class="post-container">
<div class="news_feed_title">
<img src=${post.owner.picture} alt="User Picture" />
<div class="news_feed_title_content">
<p class="postOwner">${post.owner.firstName} ${
post.owner.lastName
} </p>
<span>${normalizeISODateD(
post.publishDate
)} <i class="fas fa-globe-americas"></i></span>
</div>
</div>
<div class="news_feed_description">
<p class="news_feed_subtitle">
${post.text}
</p>
${
post.image ? `<img src=${post.image} alt="Post Image" />` : ""
}
</div>
${
post.tags.length === 0
? ""
: `<div class="postTags">
<span>Tags:${post.tags}</span>
</div>`
}
<div class="interactionArea">
<div class="emojis">
<img src="img/emoji_like.png" alt="Like Emoji" />
<span>${post.likes}</span>
</div>
<div>
<a class="show-profile"> Show This Profile </a>
</div>
</div>
<div class="postDivider">
<hr />
</div>
<div class="interactionButtons">
<div class="interactionButtons-icons">
<i class="far fa-thumbs-up"></i>
<span>Like</span>
</div>
<div class="interactionButtons-icons">
<i class="far fa-comment-alt"></i>
<span>Comment</span>
</div>
<div class="interactionButtons-icons">
<i class="fas fa-share"></i>
<span>Share</span>
</div>
</div>
</div>
`;
});
for (let i = 0; i < postsHTML.length; i++) {
feed_div.innerHTML += postsHTML[i];
}
// HER BIR KULLANICININ ALTINA AYRI OZEL ID CEKME LISTENER
for (let i = 0; i < posts.length; i++) {
document
.querySelectorAll(".show-profile")
[i].addEventListener("click", setUserId(posts[i].owner.id));
}
}
};
xhr.send();
}
// GET CONTACT API
function getContacts() {
const endPoint = "https://dummyapi.io/data/api/user?limit=9";
const xhr = new XMLHttpRequest();
xhr.open("GET", endPoint);
xhr.setRequestHeader("app-id", appId);
xhr.onload = function () {
if (xhr.status == 200) {
const data = JSON.parse(xhr.responseText);
const contacts = data.data;
const contactsHTML = contacts.map((contact, index) => {
if (!contact) {
return null;
}
return `<li>
<a href="#">
<img src="${contact.picture}" alt="User Picture" />
<span>${contact.firstName + " " + contact.lastName}</span>
</a>
</li>`;
});
for (let i = 0; i < contactsHTML.length; i++) {
contactList_ul.innerHTML += contactsHTML[i];
}
}
};
xhr.send();
}
// GET USER LIST
function getUserList() {
const endPoint = "https://dummyapi.io/data/api/user?limit=50";
const xhr = new XMLHttpRequest();
xhr.open("GET", endPoint);
xhr.setRequestHeader("app-id", appId);
xhr.onload = function () {
if (xhr.status == 200) {
const data = JSON.parse(xhr.responseText);
const profile = data.data;
const userList = profile.map((pro, index) => {
if (!pro) {
return null;
}
return `
<td><img class="socialProfile" src="${pro.picture}">
<ul>
<li><strong>First Name:</strong> ${pro.title} ${pro.firstName}</li>
<li><strong>Last Name:</strong> ${pro.lastName}</li>
<li><strong>Email:</strong> ${pro.email}</li>
</ul>
</td>
<hr id="hrForMobile">`;
});
for (let i = 0; i < userList.length; i++) {
socials.innerHTML += userList[i];
}
}
};
xhr.send();
}
// GET TAG API
function getTags() {
const endPoint = "https://dummyapi.io/data/api/tag?limit=10";
const xhr = new XMLHttpRequest();
xhr.open("GET", endPoint);
xhr.setRequestHeader("app-id", appId);
xhr.onload = function () {
if (xhr.status == 200) {
const data = JSON.parse(xhr.responseText);
const tags = data.data;
const tagsHTML = tags.map((tag, index) => {
if (!tag) {
return null;
}
return `
<a href="#">
<span>#${tag}</span>
</a>`;
});
const showTags = tags.map((hashTag, index) => {
if (!hashTag) {
return null;
}
return `
<tr>
<td>#${hashTag}</td>
</tr>`;
});
for (let i = 0; i < tagsHTML.length; i++) {
popularTags.innerHTML += tagsHTML[i];
}
for (let i = 0; i < showTags.length; i++) {
tagsPage.innerHTML += showTags[i];
}
}
};
xhr.send();
}
// API FONKSIYON CALL
getPosts();
getContacts();
getTags();
getUserList();