-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.html
More file actions
180 lines (169 loc) · 9.27 KB
/
Copy pathprofile.html
File metadata and controls
180 lines (169 loc) · 9.27 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Public Profile</title>
<link rel="stylesheet" href="styles.css">
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.2.1/firebase-storage.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Sortable/1.14.0/Sortable.min.js"></script>
<script src="config.js"></script>
</head>
<body>
<nav class="navbar">
<div class="navbar-container">
<span class="navbar-toggle" id="js-navbar-toggle">
<i class="fas fa-bars"></i>
</span>
<a href="index.html" class="logo">iMOB Racing</a>
<ul class="main-nav" id="js-menu">
<li><a href="index.html" class="nav-links">Home</a></li>
<li><a href="drivers.html" class="nav-links">Drivers</a></li>
<li><a href="cars.html" class="nav-links">Cars</a></li>
<li><a href="mission.html" class="nav-links">Our Mission</a></li>
<li><a href="index.html#contact" class="nav-links">Contact</a></li>
<li><a href="all_profiles.html" class="nav-links">All Profiles</a></li>
<li><a href="profile.html" class="nav-links">Profile</a></li>
<li><a href="javascript:void(0)" class="nav-links" id="logout">Logout</a></li>
</ul>
</div>
</nav>
<h1>Public Profile</h1>
<div id="profile-container" class="profile-container"></div>
<input type="file" id="upload-image-input" style="display:none;">
<script>
if (!firebase.apps.length) {
firebase.initializeApp(CONFIG.FIREBASE_CONFIG);
}
document.addEventListener('DOMContentLoaded', () => {
const urlParams = new URLSearchParams(window.location.search);
const userId = urlParams.get('userId');
if (userId) {
loadProfile(userId);
} else {
document.getElementById('profile-container').innerText = 'No user ID provided in URL.';
}
});
async function loadProfile(userId) {
try {
const db = firebase.firestore();
const doc = await db.collection('profiles').doc(userId).get();
if (doc.exists) {
const data = doc.data();
data.vehicleImageUrls = data.vehicleImageUrls || [];
const container = document.getElementById('profile-container');
container.innerHTML = `
<h2 contenteditable="true" onblur="saveField('username', this.innerText)">${data.username || 'No username provided'}</h2>
<div class="profile-image-container">
<img src="${data.profileImageUrl || 'default-profile.png'}" alt="Profile Image" class="thumbnail" id="profile-image">
<button class="delete-image" onclick="deleteImage('profileImageUrl')">X</button>
</div>
<div contenteditable="true" onblur="saveField('bio', this.innerHTML)">${data.bio || 'No bio provided'}</div>
<div id="vehicle-previews" class="sortable">
${(data.vehicleImageUrls || []).map(url => `
<div class="vehicle-image-container">
<img src="${url}" alt="Vehicle Image" class="thumbnail">
<button class="delete-vehicle-image" onclick="deleteVehicleImage('${url}')">X</button>
</div>
`).join('')}
</div>
<div id="social-media-previews">
${(data.socialMediaUrls || []).map(url => `
<div class="social-media-url-container">
<a href="${url}" target="_blank" contenteditable="true" onblur="saveSocialMediaUrl('${url}', this.innerText)">${url}</a>
<button class="delete-social-media-url" onclick="deleteSocialMediaUrl('${url}')">X</button>
</div>
`).join('<br>')}
</div>
<div contenteditable="true" onblur="saveField('customHtmlContent', this.innerHTML)">${data.customHtmlContent || 'No additional content provided'}</div>
`;
new Sortable(document.getElementById('vehicle-previews'), {
animation: 150,
onEnd: async (evt) => {
const newOrder = [...document.querySelectorAll('#vehicle-previews .vehicle-image-container')].map((item) => item.querySelector('img').src);
await db.collection('profiles').doc(userId).update({ vehicleImageUrls: newOrder });
}
});
container.addEventListener('dblclick', () => {
const input = document.getElementById('upload-image-input');
input.click();
input.onchange = async (event) => {
const file = event.target.files[0];
if (file) {
const storageRef = firebase.storage().ref();
const fileRef = storageRef.child(`vehicle-images/${userId}/${file.name}`);
await fileRef.put(file);
const url = await fileRef.getDownloadURL();
data.vehicleImageUrls.push(url);
await db.collection('profiles').doc(userId).update({ vehicleImageUrls: data.vehicleImageUrls });
loadProfile(userId);
}
};
});
} else {
document.getElementById('profile-container').innerText = 'Profile not found.';
}
} catch (error) {
console.error('Error loading profile: ', error);
document.getElementById('profile-container').innerText = 'Error loading profile.';
}
}
async function saveField(field, value) {
const userId = new URLSearchParams(window.location.search).get('userId');
const db = firebase.firestore();
await db.collection('profiles').doc(userId).update({ [field]: value });
}
async function saveSocialMediaUrl(oldUrl, newUrl) {
const userId = new URLSearchParams(window.location.search).get('userId');
const db = firebase.firestore();
const doc = await db.collection('profiles').doc(userId).get();
if (doc.exists) {
const data = doc.data();
const updatedUrls = data.socialMediaUrls.map(url => (url === oldUrl ? newUrl : url));
await db.collection('profiles').doc(userId).update({ socialMediaUrls: updatedUrls });
}
}
async function deleteImage(imageField) {
const userId = new URLSearchParams(window.location.search).get('userId');
const db = firebase.firestore();
await db.collection('profiles').doc(userId).update({ [imageField]: firebase.firestore.FieldValue.delete() });
loadProfile(userId);
}
async function deleteVehicleImage(url) {
const userId = new URLSearchParams(window.location.search).get('userId');
const db = firebase.firestore();
const doc = await db.collection('profiles').doc(userId).get();
if (doc.exists) {
const data = doc.data();
const updatedUrls = data.vehicleImageUrls.filter(imgUrl => imgUrl !== url);
await db.collection('profiles').doc(userId).update({ vehicleImageUrls: updatedUrls });
loadProfile(userId);
}
}
async function deleteSocialMediaUrl(url) {
const userId = new URLSearchParams(window.location.search).get('userId');
const db = firebase.firestore();
const doc = await db.collection('profiles').doc(userId).get();
if (doc.exists) {
const data = doc.data();
const updatedUrls = data.socialMediaUrls.filter(smUrl => smUrl !== url);
await db.collection('profiles').doc(userId).update({ socialMediaUrls: updatedUrls });
loadProfile(userId);
}
}
function logout() {
firebase.auth().signOut().then(() => {
window.location.href = 'index.html';
}).catch((error) => {
console.error('Error signing out: ', error);
});
}
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('logout').addEventListener('click', logout);
});
</script>
</body>
</html>