Skip to content

Commit 85a5370

Browse files
authored
Merge pull request #379 from Jatzz26/update_jatin
corrected the CV upload issue
2 parents dd39734 + 04e3006 commit 85a5370

File tree

1 file changed

+25
-20
lines changed
  • staticfiles/mainfiles/myaccount/cv_upload

1 file changed

+25
-20
lines changed

staticfiles/mainfiles/myaccount/cv_upload/script.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,56 @@ import {
1818
getDownloadURL
1919
} from "firebase/storage";
2020

21+
// Firebase initialization (you must already have this in your main project setup)
2122
const auth = getAuth();
2223
const db = getFirestore();
2324
const storage = getStorage();
2425

2526
let userEmail = null;
2627

27-
// Auth check
28+
// ✅ AUTH CHECK
2829
onAuthStateChanged(auth, (user) => {
2930
if (user) {
3031
userEmail = user.email;
3132
} else {
3233
console.log("No user is signed in");
34+
// Redirect to login with return URL (FULL URL for better redirect)
3335
window.location.href = `/login/?redirect_url=${encodeURIComponent(window.location.href)}`;
3436
}
3537
});
3638

39+
// ✅ UPLOAD CV TO STORAGE
3740
async function uploadCV(file) {
38-
const fileName = `${userEmail.replace(/[^a-zA-Z0-9]/g, "_")}_${file.name}`;
39-
const cvRef = ref(storage, "user_cv/" + fileName);
40-
await uploadBytes(cvRef, file);
41-
return await getDownloadURL(cvRef);
41+
const sanitizedFileName = file.name.replace(/[^a-zA-Z0-9_.-]/g, "_");
42+
const storageRef = ref(storage, `user_cv/${userEmail}_${sanitizedFileName}`);
43+
await uploadBytes(storageRef, file);
44+
return await getDownloadURL(storageRef);
4245
}
4346

47+
// ✅ SAVE CV URL TO FIRESTORE
4448
async function saveCVToDatabase() {
4549
const uploadButton = document.getElementById("btn");
46-
uploadButton.innerHTML = "Uploading...";
47-
uploadButton.disabled = true;
50+
const fileInput = document.getElementById("cv");
51+
const file = fileInput.files[0];
4852

49-
const cvFile = document.getElementById("cv").files[0];
50-
if (!cvFile) {
53+
if (!file) {
5154
Toastify({ text: "Please select a file", duration: 3000, style: { background: "red" } }).showToast();
52-
uploadButton.innerHTML = "UPLOAD RESUME";
53-
uploadButton.disabled = false;
5455
return;
5556
}
5657

58+
uploadButton.disabled = true;
59+
uploadButton.innerText = "Uploading...";
60+
5761
try {
58-
const cvUrl = await uploadCV(cvFile);
59-
const userProfileRef = doc(db, "user_profile", userEmail);
62+
const cvUrl = await uploadCV(file);
63+
const userRef = doc(db, "user_profile", userEmail);
6064

61-
const docSnap = await getDoc(userProfileRef);
62-
if (!docSnap.exists()) {
63-
await setDoc(userProfileRef, { about: {} });
65+
const userSnap = await getDoc(userRef);
66+
if (!userSnap.exists()) {
67+
await setDoc(userRef, { about: {} });
6468
}
6569

66-
await updateDoc(userProfileRef, {
70+
await updateDoc(userRef, {
6771
"about.cv": cvUrl
6872
});
6973

@@ -83,13 +87,14 @@ async function saveCVToDatabase() {
8387
window.location.href = "/myaccount/";
8488
}, 3000);
8589

86-
} catch (error) {
87-
console.error("Upload error:", error);
90+
} catch (err) {
91+
console.error("Upload failed:", err);
8892
Toastify({ text: "Upload failed", duration: 3000, style: { background: "red" } }).showToast();
8993
}
9094

91-
uploadButton.innerHTML = "UPLOAD RESUME";
9295
uploadButton.disabled = false;
96+
uploadButton.innerText = "UPLOAD RESUME";
9397
}
9498

99+
// ✅ HANDLE BUTTON CLICK
95100
document.getElementById("btn").addEventListener("click", saveCVToDatabase);

0 commit comments

Comments
 (0)