|
1 | | -import { |
2 | | - getAuth, |
3 | | - onAuthStateChanged |
4 | | -} from "firebase/auth"; |
| 1 | +let userEmail; |
| 2 | +// Check if the user is signed in |
| 3 | +auth.onAuthStateChanged((user) => { |
| 4 | + if (user) { |
| 5 | + userEmail = user.email; |
| 6 | + } else { |
| 7 | + // No user is signed in |
| 8 | + console.log("No user is signed in"); |
| 9 | + window.location.href = `/login/?redirect_url=${encodeURIComponent(window.location.href)}`; |
5 | 10 |
|
6 | | -import { |
7 | | - getFirestore, |
8 | | - doc, |
9 | | - getDoc, |
10 | | - setDoc, |
11 | | - updateDoc |
12 | | -} from "firebase/firestore"; |
13 | | - |
14 | | -import { |
15 | | - getStorage, |
16 | | - ref, |
17 | | - uploadBytes, |
18 | | - getDownloadURL |
19 | | -} from "firebase/storage"; |
20 | | - |
21 | | -const auth = getAuth(); |
22 | | -const db = getFirestore(); |
23 | | -const storage = getStorage(); |
| 11 | + } |
| 12 | +}); |
24 | 13 |
|
25 | | -let userEmail = null; |
| 14 | +const email = localStorage.getItem("email"); |
26 | 15 |
|
27 | | -// Auth check |
28 | | -onAuthStateChanged(auth, (user) => { |
29 | | - if (user) { |
30 | | - userEmail = user.email; |
31 | | - } else { |
32 | | - console.log("No user is signed in"); |
33 | | - window.location.href = `/login/?redirect_url=${encodeURIComponent(window.location.href)}`; |
34 | | - } |
35 | | -}); |
| 16 | +let cvUrl = ""; |
36 | 17 |
|
37 | 18 | 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); |
| 19 | + const cvRef = ref(storage, "user_cv/" + file.name); |
| 20 | + await uploadBytes(cvRef, file); |
| 21 | + |
| 22 | + const url = await getDownloadURL(cvRef); |
| 23 | + return url; // Return empty string if URL is undefined |
42 | 24 | } |
43 | 25 |
|
44 | 26 | async function saveCVToDatabase() { |
45 | | - const uploadButton = document.getElementById("btn"); |
46 | | - uploadButton.innerHTML = "Uploading..."; |
47 | | - uploadButton.disabled = true; |
48 | | - |
49 | | - const cvFile = document.getElementById("cv").files[0]; |
50 | | - if (!cvFile) { |
51 | | - Toastify({ text: "Please select a file", duration: 3000, style: { background: "red" } }).showToast(); |
| 27 | + const uploadButton = document.getElementById("btn"); |
| 28 | + uploadButton.innerHTML = "Uploading..."; |
| 29 | + uploadButton.disabled = true; |
| 30 | + const cvFile = document.getElementById("cv").files[0]; |
| 31 | + if (cvFile) { |
| 32 | + cvUrl = await uploadCV(cvFile); |
| 33 | + |
| 34 | + const userProfileRef = doc(db, "user_profile", userEmail); |
| 35 | + |
| 36 | + // Ensure the user profile document exists before updating it |
| 37 | + const docSnap = await getDoc(userProfileRef); |
| 38 | + if (!docSnap.exists()) { |
| 39 | + await setDoc(userProfileRef, { "about": {} }); |
| 40 | + } |
| 41 | + |
| 42 | + await updateDoc(userProfileRef, { "about.cv": cvUrl }) |
| 43 | + .then(() => { |
| 44 | + Toastify({ |
| 45 | + text: "CV Successfully Submitted", |
| 46 | + duration: 3000, |
| 47 | + newWindow: true, |
| 48 | + close: true, |
| 49 | + gravity: "top", |
| 50 | + position: "right", |
| 51 | + stopOnFocus: true, |
| 52 | + style: { |
| 53 | + background: "linear-gradient(to right, #0d6efd, #586ba6)", |
| 54 | + borderRadius: "10px" |
| 55 | + } |
| 56 | + }).showToast(); |
| 57 | + setTimeout(() => { |
| 58 | + window.location.href = "/myaccount/"; |
| 59 | + }, 3000); |
| 60 | + }) |
| 61 | + .catch((error) => { |
| 62 | + console.error("Error writing document: ", error); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
52 | 66 | uploadButton.innerHTML = "UPLOAD RESUME"; |
53 | 67 | uploadButton.disabled = false; |
54 | | - return; |
55 | | - } |
56 | | - |
57 | | - try { |
58 | | - const cvUrl = await uploadCV(cvFile); |
59 | | - const userProfileRef = doc(db, "user_profile", userEmail); |
60 | | - |
61 | | - const docSnap = await getDoc(userProfileRef); |
62 | | - if (!docSnap.exists()) { |
63 | | - await setDoc(userProfileRef, { about: {} }); |
64 | | - } |
65 | | - |
66 | | - await updateDoc(userProfileRef, { |
67 | | - "about.cv": cvUrl |
68 | | - }); |
69 | | - |
70 | | - Toastify({ |
71 | | - text: "CV Successfully Submitted", |
72 | | - duration: 3000, |
73 | | - close: true, |
74 | | - gravity: "top", |
75 | | - position: "right", |
76 | | - style: { |
77 | | - background: "linear-gradient(to right, #0d6efd, #586ba6)", |
78 | | - borderRadius: "10px" |
79 | | - } |
80 | | - }).showToast(); |
81 | | - |
82 | | - setTimeout(() => { |
83 | | - window.location.href = "/myaccount/"; |
84 | | - }, 3000); |
85 | | - |
86 | | - } catch (error) { |
87 | | - console.error("Upload error:", error); |
88 | | - Toastify({ text: "Upload failed", duration: 3000, style: { background: "red" } }).showToast(); |
89 | | - } |
90 | | - |
91 | | - uploadButton.innerHTML = "UPLOAD RESUME"; |
92 | | - uploadButton.disabled = false; |
93 | 68 | } |
94 | 69 |
|
95 | | -document.getElementById("btn").addEventListener("click", saveCVToDatabase); |
| 70 | +$("#btn").on("click", function () { |
| 71 | + saveCVToDatabase(); |
| 72 | +}); |
0 commit comments