-
Notifications
You must be signed in to change notification settings - Fork 156
/
profile.js
220 lines (187 loc) · 7.73 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
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
// Function to handle user sign-in
function handleCredentialResponse(response) {
const data = jwt_decode(response.credential);
const profileContainer = document.querySelector(".profile-container");
const profileName = document.querySelector(".profile-name");
const profileImage = document.querySelector(".profile-container img");
profileName.textContent = data.name;
profileImage.src = data.picture;
profileContainer.style.opacity = 1;
profileContainer.style.pointerEvents = "auto";
}
// Function to handle user sign-out
function signOut() {
const profileContainer = document.querySelector(".profile-container");
profileContainer.style.opacity = 0;
profileContainer.style.pointerEvents = "none";
}
// Add sign-out button event listener
document.querySelector(".sign-out-btn").addEventListener("click", signOut);
function handleCredentialResponse(response) {
const responsePayload = parseJwt(response.credential);
document.getElementById("user-name").innerText = `Hello, ${responsePayload.name}`;
document.getElementById("user-image").src = responsePayload.picture;
document.getElementById("user-image").style.display = "block";
document.getElementById("sign-out").style.display = "block";
// Save user profile in local storage
localStorage.setItem("userProfile", JSON.stringify(responsePayload));
// Load user progress
loadUserProgress();
}
function parseJwt(token) {
const base64Url = token.split(".")[1];
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
const jsonPayload = decodeURIComponent(
atob(base64)
.split("")
.map(function (c) {
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
})
.join("")
);
return JSON.parse(jsonPayload);
}
document.getElementById("sign-out").addEventListener("click", function () {
document.getElementById("user-name").innerText = "";
document.getElementById("user-image").style.display = "none";
document.getElementById("sign-out").style.display = "none";
localStorage.removeItem("userProfile");
localStorage.removeItem("userProgress");
location.reload();
});
function loadUserProfile() {
const userProfile = JSON.parse(localStorage.getItem("userProfile"));
if (userProfile) {
document.getElementById("user-name").innerText = `Hello, ${userProfile.name}`;
document.getElementById("user-image").src = userProfile.picture;
document.getElementById("user-image").style.display = "block";
document.getElementById("sign-out").style.display = "block";
// Load additional profile info
document.getElementById("user-email").innerText = `Email: ${userProfile.email}`;
document.getElementById("user-additional-info").innerText = `Info: ${userProfile.additionalInfo}`;
loadUserProgress();
}
}
function saveUserProgress(stats) {
localStorage.setItem("userProgress", JSON.stringify(stats));
}
function loadUserProgress() {
const userProgress = JSON.parse(localStorage.getItem("userProgress"));
if (userProgress) {
document.querySelector('.stats[name="cube-size"] b').innerText = userProgress.cubeSize;
document.querySelector('.stats[name="total-solves"] b').innerText = userProgress.totalSolves;
document.querySelector('.stats[name="best-time"] b').innerText = userProgress.bestTime;
document.querySelector('.stats[name="worst-time"] b').innerText = userProgress.worstTime;
document.querySelector('.stats[name="average-5"] b').innerText = userProgress.average5;
document.querySelector('.stats[name="average-12"] b').innerText = userProgress.average12;
document.querySelector('.stats[name="average-25"] b').innerText = userProgress.average25;
}
}
// Call loadUserProfile on page load
window. () {
loadUserProfile();
};
function changeProfilePic() {
// Implement functionality to change profile picture
alert("Change Profile Picture");
}
function editName() {
document.getElementById("profile-name").style.display = "none";
document.getElementById("name-input").style.display = "inline";
document.getElementById("save-name-btn").style.display = "inline";
document.getElementById("name-input").value = document.getElementById("profile-name").textContent;
}
function saveName() {
const newName = document.getElementById("name-input").value;
document.getElementById("profile-name").textContent = newName;
document.getElementById("profile-name").style.display = "inline";
document.getElementById("name-input").style.display = "none";
document.getElementById("save-name-btn").style.display = "none";
// Optionally, save the new name to local storage or server
}
document.getElementById("sign-out-btn").addEventListener("click", function () {
// Sign out logic here
console.log("User signed out");
// Hide profile elements
document.getElementById("profile-container").style.opacity = 0;
document.getElementById("profile-container").style.pointerEvents = "none";
// Clear user data from localStorage or state
});
function toggleProfileOptions() {
const options = document.getElementById("profile-options");
options.classList.toggle("hidden"); // Toggle visibility
}
function editProfileName() {
const newName = prompt("Enter new name:");
if (newName) {
document.getElementById("user-name").textContent = newName;
// Update this in your local storage or server as needed
}
}
function viewProgress() {
alert("Viewing progress..."); // Replace with actual progress viewing logic
}
// Update profile info
function updateProfile(event) {
event.preventDefault();
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const additionalInfo = document.getElementById("additional-info").value.trim();
const profilePic = document.getElementById("profile-pic").files[0];
if (name === "" || email === "" || additionalInfo === "") {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Please complete all fields before submitting.",
customClass: {
popup: "swal-popup",
title: "swal-title",
content: "swal-content",
confirmButton: "swal-confirm-button",
},
});
return;
}
if (!profilePic) {
Swal.fire({
icon: "error",
title: "Oops...",
text: "Please select a profile picture before submitting.",
customClass: {
popup: "swal-popup",
title: "swal-title",
content: "swal-content",
confirmButton: "swal-confirm-button",
},
});
return;
}
const reader = new FileReader();
reader. (e) {
const userProfile = {
name: name,
email: email,
additionalInfo: additionalInfo,
picture: e.target.result,
};
localStorage.setItem("userProfile", JSON.stringify(userProfile));
Swal.fire({
icon: "success",
title: "Success!",
text: "Your profile has been updated.",
customClass: {
popup: "swal-popup",
title: "swal-title",
content: "swal-content",
confirmButton: "swal-confirm-button",
},
}).then((result) => {
if (result.isConfirmed || result.isDismissed) {
document.getElementById("edit-profile").reset();
document.getElementById("main-profile-pic").src = userProfile.picture;
}
});
};
reader.readAsDataURL(profilePic);
}
document.getElementById("edit-profile").addEventListener("submit", updateProfile);