Skip to content

Commit aba3f1d

Browse files
authored
Update apply.js
1 parent a695659 commit aba3f1d

1 file changed

Lines changed: 61 additions & 35 deletions

File tree

apply.js

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,79 @@ document.addEventListener("DOMContentLoaded", async () => {
88
const messageBox = document.getElementById("messageBox");
99
const jobTitleEl = document.getElementById("jobTitle");
1010

11+
// Get job_id from the URL (e.g., apply.html?job_id=123)
1112
const jobId = new URLSearchParams(window.location.search).get("job_id");
1213

13-
if (!jobId) {
14-
messageBox.textContent = "❌ Invalid job link.";
14+
function showMessage(text, type) {
15+
messageBox.textContent = text;
16+
messageBox.className = "message " + type;
1517
messageBox.style.display = "block";
18+
}
19+
20+
if (!jobId) {
21+
showMessage("❌ Invalid job link. Please return to the job board.", "error");
1622
return;
1723
}
1824

19-
// Fetch job title for display
20-
const { data: job } = await supabase.from("jobs").select("title").eq("id", jobId).single();
21-
if (job) jobTitleEl.textContent = "Designation: " + job.title;
25+
// 1. Fetch Job Title for the header
26+
const { data: job, error: jobError } = await supabase
27+
.from("jobs")
28+
.select("title")
29+
.eq("id", jobId)
30+
.single();
2231

32+
if (job) {
33+
jobTitleEl.textContent = "Role: " + job.title;
34+
}
35+
36+
// 2. Handle Form Submission
2337
form.addEventListener("submit", async (e) => {
2438
e.preventDefault();
25-
messageBox.textContent = "⏳ Submitting...";
26-
messageBox.style.display = "block";
39+
40+
const name = document.getElementById("name").value.trim();
41+
const email = document.getElementById("email").value.trim();
42+
const phone = document.getElementById("phone").value.trim();
43+
const resumeFile = document.getElementById("resume").files[0];
2744

28-
const resume = document.getElementById("resume").files[0];
29-
const filePath = `${jobId}/${Date.now()}_${resume.name}`;
45+
showMessage("⏳ Submitting application...", "info");
3046

31-
// 1. Upload to Storage - MUST match your dashboard name "RESUMES"
32-
const { error: uploadError } = await supabase.storage.from("RESUMES").upload(filePath, resume);
33-
34-
if (uploadError) {
35-
console.error("Upload Error:", uploadError);
36-
messageBox.textContent = "❌ Upload failed: " + uploadError.message;
37-
return;
38-
}
47+
try {
48+
// A. Upload Resume to Storage (Bucket name must be exact: RESUMES)
49+
const fileExt = resumeFile.name.split('.').pop();
50+
const fileName = `${Date.now()}_${name.replace(/\s+/g, '_')}.${fileExt}`;
51+
const filePath = `${jobId}/${fileName}`;
52+
53+
const { error: uploadError } = await supabase.storage
54+
.from("RESUMES")
55+
.upload(filePath, resumeFile);
56+
57+
if (uploadError) throw uploadError;
58+
59+
// B. Get the Public URL for the resume
60+
const { data: urlData } = supabase.storage
61+
.from("RESUMES")
62+
.getPublicUrl(filePath);
63+
64+
// C. Save Application to 'candidates' table
65+
const { error: insertError } = await supabase
66+
.from("candidates")
67+
.insert({
68+
full_name: name,
69+
email: email,
70+
phone: phone,
71+
resume_url: urlData.publicUrl,
72+
job_id: jobId
73+
});
74+
75+
if (insertError) throw insertError;
76+
77+
showMessage("✅ Application submitted successfully!", "success");
78+
form.reset();
79+
jobTitleEl.textContent = "";
3980

40-
// 2. Get Public URL
41-
const { data: urlData } = supabase.storage.from("RESUMES").getPublicUrl(filePath);
42-
43-
// 3. Insert to Candidates Table
44-
const { error: insertError } = await supabase.from("candidates").insert({
45-
full_name: document.getElementById("name").value,
46-
email: document.getElementById("email").value,
47-
phone: document.getElementById("phone").value,
48-
resume_url: urlData.publicUrl,
49-
job_id: jobId
50-
});
51-
52-
if (insertError) {
53-
console.error("DB Error:", insertError);
54-
messageBox.textContent = "❌ Database Error: " + insertError.message;
55-
} else {
56-
messageBox.textContent = "✅ Success! Application submitted.";
57-
form.reset();
81+
} catch (err) {
82+
console.error("Submission Error:", err);
83+
showMessage("❌ Error: " + (err.message || "Failed to submit"), "error");
5884
}
5985
});
6086
});

0 commit comments

Comments
 (0)