-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.html
263 lines (202 loc) · 11.5 KB
/
home.html
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
<!--Ad Post Area-->
<div class="jumbotron bg-dark" style="margin-top: 15px;">
<script>
var counter = 0;
</script>
<div class="container text-center">
<form id="main-form">
<div class="form-group">
<textarea type="text" rows="5" placeholder="Description" class="form-control"
id="main-desc"></textarea>
<div class="invalid-feedback">
Please write some description
</div>
</div>
<div class="form-group">
<input type="file" class="form-control" id="main-image" />
<div class="invalid-feedback">
Please choose a valid picture
</div>
</div>
<div class="form-group">
<img id="selected-image" width="250" height="150" src="#" />
</div>
<div class="form-group">
<div class="progress bg-secondary">
<div class="progress-bar bg-success" id="upload-progress" style="width: 0%">0%</div>
</div>
</div>
<div class="form-group">
<button id="save-blog" type="button" style="width: 150px; height: 60px;"
class="btn btn-light bg-light text-dark">Post</button>
</div>
</form>
<div id="result">
</div>
</div>
</div>
<!--Post Area-->
<!--After Post Area-->
<hr>
<br><br><br>
<div class="text-center bg-light text-dark">
<h3>All New Posts</h3>
</div>
<hr>
<br>
<div class="row container-fluid bg-3">
<div class="col-sm-12" id="blogs">
</div>
</div>
<br>
<!--After Post Area End here -->
<!--Validations and uploading of post blogs -->
<script>
var validImagetypes = ["image/gif", "image/jpeg", "image/png"];
$("#selected-image").hide();
function previewImage(image_blog) {
if (image_blog.files && image_blog.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$("#selected-image").attr('src', e.target.result);
$("#selected-image").fadeIn();
}
reader.readAsDataURL(image_blog.files[0]);
$("#selected-image").show();
}
}
$("#main-image").change(function () {
previewImage(this);
});
$("#save-blog").click(function () {
$("#main-desc").removeClass("is-invalid");
$("#main-image").removeClass("is-invalid");
var desc = $("#main-desc").val();
var picture = $("#main-image").prop("files")[0];
if (!desc) {
$("#main-desc").addClass("is-invalid");
return;
}
if (!picture) {
$("#main-image").addClass("is-invalid");
return;
}
if ($.inArray(picture["type"], validImagetypes) < 0) {
$("#main-image").addClass("is-invalid");
return;
}
// Upload and save to firebase storage
var databaseRef = firebase.database().ref().child("Promotions");
databaseRef.once("value").then(function (snapshot) {
var name = picture["name"];
var date = new Date();
var dateStr = date.getTime();
var fileCompleteName = name + "_" + dateStr;
var storageRef = firebase.storage().ref().child("Prom Images");
var blogStorageRef = storageRef.child(fileCompleteName);
var uploadTask = blogStorageRef.put(picture);
uploadTask.on("state_changed",
function progress(snapshot) {
var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
$("#upload-progress").html(Math.round(percentage) + "%");
$("#upload-progress").attr("style", "width" + percentage + "%");
},
function error(err) {
},
function complete() {
var user = firebase.auth().currentUser;
var userName;
firebase.database().ref('Admin/' + user.uid).once('value').then(function (snapshot) {
var fName = (snapshot.val() && snapshot.val().firstName);
var sName = (snapshot.val() && snapshot.val().lastName);
userName = fName + "_" + sName;
});
uploadTask.snapshot.ref.getDownloadURL().then(function (downloadUrl) {
var time = new Date();
var options = {
weekday: "long",
month: "long",
day: "2-digit",
year: "numeric",
};
var blogData = {
"image": downloadUrl,
"name": fileCompleteName,
"desc": desc,
"uid": user.uid,
"counter": 5000 - counter,
"name": userName,
"time": time.toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true }),
"date": time.toLocaleDateString('en-US', options),
};
var newPostRef = databaseRef.push();
newPostRef.set(blogData, function (err) {
if (err) {
$("#result").attr("class", "alert alert-danger");
$("#result").html(err.message);
}
else {
$("#result").attr("class", "alert alert-success");
$("#result").html("Propmotion has been Uploaded successfully.");
window.open("", "_self");
}
});
});
}
);
});
});
function resetForm() {
$("#main-form")[0].reset();
$("#selected-form").fadeOut();
$("#upload-form").html("Completed");
}
//start rertrive data from the datbase
var firebaseConfig = {
apiKey: "AIzaSyDVSNwiJxw-godBSv-gzFzYgNUXZU81ksY",
authDomain: "ptms-4b687.firebaseapp.com",
databaseURL: "https://ptms-4b687.firebaseio.com",
projectId: "ptms-4b687",
storageBucket: "ptms-4b687.appspot.com",
messagingSenderId: "1028667957586",
appId: "1:1028667957586:web:698a3ac97b71487e131a8b",
measurementId: "G-DPJE0QW44H"
};
// Initialize Firebase
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
//firebase.initializeApp(firebaseConfig);
firebase.auth.Auth.Persistence.LOCAL;
var dblogs = firebase.database().ref().child("Promotions").orderByChild("counter");
dblogs.on("value", function (blogs) {
if (blogs.exists()) {
var blogsHtml = "";
blogs.forEach(function (singleBlog) {
counter = counter + 1;
blogsHtml += "<div class='jumbotron bg-light text-dark border border-dark'>";
blogsHtml += "<div> <img width='1000px' height='550px' src='";
blogsHtml += singleBlog.val().image;
blogsHtml += "'/></div><br>";
blogsHtml += "<div class='row'>";
blogsHtml += "<div class='col-sm-5'> <p style='grey';>"
+ "published by: " + singleBlog.val().userName
+ "</p></div>" +
"<div class='col-sm-3'> <p style='grey';>"
+ "Time : " + singleBlog.val().time
+ "</p></div>" +
"<div class='col-sm-4'> <p style='grey; margin-left: 15px;'>"
+ "Date : " + singleBlog.val().date
+ "</p></div>"
;
blogsHtml += "</div><br>";
blogsHtml += "<div style='text-align: justify; color: black;'>";
blogsHtml += singleBlog.val().desc;
blogsHtml += "</div><br>";
blogsHtml += "</div>";
});
$("#blogs").html(blogsHtml);
}
});
//end recieve data from the database
</script>