2025-02-18 16:39:38 -05:00
|
|
|
const profileJsonTextarea = document.getElementById("profileJson");
|
|
|
|
|
|
|
|
// Function to validate JSON input live
|
|
|
|
function validateJsonInput() {
|
|
|
|
const errorText = document.getElementById("jsonError");
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Try parsing JSON
|
|
|
|
// TODO Update these styles to be correct tailwind classes for constancy
|
|
|
|
JSON.parse(profileJsonTextarea.value);
|
|
|
|
profileJsonTextarea.style.border = "2px solid green"; // Green border on valid input
|
|
|
|
errorText.style.display = "none";
|
|
|
|
} catch (e) {
|
|
|
|
profileJsonTextarea.style.border = "2px solid red"; // Red border on error
|
|
|
|
errorText.style.display = "block";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
profileJsonTextarea.addEventListener("input", validateJsonInput);
|
|
|
|
|
|
|
|
|
2025-02-13 15:56:52 -05:00
|
|
|
document.getElementById('uploadForm').addEventListener('submit', async function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
2025-02-17 15:00:17 -05:00
|
|
|
const resumePreviewSection = document.getElementById('resumePreviewSection');
|
|
|
|
const generateBtn = document.getElementById('generateCoverLetterBtn');
|
2025-02-18 16:22:27 -05:00
|
|
|
const profileJson = document.getElementById('profileJson');
|
2025-02-13 15:56:52 -05:00
|
|
|
|
|
|
|
|
2025-02-17 16:02:39 -05:00
|
|
|
|
2025-02-18 16:22:27 -05:00
|
|
|
generateBtn.disabled = true;
|
2025-02-17 16:54:26 -05:00
|
|
|
resumePreviewSection.style.display = "grid";
|
2025-02-18 16:22:27 -05:00
|
|
|
profileJson.textContent = "Parsing Resume....."
|
2025-02-14 16:02:34 -05:00
|
|
|
|
|
|
|
const fileInput = document.getElementById('resume');
|
|
|
|
const file = fileInput.files[0];
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
alert("Please upload a resume.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("resume", file);
|
|
|
|
|
2025-02-14 08:33:22 -05:00
|
|
|
try {
|
2025-02-17 15:00:17 -05:00
|
|
|
const response = await fetch('/generate/extract-resume', {
|
2025-02-14 16:02:34 -05:00
|
|
|
method: "POST",
|
|
|
|
body: formData,
|
|
|
|
});
|
2025-02-13 15:56:52 -05:00
|
|
|
|
2025-02-18 16:22:27 -05:00
|
|
|
if (!response.ok) {
|
|
|
|
const errorData = await response.json();
|
|
|
|
throw new Error(errorData.error || 'Failed to process the resume.');
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
generateBtn.disabled = false;
|
|
|
|
profileJson.textContent = JSON.stringify(data.candidateProfile, null, 2);
|
2025-02-14 08:33:22 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Error:', error);
|
|
|
|
alert('Something went wrong. Please try again.');
|
2025-02-17 16:02:39 -05:00
|
|
|
resumePreviewSection.style.display = "none";
|
2025-02-14 08:33:22 -05:00
|
|
|
}
|
|
|
|
|
2025-02-14 09:02:33 -05:00
|
|
|
});
|
|
|
|
|
2025-02-17 16:02:39 -05:00
|
|
|
// Send Resume and Job Description to Generate Cover Letter
|
2025-02-17 15:00:17 -05:00
|
|
|
document.getElementById("generateCoverLetterBtn").addEventListener("click", async function () {
|
2025-02-18 16:22:27 -05:00
|
|
|
const candidateProfile = document.getElementById("profileJson").value;
|
2025-02-17 15:00:17 -05:00
|
|
|
const jobDescription = document.getElementById("jobDescription").value;
|
2025-02-17 16:12:28 -05:00
|
|
|
const keyPoints = document.getElementById("keyPoints").value;
|
2025-02-17 16:02:39 -05:00
|
|
|
const generateBtn = document.getElementById("generateCoverLetterBtn")
|
2025-02-17 15:00:17 -05:00
|
|
|
|
2025-02-18 16:22:27 -05:00
|
|
|
if (!candidateProfile.trim()) {
|
2025-02-17 15:00:17 -05:00
|
|
|
alert("Please confirm the extracted resume text.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!jobDescription.trim()) {
|
|
|
|
alert("Please enter a job description.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const requestData = {
|
2025-02-18 16:22:27 -05:00
|
|
|
candidateProfile,
|
2025-02-17 15:00:17 -05:00
|
|
|
jobDescription,
|
2025-02-17 16:12:28 -05:00
|
|
|
keyPoints,
|
2025-02-17 15:00:17 -05:00
|
|
|
};
|
|
|
|
|
2025-02-17 16:02:39 -05:00
|
|
|
generateBtn.textContent = "Generating Cover Letter...."
|
|
|
|
|
2025-02-17 15:00:17 -05:00
|
|
|
try {
|
|
|
|
const response = await fetch("/generate", {
|
|
|
|
method: "POST",
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
body: JSON.stringify(requestData),
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.error) {
|
|
|
|
alert("Error: " + data.error);
|
|
|
|
} else {
|
2025-02-18 16:22:27 -05:00
|
|
|
const formattedText = formatCoverLetter(data.coverLetter);
|
|
|
|
document.getElementById("coverLetterOutput").value = formattedText;
|
2025-02-17 16:54:26 -05:00
|
|
|
document.getElementById("coverLetterSection").style.display = "grid"; // Show cover letter section
|
2025-02-20 09:19:20 -05:00
|
|
|
document.getElementById("tailoredResumeSection").style.display = "grid";
|
|
|
|
document.getElementById("tailoredResumeOutput").value = data.tailoredResumeText;
|
|
|
|
document.getElementById("keyResumeUpdates").textContent = data.resumeChanges;
|
2025-02-17 16:02:39 -05:00
|
|
|
generateBtn.textContent = "Generate New Cover Letter"
|
2025-02-17 15:00:17 -05:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error generating cover letter:", error);
|
|
|
|
alert("Something went wrong. Please try again.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2025-02-14 09:02:33 -05:00
|
|
|
|
2025-02-20 15:57:38 -05:00
|
|
|
async function downloadDocument(content, endpoint, filename) {
|
2025-02-14 09:02:33 -05:00
|
|
|
try {
|
2025-02-20 15:57:38 -05:00
|
|
|
const response = await fetch(endpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
body: JSON.stringify({ content })
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!response.ok) throw new Error('Failed to download document.');
|
|
|
|
|
|
|
|
// Convert response to blob
|
|
|
|
const blob = await response.blob();
|
|
|
|
const url = window.URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
// Create a temporary download link
|
|
|
|
const a = document.createElement('a');
|
|
|
|
a.href = url;
|
|
|
|
a.download = filename;
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
document.body.removeChild(a);
|
|
|
|
window.URL.revokeObjectURL(url);
|
2025-02-14 09:02:33 -05:00
|
|
|
} catch (error) {
|
2025-02-14 16:02:34 -05:00
|
|
|
console.error('Error:', error);
|
|
|
|
alert('Failed to download document.');
|
2025-02-14 09:02:33 -05:00
|
|
|
}
|
2025-02-20 15:57:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Event listener for cover letter download
|
|
|
|
document.getElementById('downloadCoverLetterBtn').addEventListener('click', function () {
|
|
|
|
const coverLetterText = document.getElementById('coverLetterOutput').value;
|
|
|
|
if (!coverLetterText.trim()) {
|
|
|
|
alert("Cover letter is empty!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
downloadDocument(coverLetterText, '/generate/download-cover-letter', 'cover_letter.docx');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Event listener for tailored resume download
|
|
|
|
document.getElementById('downloadResumeBtn').addEventListener('click', function () {
|
|
|
|
const tailoredResumeText = document.getElementById('tailoredResumeOutput').value;
|
|
|
|
if (!tailoredResumeText.trim()) {
|
|
|
|
alert("Tailored resume is empty!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
downloadDocument(tailoredResumeText, '/generate/download-resume', 'tailored_resume.docx');
|
2025-02-18 16:22:27 -05:00
|
|
|
});
|
|
|
|
|
2025-02-20 15:57:38 -05:00
|
|
|
// document.getElementById('downloadBtn').addEventListener('click', async function () {
|
|
|
|
// const coverLetterText = document.getElementById('coverLetterOutput').value;
|
|
|
|
|
|
|
|
// try {
|
|
|
|
// const response = await fetch('/generate/download', {
|
|
|
|
// method: 'POST',
|
|
|
|
// headers: { 'Content-Type': 'application/json' },
|
|
|
|
// body: JSON.stringify({ coverLetterText })
|
|
|
|
// });
|
|
|
|
|
|
|
|
// if (!response.ok) throw new Error('Failed to download document.');
|
|
|
|
|
|
|
|
// // Convert response to blob
|
|
|
|
// const blob = await response.blob();
|
|
|
|
// const url = window.URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
// // Create a temporary download link
|
|
|
|
// const a = document.createElement('a');
|
|
|
|
// a.href = url;
|
|
|
|
// a.download = 'cover_letter.docx';
|
|
|
|
// document.body.appendChild(a);
|
|
|
|
// a.click();
|
|
|
|
|
|
|
|
// // Cleanup
|
|
|
|
// document.body.removeChild(a);
|
|
|
|
// window.URL.revokeObjectURL(url);
|
|
|
|
// } catch (error) {
|
|
|
|
// console.error('Error:', error);
|
|
|
|
// alert('Failed to download document.');
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
|
2025-02-18 16:22:27 -05:00
|
|
|
|
|
|
|
function formatCoverLetter(rawText) {
|
|
|
|
return rawText
|
|
|
|
.replace(/<\/header>/g, '')
|
|
|
|
.replace(/<\/greeting>/g, '')
|
|
|
|
.replace(/<\/introduction>/g, '')
|
|
|
|
.replace(/<\/body>/g, '')
|
|
|
|
.replace(/<\/conclusion>/g, '')
|
|
|
|
.replace(/<\/signature>/g, '')
|
|
|
|
.replace(/<[^>]+>/g, ''); // Remove all XML-like tags
|
2025-02-18 16:39:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|