-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmybrand.js
397 lines (318 loc) · 13.1 KB
/
mybrand.js
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// the beggining of slide function
// function toggleMenu() {
// // alert("I am clicked!");
// const menu = document.querySelector('.menu');
// menu.classList.toggle('active');
// }
// function closeMenu() {
// const menu = document.querySelector('.menu');
// menu.classList.remove('active');
// }
// function xScroll(horizontal){
// const first = document.querySelector('.box6-over')
// const second = document.querySelector('.cont-box6-1').offsetWidth;
// const third = document.querySelector('')
// if(horizontal == "next"){
// first.scrollLeft += second + 10;
// }
// else if(horizontal == "prev"){
// first.scrollLeft -= second + 10;
// }
// }
// function autoSlide() {
// const container = document.querySelector('.myBlog');
// const cards = document.querySelectorAll('.myCard');
// const firstCard = cards[0];
// const cardWidth = firstCard.offsetWidth + parseInt(window.getComputedStyle(firstCard).marginRight);
// // Clone the first card and append it to the end
// container.appendChild(firstCard.cloneNode(true));
// let position = 0;
// // Slide cards to the left
// function slideLeft() {
// position -= cardWidth;
// container.style.transform = `translateX(${position}px)`;
// container.style.transition = 'transform 1s ease-in-out';
// // Reset position and transition when last card is reached
// if (position <= -container.scrollWidth + cardWidth) {
// setTimeout(() => {
// position = 0;
// container.style.transform = `translateX(${position}px)`;
// container.style.transition = 'none';
// }, 1000);
// }
// }
// // Automatically slide cards every 3 seconds
// setInterval(slideLeft, 5000);
// }
// // Call autoSlide function when the page is loaded
// window.addEventListener('load', autoSlide);
// this the end of slide function
// The beggining of form validation
// const form = document.getElementById('form')
// var username = document.getElementById('username')
// const email = document.getElementById('email')
// const locate = document.getElementById('in_3')
// const sub = document.getElementById('in_4')
// const mess = document.getElementById('message')
// const pass = document.getElementById('pass_1')
// const confirm = document.getElementById('pass_3')
// form.addEventListener('submit', e =>{
// e.preventDefault();
// validateInputs();
// });
// const verError = (Element,message) => {
// const errorControl = Element.parentElement;
// const showError = errorControl.querySelector('.error');
// showError.innerText = message;
// errorControl.classList.add('error')
// errorControl.classList.remove('success')
// }
// const verPass = Element =>{
// const errorControl = Element.parentElement;
// const showError = errorControl.querySelector('.error');
// showError.innerText = '';
// errorControl.classList.add('success')
// errorControl.classList.remove('error')
// }
// const checkEmail = email =>{
// const sign = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
// return sign.test(String(email).toLowerCase());
// }
// function validateInputs(){
// const fullName = username.value.trim();
// const inEmail = email.value.trim();
// const oneLocate = locate.value.trim();
// const subJect = sub.value.trim();
// const messValue = mess.value.trim();
// if(fullName === ''){
// verError(username, 'Name field is required!');
// } else if(fullName < 8){
// verError(username, 'username must not be less than 8 character')
// }
// else{
// verPass(username);
// }
// if(inEmail ===''){
// verError(email, 'Email field is Required!');
// }
// else if(!checkEmail(inEmail)){
// verError(email, 'Enter the valid email!');
// }
// else{
// verPass(email)
// }
// if(oneLocate === ''){
// verError(locate, 'Location field is required!');
// }
// else{
// verPass(locate);
// }
// if(subJect === ''){
// verError(sub, 'Subject field is required!')
// }
// else{
// verPass(sub)
// }
// if(messValue === ''){
// verError(mess, 'Message field is required');
// }
// else{
// verPass(mess);
// }
// }
// the end of form validation
// function retrieve() {
// let myBlogs = [];
// let allBlogs = "https://mybrand-be-6rxz.onrender.com/api/blogs";
// let thePic = localStorage.getItem("theImg");
// myBlogs = allBlogs ? JSON.parse(allBlogs) : [];
// let container = document.getElementById('all');
// // let house = document.getElementById('single');
// myBlogs.forEach(myBlog => {
// const card = document.createElement("div")
// card.innerHTML = `
// <div class="myCard">
// <img src="./Pictures/benjamin-zanatta-WbkfJ2TmSug-unsplash.jpg" alt="" id="pic-1">
// <div id="word-1">
// <p class="cont-box6-p">22 Oct, 2020 <br> <br>
// <i class="fa-regular fa-thumbs-up"> 4k</i>
// <i class="fa-solid fa-comment"></i> 246 Comments
// </p>
// </div>
// <div id="the-1">
// <h1>${myBlog.myContent}</h1>
// </div>
// <div id="p-2">
// <p>${myBlog.areaText}</p>
// </div>
// <div id="our-btn">
// <button id="press-1"><a href="./Single-Blog/Blog.html" target="_blank" id="under">Visit</a></button>
// </div>
// </div>
// `;
// document.addEventListener("DOMContentLoaded", () => {
// const getImageUrl = localStorage.getItem("imagePath");
// if(getImageUrl){
// document.querySelector('#insertImg').setAttribute("src", getImageUrl);
// }
// });
// container.appendChild(card);
// });
// }
// retrieve();
fetch('https://mybrand-be-6rxz.onrender.com/api/blogs')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(blogs => {
const blogsContainer = document.getElementById('Blogs');
blogs.forEach(blog => {
// console.log(blog);
const blogElement = document.createElement('div');
blogElement.setAttribute('key', blog._id);
blogElement.className = 'cont-card';
// Limiting content to a fixed size
const truncatedContent = blog.content.length > 100 ? blog.content.substring(0, 100) + '...' : blog.content;
blogElement.innerHTML = `
<img src="${blog.image}" alt="img-box6" class="img-box6">
<p class="cont-box6-p">
<span><i class="fa-regular fa-thumbs-up" id="like"> ${blog.like}</i></span>
<span><i class="fa-solid fa-comment" id="comment"></i> 246 Comments </span>
</p>
<h2>${blog.title}</h2>
<p class="cont-desc-p">${truncatedContent}</p>
<span class="a-box6">Read more
<i class="fa-solid fa-arrow-right"></i>
</span>
`;
blogsContainer.appendChild(blogElement);
// Adding event listener to show full content
const readMoreLinks = blogElement.querySelectorAll(".a-box6");
readMoreLinks.forEach((link) => {
link.addEventListener("click", (e) => {
const id = e.target.closest(".cont-card").getAttribute("key");
window.location.href = `/Single-Blog/Blog.html?id=${id}`;
});
});
});
})
.catch(error => console.error('Error fetching blogs:', error));
var form = document.getElementById('form');
var userName = document.getElementById('username');
var userEmail = document.getElementById('email');
var userMessage = document.getElementById('message');
if (form && userName && userEmail && userMessage) {
form.addEventListener('submit', function (e) {
e.preventDefault();
validateInputs();
});
}
function validateInputs() {
var userValue = userName.value.trim();
var emailValue = userEmail.value.trim();
var messageValue = userMessage.value.trim();
if (userValue === '') {
verError(userName, 'Name field is required!');
} else if (userValue.length < 8) {
verError(userName, 'Username must be at least 8 characters long');
} else {
// verPass(username);
if (emailValue === '') {
verError(userEmail, 'Email field is required!');
} else if (!checkEmail(emailValue)) {
verError(userEmail, 'Enter a valid email!');
} else {
// verPass(email);
if (messageValue === '') {
verError(userMessage, 'Message field is required');
} else {
// verPass(message);
const data = {
name: userValue,
email: emailValue,
message: messageValue
};
fetch('https://mybrand-be-6rxz.onrender.com/api/query', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => {
if (response.ok) {
console.error("Query created successfully");
showToast("Query created successfully");
userName.value = '';
userEmail.value = '';
userMessage.value = '';
} else {
console.log("Creating a query failed");
// showToast("Creating a query failed");
}
})
.catch(error => {
console.error("Error:", error);
showToast("Error occurred");
});
}
}
}
}
function verError(element, message) {
var errorControl = element.parentElement;
var showError = errorControl.querySelector('.error');
showError.innerText = message;
errorControl.classList.add('error');
errorControl.classList.remove('success');
}
function verPass(element) {
var errorControl = element.parentElement;
var showError = errorControl.querySelector('.error');
showError.innerText = '';
errorControl.classList.add('success');
errorControl.classList.remove('error');
}
function checkEmail(email) {
var sign = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return sign.test(String(email).toLowerCase());
}
function showToast(message) {
Toastify({
text: message,
duration: 3000,
gravity: "top",
position: "right",
}).showToast();
}
// const blogContainer = document.getElementById("Blogs");
// const fetchBlogs = async () => {
// try{
// const response = await fetch('https://mybrand-be-6rxz.onrender.com' + '/api/blogs');
// const data = await response.json();
// blogContainer.innerHTML = '';
// data.forEach( blog => {
// const blogElement = document.createElement('div');
// blogElement.classList.add('myCard');
// blogElement.innerHTML = `
// <span class="myCard">
// <img src="${blog.image}" alt="img-box6" class="img-box6">
// <p class="cont-box6-p">22 Oct, 2020 <br> <br>
// <i class="fa-regular fa-thumbs-up"> 4k</i>
// <i class="fa-solid fa-comment"></i> 246 Comments
// </p>
// <p class="cont-desc-p">${blog.content}</p>
// <a href="./Single-Blog/Blog.html" class="a-box6">visit Site <i
// class="fa-solid fa-arrow-right"></i></a>
// </span>
// `
// blogContainer.appendChild(blogElement);
// });
// }catch(error){
// console.log(error);
// }
// };
// fetchBlogs();