-
Notifications
You must be signed in to change notification settings - Fork 0
/
addblog.js
64 lines (56 loc) · 1.83 KB
/
addblog.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
function saveBlog(event) {
event.preventDefault();
var fileInput = document.getElementById('upload-file');
if (fileInput.files.length > 0) {
var file = fileInput.files[0];
var fileName = file.name;
console.log('Selected file name:', fileName);
} else {
console.log('No file selected');
}
}
document
.getElementById("submit-button")
.addEventListener("click",() => {
console.log("Clickedddddddddddddddddddddddd");
let isloading = true;
const title = document.getElementById("blog_title").value;
const content = document.getElementById("blog_content").value;
const image = document.getElementById("upload-file").files[0];
console.log(title,content,image);
if (!title || !image) {
alert("Please enter a title or image")
return;
}
posting_Blog_func(title, content, image);
});
const posting_Blog_func = async (title, content, cover) => {
console.log(title,content,cover,"okay ");
try {
const authtoken = localStorage.getItem("jwt");
const formData = new FormData();
formData.append("title", title);
formData.append("content", content);
formData.append("image", cover);
let isloading = true;
if (isloading) {
document.getElementById("submit-button").style.display = "none";
document.getElementById("submit-buttonn").style.display = "block";
}
const res = await axios({
method: "POST",
url: "https://mybrandbackend-93l8.onrender.com/api/blog",
data: formData,
headers: {
Authorization: `Bearer ${authtoken}`,
"Content-Type": "multipart/form-data",
},
});
console.log(res);
setTimeout(() => {
window.location.href="index.html"
}, 2000);
} catch (e) {
console.log(e);
}
};