-
Notifications
You must be signed in to change notification settings - Fork 0
/
test3.js
135 lines (110 loc) · 3.74 KB
/
test3.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
const form = document.getElementById('form');
const title = document.getElementById('title');
const date = document.getElementById('date');
const body = document.getElementById('body');
const picture = document.getElementById('picture');
const category = document.getElementById('category');
const statuse = document.getElementById('statuse');
const addBlog = document.getElementById('addBlog');
form.addEventListener('submit', e => {
e.preventDefault();
validateInputs();
});
const setError = (element, message) => {
const inputControl = element.parentElement;
console.log(inputControl + "input control")
const errorDisplay = inputControl.querySelector('.error');
console.log(errorDisplay + "error display");
errorDisplay.innerText = message;
inputControl.classList.add('error');
console.log(inputControl.classList.add('error'))
inputControl.classList.remove('success')
}
const setSuccess = element => {
const inputControl = element.parentElement;
const errorDisplay = inputControl.querySelector('.error');
errorDisplay.innerText = '';
inputControl.classList.add('success');
inputControl.classList.remove('error');
};
const validateInputs = () => {
const titleValue = title.value.trim();
const dateValue = date.value.trim();
const bodyValue = body.value.trim();
const pictureValue = picture.files;
const categoryValue = category.value.trim();
const statusValue = statuse.value.trim();
if(titleValue === '') {
setError(title, 'title is required');
} else {
const pattern = /^[a-zA-Z]+(\s[a-zA-Z]+)*$/;
if (!pattern.test(titleValue)) {
setError(title, 'Title should contain only characters');
} else {
setSuccess(title);
}
}
if(dateValue === '') {
setError(date, 'date is required');
} else {
setSuccess(date);
}
if(pictureValue.length === 0) {
setError( picture, 'image is required');
} else {
const file = pictureValue[0];
const fileType = file.type;
const fileSize = file.size;
if (fileType !== 'image/jpeg' && fileType !== 'image/png' && fileType !== 'image/jpg') {
setError(picture, 'File type should be jpeg, png, or jpg');
} else {
setSuccess( picture);
}
}
if(bodyValue === '') {
setError(body, 'Body is required');
} else {
setSuccess(body);
}
if(categoryValue === '') {
setError(category, 'Category is required');
} else {
setSuccess(category);
}
if(statusValue === '') {
setError(statuse, 'Status is required');
} else {
setSuccess(statuse);
}
};
let renderImage;
document.querySelector("#picture").addEventListener("change", function(){
const reader = new FileReader();
reader.addEventListener('load', () =>{
localStorage.setItem("recent-image", reader.result);
});
reader.readAsDataURL(this.files[0]);
});
blogs = JSON.parse(localStorage.getItem('blogs')) || [];
function saveBlog() {
renderImage = localStorage.getItem('recent-image');
validateInputs();
const inputControls = form.querySelectorAll('.form-group');
const hasError = [...inputControls].some((inputControl) => inputControl.classList.contains('error'));
if (hasError) {
return;
}
let blog = {};
blog.picture = renderImage
blog.title = title.value
blog.date = date.value
blog.body= body.value
blog.category = category.value
blog.statuse = statuse.value
blogs.push(blog);
const stringBlogs = JSON.stringify(blogs);
localStorage.setItem('blogs', stringBlogs);
// Redirect to login page after saving blog
window.location.replace("blogs.html");
}
addBlog.onclick = saveBlog;