-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.js
More file actions
195 lines (183 loc) · 8.1 KB
/
profile.js
File metadata and controls
195 lines (183 loc) · 8.1 KB
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
import {
auth, updateDoc, doc, db, ref, uploadBytesResumable, getDownloadURL, storage, onAuthStateChanged, getDoc
} from "./firebase.js";
const loader = document.getElementById("loader");
const mainContent = document.getElementById("mainContent");
const name = document.getElementById("name");
const email = document.getElementById("email");
const number = document.getElementById("number");
const userImage = document.getElementById("userImage");
onAuthStateChanged(auth, async (user) => {
if (user) {
const docRef = doc(db, "users", user.uid);
const docSnap = await getDoc(docRef);
if (docSnap.data()) {
if (location.pathname !== "/profile.html") {
window.location = "profile.html"
}
loader.style.display = "none"
mainContent.style.display = "block"
if (docSnap.data().photo) {
userImage.innerHTML = `
<img alt="" id="uploadIcon" src=${docSnap.data().photo} style="width: 200px; height: 200px;
cursor: pointer;" class=" mb-2 avatar avatar-user width-full border color-bg-default">
<input type="file" id="file" class="file" style="display: none;" />`
} else {
userImage.innerHTML =
`<img class="mb-2 avatar" id="uploadIcon" src="/Images/user-plus.png"
style="width: 200px; height: 200px; cursor: pointer;" />
<input type="file" id="file" class="file" style="display: none;" />`
}
if (docSnap.data().name) {
name.innerHTML = `<i class="fa-solid fa-user icon"></i>
<li style="cursor: no-drop" class="list-group-item ">${docSnap.data().name}</li>`;
} else {
name.innerHTML = `<i class="fa-solid fa-user icon"></i>
<input id="nameUpdate" class="input-field" type="text"
placeholder="Name" name="Name"></input>`;
}
if (docSnap.data().email) {
email.innerHTML = `<i class="fa fa-envelope icon"></i>
<li style="cursor: no-drop" class="list-group-item">${docSnap.data().email}</li>`;
} else {
email.innerHTML = `<i class="fa fa-envelope icon"></i>
<input id="emailUpdate" class="input-field" type="email"
placeholder="Email" name="Email"></input>`;
}
if (docSnap.data().number) {
number.innerHTML = `<i class="fa-solid fa-phone icon"></i>
<li style="cursor: no-drop" class="list-group-item">${docSnap.data().number}</li>`;
} else {
number.innerHTML = `<i class="fa-solid fa-phone icon"></i>
<input id="numberUpdate" class="input-field" type="number"
placeholder="Number" name="Number"></input>`;
}
}
console.log(docSnap.data().photo);
document.getElementById('uploadIcon').addEventListener('click', function () {
document.getElementById('file').click();
});
const uploadImageToFirestore = (file) => {
return new Promise((resolve, reject) => {
const fileName = file.name
const storageRef = ref(storage, user.uid);
const uploadTask = uploadBytesResumable(storageRef, file);
const loaderImage = document.getElementById("loaderImage")
uploadTask.on('state_changed',
(snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + Math.round(progress) + '% done');
if (progress) {
loaderImage.style.display = "block"
}
switch (snapshot.state) {
case 'paused':
console.log('Upload is paused');
break;
case 'running':
console.log('Upload is running');
break;
}
},
(error) => {
reject(error)
Swal.fire({
icon: "error",
text: error,
});
},
() => {
getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
resolve(downloadURL);
});
}
);
});
}
const uploadFile = async () => {
const file = document.getElementById("file");
const url = await uploadImageToFirestore(file.files[0])
const uploadIcon = document.getElementById("uploadIcon");
uploadIcon.src = url
console.log("uploadIcon", uploadIcon, "file", file);
const userRef = doc(db, "users", user.uid);
if (docSnap.data().photo === "" || docSnap.data().photo === null || docSnap.data().photo !== null) {
await updateDoc(userRef, {
photo: url,
});
loaderImage.style.display = "none"
} else {
await updateDoc(userRef, {
photo: docSnap.data().photo
});
};
console.log("Image Updated")
Swal.fire({
position: "top-end",
icon: "success",
title: "Image Updated",
showConfirmButton: false,
timer: 1500
});
if (location.pathname !== "/todo.html") {
setTimeout(() => {
window.location.href = "todo.html";
loader.style.display = "block"
mainContent.style.display = "none"
}, 1500);
}
};
const imageBtn = document.getElementById("imageBtn");
imageBtn.addEventListener("click", uploadFile);
let updateProfile = async () => {
const nameUpdate = document.getElementById("nameUpdate")
const emailUpdate = document.getElementById("emailUpdate")
const numberUpdate = document.getElementById("numberUpdate")
const userRef = doc(db, "users", user.uid);
if (docSnap.data().name === "" || docSnap.data().name === null) {
await updateDoc(userRef, {
name: nameUpdate.value
});
} else {
await updateDoc(userRef, {
name: docSnap.data().name
});
};
if (docSnap.data().email === "" || docSnap.data().email === null) {
await updateDoc(userRef, {
email: emailUpdate.value
});
} else {
await updateDoc(userRef, {
email: docSnap.data().email
});
};
if (docSnap.data().number === "" || docSnap.data().number === null) {
await updateDoc(userRef, {
number: numberUpdate.value
});
} else {
await updateDoc(userRef, {
number: docSnap.data().number
});
};
console.log("Profile Updated")
Swal.fire({
position: "top-end",
icon: "success",
title: "Profile Updated",
showConfirmButton: false,
timer: 1500
});
if (location.pathname !== "/todo.html") {
setTimeout(() => {
window.location.href = "todo.html";
loader.style.display = "block"
mainContent.style.display = "none"
}, 1500);
}
}
const profileBtn = document.getElementById("profileBtn");
profileBtn.addEventListener("click", updateProfile);
}
});