31
loading...
This website collects cookies to deliver better user experience
Sorry about posting this one late I was working on this and drafted it for a while and thinking to post this one everyday and finally did it today.
Sorry for my procrastinating issues.
complete
uploadTask.on('state_changed',
(snapshot) => {
// Handle upload Progress
},
(error) => {
// Handle unsuccessful upload
},
() => {
//Handle complete upload
task.snapshot.ref.getDownloadURL().then((downloadURL) => {
updateDatabase(downloadURL, firebase.auth().currentUser);
});
}
);
// Update Database Function
const db = firebase.firestore();
const dbRef = db.collection('posts');
const updateDatabase = (link,user) => {
let post = {
postid : makeid(7),
heading:headingPhoto.value,
By : user.displayName,
uid : user.uid,
img : link,
time : firebase.firestore.FieldValue.serverTimestamp()
}
dbRef.add(post)
}
const makeid = (length) => {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
.where
query to get data in all feed we will display all posts and in user profile we will change it to .where('uid','==',user.uid)
var allfeedStatus = false;
var userfeedStatus = false;
// sub all will subscribe to all feed
subAll = () => {
if(allfeedStatus == false){
allfeed = dbRef.onSnapshot((querySnapshot) => {
photoContainer.innerHTML = ''
querySnapshot.forEach((doc) => {
drawPhotos(doc.data().img,doc.data().By,doc.data().heading);
});
});
allfeedStatus = true;
userSection.hidden = true;
}
if(userfeedStatus){
userfeed();
userfeedStatus = false;
}
}
// sub profile will subscribe to the profile feed
subProfile = () => {
if(userfeedStatus == false){
userfeed = dbRef.where('uid','==',auth.currentUser.uid).onSnapshot((querySnapshot) => {
photoContainer.innerHTML = ''
querySnapshot.forEach((doc) => {
drawPhotos(doc.data().img,doc.data().By,doc.data().heading);
});
});
userSection.hidden = false;
userfeedStatus = true;
}
if(allfeedStatus == true){
allfeed();
allfeedStatus = false;
}
}
subAll();
draw post
which will add the posts to the page.