forked from assembler-institute/blog-with-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjQuery.js
More file actions
182 lines (163 loc) · 5.83 KB
/
Copy pathjQuery.js
File metadata and controls
182 lines (163 loc) · 5.83 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
let urlPosts = 'https://jsonplaceholder.typicode.com/posts/';
let urlUsers = 'https://jsonplaceholder.typicode.com/users';
//_________ USER NAME ON THE POST__________//
for (let i = 1; i < 11; i++) {
for (let k = 1; k < 11; k++) {
var j = (i - 1) * 10 + k;
; getUser(i, j);
};
};
//_________INSERT DIVS IN HTML__________//
for (let id = 1; id < 101; id++) {
getPost(id);
if (id > 1) {
var post = $('<div class="col-md-6" id="postContainer' + id + '"><div class="row g-0 border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative post-box" id="box' + id + '"> <div class="col p-4 d-flex flex-column position-static"><h3 class="mb-3 titlePost" id="titlePost' + id + '">Featured post</h3><p class="card-text mb-auto bodyPost" id="bodyPost' + id + '"></p> <div class="post-creator"><strong class="d-inline-block mb-2 text-warning" id="userName' + id + '"></strong></div><div class="button-flex"><button type="button" class="btn btn-primary modalBtn" data-toggle="modal" data-target="#exampleModal" data-btn-id="' + id + '">Read post</button></div></div></div></div>')
$('#postDiv').append(post);
};
};
//_________GET POSTS DATA__________//
function getPost(id) {
var settings = {
"url": urlPosts,
"method": "GET",
"data": {
id: id,
},
"timeout": 0,
};
$.ajax(settings)
.done(function (response) {
response.forEach(data => {
createPost(id, data);
});
});
};
//_________CREATE POST__________//
function createPost(id, data) {
if (id > 1) {
$('#titlePost' + id).text(data.title);
$('#bodyPost' + id).text(data.body);
$(".post-box").hover(
function () {
$(this).addClass('shadow-lg').css('cursor', 'pointer');
}, function () {
$(this).removeClass('shadow-lg');
});
} else {
$('#postText').text(data.body);
$('#postTitle').text(data.title);
};
};
//_________GET POST USERS__________//
function postUserData(userId) {
var userSettings = {
"url": urlUsers,
"method": "GET",
"data": {
id: userId,
},
"timeout": 0,
};
$.ajax(userSettings)
.then(function (response) {
$('#modal-name').text(response[0].name);
$('#modal-email').text(response[0].email);
});
};
//_________GET USERS__________//
function getUser(userId, postId) {
var userSettings = {
"url": urlUsers,
"method": "GET",
"data": {
id: userId,
},
"timeout": 0,
};
$.ajax(userSettings)
.then(function (response) {
if (postId > 1) {
$('#userName' + postId).text(response[0].name);
} else {
$('#userName').text(response[0].name);
};
});
};
//_________GET MODAL INFO & RUN MODAL FUNCTIONS__________//
$('.modalBtn').each(function () {
$(this).off();
$(this).on('click', function () {
$('.btn-danger').off();
var postId = $(this).data('btn-id');
if (postId > 1) {
$('#modalBody').text($('#bodyPost' + postId).text());
$('#exampleModalLabel').text($('#titlePost' + postId).text());
} else {
$('#modalBody').text($('#postText').text());
$('#exampleModalLabel').text($('#postTitle').text());
};
$('.edit-modal').hide();
$('.modal-content').show();
$('#edit-post').off();
$('#edit-post').on('click', function () {
editPost(postId);
});
getComments(postId);
deletePost(postId);
});
});
//_________ GET POST COMMENTS__________//
function getComments(postId) {
var settings = {
"url": "https://jsonplaceholder.typicode.com/posts/" + postId + "/comments/",
"method": "GET",
"timeout": 0,
};
$.ajax(settings).done(function (response) {
$('.collapse').empty();
response.forEach(function (data) {
$('.collapse').append('<div class="card card-body p-5"><p class="data-name">' + data.name + '</p><p class="data-body p-4 bg-light rounded">' + data.body + '<p><p class="data-email">' + data.email + '<img src="img/user.png" width="20" height="20"></p></div>');
});
});
};
//___________EDIT POST_____________//
function editPost(postId) {
console.log('Post ID: ' + postId);
$('.edit-modal').hide();
let newModal = document.querySelector("template#editModal");
const importNewEvent = document.importNode(newModal.content, true);
$(".modal-dialog").append(importNewEvent);
$('#closeModal').off();
$('#buttonEdit').on('click', function () {
console.log('Post ID: ' + postId);
if (postId > 1) {
console.log('Post ID: ' + postId);
$('#bodyPost' + postId).text($('#newBody').val());
$('#titlePost' + postId).text($('#newTitle').val());
} else {
console.log('Post ID: ' + postId);
$('#modalBody').text($('#newBody').val());
$('#exampleModalLabel').text($('#newTitle').val());
};
console.log('funciona')
})
$('#closeModal').on('click',function () {
console.log('Hey');
$('.edit-modal').hide();
})
};
//____________DELETE POST_____________//
function deletePost(postId) {
$('.btn-danger').on('click', function () {
if (postId > 1) {
console.log('Deleted post: ' + postId);
$("#postContainer" + postId).remove();
} else {
var post = $('#postDiv').children()[0].id;
var id = post.slice(-1);
$('#postText').text($('#bodyPost' + id).text());
$('#postTitle').text($('#titlePost' + id).text());
$('#postDiv').children()[0].remove();
};
});
}