Repository navigation
Expand file tree
/
Copy pathprofile.js
More file actions
89 lines (79 loc) · 3.34 KB
/
Copy pathprofile.js
File metadata and controls
89 lines (79 loc) · 3.34 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
document.addEventListener('DOMContentLoaded', function() {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
document.getElementById('username').value = user.displayName || user.email;
document.getElementById('email').value = user.email;
loadProfile(user.uid);
} else {
window.location.href = 'login.html';
}
});
});
function loadProfile(uid) {
const db = firebase.firestore();
const profileRef = db.collection('profiles').doc(uid);
profileRef.get().then(doc => {
if (doc.exists) {
const profile = doc.data();
document.getElementById('username').value = profile.username || '';
CKEDITOR.instances.editor.setData(profile.profileContent || '');
document.getElementById('profileImagePreview').src = profile.profileImageUrl || 'default-profile.png';
updateVehiclePreviews(profile.vehicleImages || []);
} else {
console.log('No profile data found');
}
}).catch(error => console.error('Error loading profile: ', error));
}
function saveProfile() {
const user = firebase.auth().currentUser;
if (user) {
const uid = user.uid;
const username = document.getElementById('username').value;
const profileContent = CKEDITOR.instances.editor.getData();
const profileImage = document.getElementById('profileImage').files[0];
const vehicleImages = Array.from(document.getElementById('vehicleImages').files);
const db = firebase.firestore();
const storage = firebase.storage();
const profileRef = db.collection('profiles').doc(uid);
let profileImageUrl = '';
let vehicleImageUrls = [];
const profileImagePromise = profileImage ? uploadFile(storage, `profile-images/${uid}/${profileImage.name}`, profileImage) : Promise.resolve('');
const vehicleImagePromises = vehicleImages.map(file => uploadFile(storage, `vehicle-images/${uid}/${file.name}`, file));
profileImagePromise.then(url => {
profileImageUrl = url;
return Promise.all(vehicleImagePromises);
}).then(urls => {
vehicleImageUrls = urls;
return profileRef.set({
username,
email: user.email,
profileContent,
profileImageUrl,
vehicleImages: vehicleImageUrls
});
}).then(() => {
alert('Profile saved successfully');
updatePreview();
}).catch(error => console.error('Error saving profile: ', error));
}
}
function uploadFile(storage, path, file) {
const ref = storage.ref().child(path);
return ref.put(file).then(snapshot => snapshot.ref.getDownloadURL());
}
function updateVehiclePreviews(vehicleImages) {
const vehicleContainer = document.getElementById('vehiclePreviews');
vehicleContainer.innerHTML = '';
vehicleImages.forEach(url => {
const img = document.createElement('img');
img.src = url;
img.alt = 'Vehicle Image';
img.style.maxWidth = '100px';
img.style.maxHeight = '100px';
vehicleContainer.appendChild(img);
});
}
function updatePreview() {
const profileContent = CKEDITOR.instances.editor.getData();
document.getElementById('profile-preview').innerHTML = profileContent;
}