-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.js
121 lines (86 loc) · 3.15 KB
/
profile.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
import {getDoc,doc,db,updateDoc,storage,uploadBytesResumable,getDownloadURL, ref} from "./firebase.js"
let content = document.getElementById("content");
let loader = document.getElementById("loader");
loader.style.display="flex"
content.style.display="none"
var fileInput = document.getElementById("fileInput")
fileInput.addEventListener("change",(e)=>{
profilepicture.src = URL.createObjectURL(e.target.files[0])
})
const uploadFile = (file) =>{
return new Promise((resolve,reject)=>{
const mountainImagesRef = ref(storage, `images/${file.name}`);
const uploadTask = uploadBytesResumable(mountainImagesRef, file);
uploadTask.on('state_changed',
(snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case 'paused':
console.log('Upload is paused');
break;
case 'running':
console.log('Upload is running');
break;
}
},
(error) => {
reject(error);
console.log(reject)
},
() => {
getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
resolve( downloadURL);
});
}
);
})
}
var userid = localStorage.getItem("userid")
var loginuserid = localStorage.getItem("loginuserid")
let email = document.getElementById("login-email")
let fullname = document.getElementById("fullname")
let infotext = document.querySelector(".info-text")
var profilepicture = document.getElementById("profilepicture")
const docRef = doc(db, "users", userid || loginuserid );
loader.style.display="flex"
content.style.display="none"
const docSnap = await getDoc(docRef);
if (docSnap.exists()) {
fullname.value = docSnap.data().name
email.value = docSnap.data().email
profilepicture.src = docSnap.data().profile && docSnap.data().profile !== "undefined" ? docSnap.data().profile : "images/download.png"
loader.style.display="none"
content.style.display="block"
}
else {
console.log("No such document!");
}
infotext.innerHTML = (
`
<h3><i class='bx bxs-left-arrow' style='color:#f5f0f0' ></i><a href="./dashboard.html">My BLogs</a></h3>
<h2 id="usernameheading">Welcome ${docSnap.data().name}</h2>
<p >Lorem ipsum dolor sit amet consectetur adipisicing elit. </p>
`
)
let updateBtn = document.getElementById("updatebtn")
updateBtn.addEventListener("click",async()=>{
let fullname = document.getElementById("fullname")
loader.style.display="flex"
content.style.display="none"
const user = {
name: fullname.value,
}
if(fileInput.files[0]){
user.profile = await uploadFile(fileInput.files[0])
}
const userRef = doc(db, "users", userid || loginuserid );
await updateDoc(userRef, user );
loader.style.display="none"
content.style.display="block"
Swal.fire(
'Profile!',
'Your Profile is Updated!',
'success'
)
})