Skip to content

Commit dd39734

Browse files
authored
Merge pull request #378 from Jatzz26/update_jatin
corrected the CV upload issue
2 parents 43bded4 + 941e9b2 commit dd39734

File tree

3 files changed

+94
-85
lines changed
  • jobsdoor360-website/src/main/ejs/main-files/myaccount/cv_upload
  • staticfiles/mainfiles

3 files changed

+94
-85
lines changed
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<title>Cv upload</title>
5-
<link
6-
rel="stylesheet"
7-
href="/staticfiles/mainfiles/myaccount/cv_upload/style.css"
8-
/>
4+
<title>CV Upload</title>
5+
<link rel="stylesheet" href="/staticfiles/mainfiles/myaccount/cv_upload/style.css">
96
<%- include('../../../partials/head.ejs') %>
10-
<link
11-
rel="stylesheet"
12-
type="text/css"
13-
href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css"
14-
/>
7+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">
158
</head>
169
<body>
1710
<%- include('../../../partials/navbar.ejs') %>
18-
<div
19-
class="d-flex justify-content-center align-items-center flex-column gap-4"
20-
style="margin-block: 100px"
21-
>
11+
<div class="d-flex justify-content-center align-items-center flex-column gap-4" style="margin-block: 100px">
2212
<h2>UPLOAD YOUR RESUME</h2>
23-
<form action="" class="d-flex flex-column justify-content-center gap-3">
13+
<form class="d-flex flex-column justify-content-center gap-3" onsubmit="return false;">
2414
<div class="d-flex justify-content-center flex-column">
2515
<input
2616
id="cv"
2717
type="file"
2818
class="form-control cv"
29-
accept="cv/*"
19+
accept=".pdf,.doc,.docx"
3020
required
3121
/>
3222
<span class="form-text"></span>
@@ -43,13 +33,9 @@
4333
</form>
4434
</div>
4535
<%- include('../../../partials/footer.ejs') %>
46-
<script
47-
type="module"
48-
src="/staticfiles/mainfiles/myaccount/cv_upload/script.js"
49-
></script>
50-
<script
51-
type="text/javascript"
52-
src="https://cdn.jsdelivr.net/npm/toastify-js"
53-
></script>
36+
37+
<!-- Firebase -->
38+
<script type="module" src="/staticfiles/mainfiles/myaccount/cv_upload/script.js"></script>
39+
<script src="https://cdn.jsdelivr.net/npm/toastify-js"></script>
5440
</body>
5541
</html>
Lines changed: 83 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,95 @@
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)}`;
1+
import {
2+
getAuth,
3+
onAuthStateChanged
4+
} from "firebase/auth";
105

11-
}
12-
});
6+
import {
7+
getFirestore,
8+
doc,
9+
getDoc,
10+
setDoc,
11+
updateDoc
12+
} from "firebase/firestore";
1313

14-
const email = localStorage.getItem("email");
14+
import {
15+
getStorage,
16+
ref,
17+
uploadBytes,
18+
getDownloadURL
19+
} from "firebase/storage";
1520

16-
let cvUrl = "";
21+
const auth = getAuth();
22+
const db = getFirestore();
23+
const storage = getStorage();
1724

18-
async function uploadCV(file) {
19-
const cvRef = ref(storage, "user_cv/" + file.name);
20-
await uploadBytes(cvRef, file);
25+
let userEmail = null;
26+
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+
});
2136

22-
const url = await getDownloadURL(cvRef);
23-
return url; // Return empty string if URL is undefined
37+
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);
2442
}
2543

2644
async function saveCVToDatabase() {
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-
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();
6652
uploadButton.innerHTML = "UPLOAD RESUME";
6753
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;
6893
}
6994

70-
$("#btn").on("click", function () {
71-
saveCVToDatabase();
72-
});
95+
document.getElementById("btn").addEventListener("click", saveCVToDatabase);

staticfiles/mainfiles/signup/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ signupForm.addEventListener("submit", async (e) => {
4848
await sendEmailVerification(auth.currentUser);
4949

5050
window.location.href = "/resend_email_verification/";
51-
} catch (error) {
51+
} catch (error) {.
5252
alert("Error signing up: " + error.message);
5353
} finally {
5454
signupButton.innerHTML = "Sign Up";

0 commit comments

Comments
 (0)