edit date renewed

This commit is contained in:
2026-03-25 17:40:57 -05:00
parent a9173acc8d
commit fe7a9f6dd4

View File

@@ -589,42 +589,73 @@ async function checkStripePayment(invoiceId) {
}
async function editSentDates(invoiceId) {
// Load current invoice data
const res = await fetch(`/api/invoices/${invoiceId}`);
const data = await res.json();
const invoice = data.invoice;
const sentDates = invoice.sent_dates || [];
const sentDates = (invoice.sent_dates || []).map(d => d.split('T')[0]);
// Build a simple prompt-based editor
let datesStr = sentDates.join('\n');
const input = prompt(
'Edit sent dates (one per line, YYYY-MM-DD format):\n\n' +
'Add a new line to add a date.\nRemove a line to delete a date.\nLeave empty to clear all.',
datesStr
);
if (input === null) return; // Cancelled
// Parse and validate
const newDates = input.trim()
? input.trim().split('\n').map(d => d.trim()).filter(d => d)
: [];
for (const d of newDates) {
if (!/^\d{4}-\d{2}-\d{2}$/.test(d)) {
alert(`Invalid date: "${d}"\nPlease use YYYY-MM-DD format.`);
return;
// Build modal
let modal = document.getElementById('sent-dates-modal');
if (!modal) {
modal = document.createElement('div');
modal.id = 'sent-dates-modal';
modal.className = 'fixed inset-0 bg-black bg-opacity-50 z-50 flex justify-center items-center';
document.body.appendChild(modal);
}
modal.innerHTML = `
<div class="bg-white rounded-lg shadow-2xl w-full max-w-sm mx-auto p-6">
<h3 class="text-lg font-bold text-gray-800 mb-4">Edit Sent Dates</h3>
<div id="sent-dates-list" class="space-y-2 mb-4"></div>
<button type="button" onclick="window.invoiceView._addSentDateRow()"
class="text-sm text-blue-600 hover:text-blue-800 mb-4">+ Add date</button>
<div class="flex justify-end space-x-3 pt-4 border-t">
<button onclick="document.getElementById('sent-dates-modal').classList.add('hidden')"
class="px-4 py-2 bg-gray-200 text-gray-700 rounded-md hover:bg-gray-300 text-sm">Cancel</button>
<button onclick="window.invoiceView._saveSentDates(${invoiceId})"
class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 text-sm font-semibold">Save</button>
</div>
</div>`;
const list = document.getElementById('sent-dates-list');
if (sentDates.length === 0) {
_addSentDateRow();
} else {
sentDates.forEach(d => _addSentDateRow(d));
}
modal.classList.remove('hidden');
}
function _addSentDateRow(value = '') {
const list = document.getElementById('sent-dates-list');
if (!list) return;
const row = document.createElement('div');
row.className = 'flex items-center gap-2';
row.innerHTML = `
<input type="date" value="${value}" class="sent-date-input flex-1 px-3 py-2 border border-gray-300 rounded-md text-sm focus:ring-blue-500 focus:border-blue-500">
<button type="button" onclick="this.parentElement.remove()" class="px-2 py-1 bg-red-100 text-red-600 rounded hover:bg-red-200 text-sm">×</button>`;
list.appendChild(row);
}
async function _saveSentDates(invoiceId) {
const inputs = document.querySelectorAll('#sent-dates-list .sent-date-input');
const dates = [];
for (const input of inputs) {
if (input.value) dates.push(input.value);
}
try {
const response = await fetch(`/api/invoices/${invoiceId}/sent-dates`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sent_dates: newDates })
body: JSON.stringify({ sent_dates: dates })
});
if (response.ok) {
document.getElementById('sent-dates-modal').classList.add('hidden');
loadInvoices();
} else {
const err = await response.json();
@@ -632,7 +663,7 @@ async function editSentDates(invoiceId) {
}
} catch (e) {
console.error('Error updating sent dates:', e);
alert('Network error updating sent dates.');
alert('Network error.');
}
}
// ============================================================
@@ -641,5 +672,5 @@ async function editSentDates(invoiceId) {
window.invoiceView = {
viewPDF, viewHTML, syncFromQBO, resetQbo, markPaid, setEmailStatus, edit, remove,
loadInvoices, renderInvoiceView, setStatus, checkStripePayment, editSentDates
loadInvoices, renderInvoiceView, setStatus, checkStripePayment, editSentDates ,_addSentDateRow, _saveSentDates
};