-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetting.js
More file actions
86 lines (78 loc) · 2.99 KB
/
setting.js
File metadata and controls
86 lines (78 loc) · 2.99 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
const changeAvatarBtn = document.getElementById("change-avatar-btn");
const viewAvatar = document.getElementById("view-avatar");
const db = firebase.database();
const auth = firebase.auth();
async function fileToDataURL(file, maxWidth = 512, maxHeight = 512, outputType = "image/png", quality = 0.92) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onerror = () => reject(new Error("Failed reading file"));
reader.onload = () => {
const img = new Image();
img.onload = () => {
const ratio = Math.min(maxWidth / img.naturalWidth, maxHeight / img.naturalHeight, 1);
const w = Math.round(img.naturalWidth * ratio);
const h = Math.round(img.naturalHeight * ratio);
const canvas = document.createElement("canvas");
canvas.width = w;
canvas.height = h;
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, w, h);
resolve(canvas.toDataURL(outputType, quality));
};
img.onerror = () => reject(new Error("Gambar tidak valid"));
img.src = reader.result;
};
reader.readAsDataURL(file);
});
}
async function setAvatar(user, school, dataUrl) {
if (!user || !school || !dataUrl) throw new Error("Data tidak ditemukan");
const ref = db.ref(`UserAvatar/${school}/${user}`);
await ref.set(dataUrl);
return dataUrl;
}
function updateAvatarUI(dataUrl) {
if (!viewAvatar) return;
viewAvatar.style.backgroundImage = `url("${dataUrl}")`;
viewAvatar.style.backgroundSize = "cover";
viewAvatar.style.backgroundPosition = "center";
viewAvatar.style.backgroundRepeat = "no-repeat";
}
async function chooseAndUploadAvatar(school, user) {
const input = document.createElement("input");
input.type = "file";
input.accept = "image/*";
input.click();
input.onchange = async () => {
if (!input.files || input.files.length === 0) return;
const file = input.files[0];
try {
const dataUrl = await fileToDataURL(file, 512, 512, "image/png", 0.92);
await setAvatar(user, school, dataUrl);
updateAvatarUI(dataUrl);
alert("Avatar telah diupdate!");
} catch (err) {
console.error(err);
alert("Gagal memperbarui avatar. Silakan coba gambar lain.");
}
};
}
auth.onAuthStateChanged((user) => {
if (!user) {
console.log("Sepertinya kamu belum masuk. Arahkan ke halaman login.");
location.href = "index.html";
return;
}
const username = (user.displayName || user.email.split("@")[0]).toLowerCase();
const school = localStorage.getItem("school") || "tonggalan";
const cleanedUsername = username.charAt(0).toUpperCase() + username.slice(1);
db.ref(`UserAvatar/${school}/${username}`).once("value")
.then(snapshot => {
const val = snapshot.val();
if (typeof val === "string" && val.trim() !== "") {
updateAvatarUI(val);
}
})
.catch(err => console.warn("Could not load avatar:", err));
changeAvatarBtn.addEventListener("click", () => chooseAndUploadAvatar(school, username));
});