/**
* Manage Submission Modal
* Handles submission tracking for blog posts to external publications
*/
// Create the modal HTML structure dynamically
function createManageSubmissionModal() {
const modal = document.createElement('div');
modal.id = 'manage-submission-modal';
modal.className = 'fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden';
modal.innerHTML = `
Submission Package Progress
0%
`;
return modal;
}
// Open modal and load data
async function openManageSubmissionModal(articleId, submissionId) {
currentSubmissionArticleId = articleId;
currentSubmissionId = submissionId;
// Create modal if it doesn't exist
let modal = document.getElementById('manage-submission-modal');
if (!modal) {
modal = createManageSubmissionModal();
document.getElementById('modal-container').appendChild(modal);
// Add close handler
document.getElementById('close-submission-modal').addEventListener('click', closeManageSubmissionModal);
// Load publication targets
await loadPublicationTargets();
// Add publication change handler
document.getElementById('publication-select').addEventListener('change', onPublicationChange);
}
// Load article and submission data
await loadSubmissionData(articleId);
// Show modal
modal.classList.remove('hidden');
}
// Close modal
function closeManageSubmissionModal() {
const modal = document.getElementById('manage-submission-modal');
if (modal) {
modal.classList.add('hidden');
clearSubmissionForm();
}
currentSubmissionArticleId = null;
currentSubmissionId = null;
currentArticleData = null;
}
// Load publication targets into dropdown
async function loadPublicationTargets() {
try {
const response = await fetch('/api/publications');
const data = await response.json();
const select = document.getElementById('publication-select');
if (!select) return; // Modal not created yet
const publications = data.data || [];
publications.forEach(pub => {
const option = document.createElement('option');
option.value = pub.id;
option.textContent = `${pub.name} (${pub.type})`;
option.dataset.requirements = JSON.stringify(pub.requirements);
option.dataset.submission = JSON.stringify(pub.submission);
select.appendChild(option);
});
} catch (error) {
console.error('Failed to load publication targets:', error);
}
}
// Handle publication selection change
function onPublicationChange(e) {
const select = e.target;
const selectedOption = select.options[select.selectedIndex];
const requirementsDiv = document.getElementById('publication-requirements');
const requirementsContent = document.getElementById('requirements-content');
if (!selectedOption.value) {
requirementsDiv.classList.add('hidden');
return;
}
const requirements = JSON.parse(selectedOption.dataset.requirements || '{}');
const submission = JSON.parse(selectedOption.dataset.submission || '{}');
let html = '';
if (requirements.wordCount) {
html += `- Word Count: ${requirements.wordCount.min}-${requirements.wordCount.max} words
`;
}
if (requirements.exclusivity) {
html += '- Exclusivity: Must not be published elsewhere
';
}
if (submission.method) {
html += `- Method: ${submission.method}`;
if (submission.email) {
html += ` (${submission.email})`;
}
html += '
';
}
if (submission.responseTime) {
html += `- Response Time: ${submission.responseTime.min}-${submission.responseTime.max} ${submission.responseTime.unit}
`;
}
html += '
';
requirementsContent.innerHTML = html;
requirementsDiv.classList.remove('hidden');
}
// Load submission data
async function loadSubmissionData(articleId) {
try {
// Load article data
const articleResponse = await fetch(`/api/blog/${articleId}`);
const articleData = await articleResponse.json();
currentArticleData = articleData;
// Populate article info
document.getElementById('article-title').textContent = articleData.title;
document.getElementById('article-excerpt').textContent = articleData.excerpt || '';
document.getElementById('article-word-count').textContent = articleData.wordCount || 0;
// Load existing submission if any
const submissionResponse = await fetch(`/api/blog/${articleId}/submissions`);
if (submissionResponse.ok) {
const submissionData = await submissionResponse.json();
if (submissionData.submissions && submissionData.submissions.length > 0) {
const submission = submissionData.submissions[0];
currentSubmissionId = submission._id;
// Populate publication select
if (submission.publicationId) {
document.getElementById('publication-select').value = submission.publicationId;
onPublicationChange({ target: document.getElementById('publication-select') });
}
// Populate checklist items
const fields = ['coverLetter', 'pitchEmail', 'notesToEditor', 'authorBio'];
fields.forEach(field => {
const packageData = submission.submissionPackage?.[field];
if (packageData) {
document.getElementById(`check-${field}`).checked = packageData.completed || false;
document.getElementById(`content-${field}`).value = packageData.content || '';
}
});
updateProgress();
}
}
} catch (error) {
console.error('Failed to load submission data:', error);
}
}
// Clear submission form
function clearSubmissionForm() {
document.getElementById('publication-select').value = '';
document.getElementById('publication-requirements').classList.add('hidden');
const fields = ['coverLetter', 'pitchEmail', 'notesToEditor', 'authorBio'];
fields.forEach(field => {
document.getElementById(`check-${field}`).checked = false;
document.getElementById(`content-${field}`).value = '';
});
updateProgress();
}
// Save checklist item
async function saveChecklistItem(field) {
if (!currentSubmissionArticleId) return;
const publicationId = document.getElementById('publication-select').value;
if (!publicationId) {
alert('Please select a publication first');
return;
}
const completed = document.getElementById(`check-${field}`).checked;
const content = document.getElementById(`content-${field}`).value;
try {
const response = await fetch(`/api/blog/${currentSubmissionArticleId}/submissions`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
publicationId,
submissionPackage: {
[field]: {
completed,
content,
lastUpdated: new Date().toISOString()
}
}
})
});
if (response.ok) {
const data = await response.json();
currentSubmissionId = data.submission._id;
// Show saved indicator
const savedIndicator = document.getElementById(`saved-${field}`);
savedIndicator.classList.remove('hidden');
setTimeout(() => {
savedIndicator.classList.add('hidden');
}, 2000);
updateProgress();
} else {
console.error('Failed to save checklist item');
}
} catch (error) {
console.error('Error saving checklist item:', error);
}
}
// Update progress indicator
function updateProgress() {
const fields = ['coverLetter', 'pitchEmail', 'notesToEditor', 'authorBio'];
let completed = 0;
fields.forEach(field => {
if (document.getElementById(`check-${field}`).checked) {
completed++;
}
});
const percentage = Math.round((completed / fields.length) * 100);
document.getElementById('progress-percentage').textContent = `${percentage}%`;
document.getElementById('progress-bar').style.width = `${percentage}%`;
}