Before refactor

This commit is contained in:
2026-03-24 13:35:29 -04:00
parent bb6597ff76
commit eb2321ef40
10 changed files with 208 additions and 75 deletions
+67 -59
View File
@@ -3009,8 +3009,12 @@
}
// Circuit modal functions
function showCircuitModal() {
if (!currentLocalFlightId) return;
function showCircuitModal(localFlightId = null, arrivalId = null) {
if (!localFlightId && !arrivalId) return;
// Set the current IDs
currentLocalFlightId = localFlightId;
currentArrivalId = arrivalId;
// Set default timestamp to current time
const now = new Date();
@@ -3027,13 +3031,15 @@
function closeCircuitModal() {
document.getElementById('circuitModal').style.display = 'none';
document.getElementById('circuit-form').reset();
currentLocalFlightId = null;
currentArrivalId = null;
}
// Circuit form submission
document.getElementById('circuit-form').addEventListener('submit', async function(e) {
e.preventDefault();
if (!currentLocalFlightId || !accessToken) return;
if ((!currentLocalFlightId && !currentArrivalId) || !accessToken) return;
const circuitTimestampInput = document.getElementById('circuit-timestamp').value;
if (!circuitTimestampInput) {
@@ -3046,15 +3052,21 @@
const localDate = new Date(circuitTimestampInput);
const circuitTimestamp = localDate.toISOString();
const requestBody = {
circuit_timestamp: circuitTimestamp
};
if (currentLocalFlightId) {
requestBody.local_flight_id = currentLocalFlightId;
} else if (currentArrivalId) {
requestBody.arrival_id = currentArrivalId;
}
const response = await authenticatedFetch('/api/v1/circuits/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
local_flight_id: currentLocalFlightId,
circuit_timestamp: circuitTimestamp
})
body: JSON.stringify(requestBody)
});
if (!response.ok) {
@@ -4602,41 +4614,10 @@
}
}
// Update status from table for departures
async function updateDepartureStatusFromTable(departureId, status) {
if (!accessToken) return;
try {
const response = await fetch(`/api/v1/departures/${departureId}/status`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify({ status: status })
});
if (!response.ok) throw new Error('Failed to update status');
loadPPRs(); // Refresh display
showNotification(`Departure marked as ${status.toLowerCase()}`);
} catch (error) {
console.error('Error updating status:', error);
showNotification('Error updating departure status', true);
}
}
// Update status from table for booked-in arrivals
// Update status from table for arrivals
async function updateArrivalStatusFromTable(arrivalId, status) {
if (!accessToken) return;
// Show confirmation for cancel actions
if (status === 'CANCELLED') {
if (!confirm('Are you sure you want to cancel this arrival? This action cannot be easily undone.')) {
return;
}
}
try {
const response = await fetch(`/api/v1/arrivals/${arrivalId}/status`, {
method: 'PATCH',
@@ -4649,7 +4630,7 @@
if (!response.ok) throw new Error('Failed to update status');
loadArrivals(); // Refresh arrivals table
loadATCAircraft(); // Refresh display
showNotification(`Arrival marked as ${status.toLowerCase()}`);
} catch (error) {
console.error('Error updating status:', error);
@@ -5230,12 +5211,14 @@
try {
const response = await Promise.all([
authenticatedFetch('/api/v1/local-flights/?status=LOCAL&limit=1000'),
authenticatedFetch('/api/v1/departures/?status=LOCAL&limit=1000')
authenticatedFetch('/api/v1/departures/?status=LOCAL&limit=1000'),
authenticatedFetch('/api/v1/arrivals/?status=LOCAL&limit=1000')
]);
let locals = [];
if (response[0].ok) locals = (await response[0].json()).map(l => ({ ...l, isLocalFlight: true }));
if (response[1].ok) locals = locals.concat((await response[1].json()).map(d => ({ ...d, isDeparture: true })));
if (response[2].ok) locals = locals.concat((await response[2].json()).map(a => ({ ...a, isArrival: true })));
displayLocalAircraft(locals);
} catch (error) {
@@ -5264,9 +5247,9 @@
if (isDeparture) {
// Departure in LOCAL status - show QSY button
buttons = `<button class="status-btn" onclick="event.stopPropagation(); currentDepartureId = '${ac.id}'; showTimestampModal('DEPARTED', ${ac.id}, false, true)">QSY</button>`;
} else if (ac.isLocalFlight) {
// Local flight in LOCAL status - show REJOIN button
buttons = `<button class="status-btn" onclick="event.stopPropagation(); updateLocalFlightStatusFromTable('${ac.id}', 'CIRCUIT')">REJOIN</button>`;
} else if (ac.isLocalFlight || ac.isArrival) {
// Local flight or arrival in LOCAL status - show REJOIN button
buttons = `<button class="status-btn" onclick="event.stopPropagation(); ${ac.isArrival ? `updateArrivalStatusFromTable('${ac.id}', 'CIRCUIT')` : `updateLocalFlightStatusFromTable('${ac.id}', 'CIRCUIT')`}">REJOIN</button>`;
}
return `
@@ -5322,6 +5305,12 @@
const from = ac.in_from;
const eta = ac.eta;
let buttons = '';
if (ac.isArrival) {
// Arrival in BOOKED_IN status - show CONTACT button
buttons = `<button class="status-btn" onclick="event.stopPropagation(); updateArrivalStatusFromTable('${ac.id}', 'LOCAL')">CONTACT</button>`;
}
return `
<div class="aircraft-item" onclick="handleATCClick('${ac.id}', '${ac.isArrival ? 'arrival' : 'ppr'}')">
<div class="aircraft-info">
@@ -5329,7 +5318,7 @@
<div class="aircraft-details">${type} from ${from || '?'}</div>
<div class="aircraft-time">${eta ? formatTimeOnly(eta) : ''}</div>
</div>
<div class="aircraft-status status-inbound">IB</div>
${buttons ? `<div style="display: flex; gap: 0.5rem;">${buttons}</div>` : '<div class="aircraft-status status-inbound">IB</div>'}
</div>
`;
}).join('');
@@ -5340,12 +5329,14 @@
try {
const response = await Promise.all([
authenticatedFetch('/api/v1/local-flights/?status=DEPARTED&limit=1000&flight_type=CIRCUITS'),
authenticatedFetch('/api/v1/local-flights/?status=CIRCUIT&limit=1000')
authenticatedFetch('/api/v1/local-flights/?status=CIRCUIT&limit=1000'),
authenticatedFetch('/api/v1/arrivals/?status=CIRCUIT&limit=1000')
]);
let circuits = [];
if (response[0].ok) circuits = await response[0].json();
if (response[1].ok) circuits = circuits.concat(await response[1].json());
if (response[2].ok) circuits = circuits.concat((await response[2].json()).map(a => ({ ...a, isArrival: true })));
displayCircuitAircraft(circuits);
} catch (error) {
@@ -5364,20 +5355,37 @@
return;
}
container.innerHTML = aircraft.map(ac => `
<div class="aircraft-item" onclick="handleATCClick('${ac.id}', 'local')">
<div class="aircraft-info">
<div class="aircraft-reg">${ac.registration}</div>
<div class="aircraft-details">${ac.type}</div>
<div class="aircraft-time">${formatTimeOnly(ac.created_dt)}</div>
container.innerHTML = aircraft.map(ac => {
const isArrival = ac.isArrival;
const entityType = isArrival ? 'arrival' : 'local';
const updateFunction = isArrival ? 'updateArrivalStatusFromTable' : 'updateLocalFlightStatusFromTable';
const landFunction = isArrival ? `${updateFunction}('${ac.id}', 'LANDED')` : `showTimestampModal('LANDED', ${ac.id}, true)`;
let buttons = `
<button class="status-btn" onclick="event.stopPropagation(); ${updateFunction}('${ac.id}', 'LOCAL')">LOCAL</button>
`;
// Show T&G for both local flights and arrivals
const tgFunction = isArrival
? `currentArrivalId = '${ac.id}'; showCircuitModal(null, '${ac.id}')`
: `currentLocalFlightId = '${ac.id}'; showCircuitModal('${ac.id}')`;
buttons += `<button class="status-btn" onclick="event.stopPropagation(); ${tgFunction}">T&G</button>`;
buttons += `<button class="status-btn" onclick="event.stopPropagation(); ${landFunction}">LAND</button>`;
return `
<div class="aircraft-item" onclick="handleATCClick('${ac.id}', '${entityType}')">
<div class="aircraft-info">
<div class="aircraft-reg">${ac.registration}</div>
<div class="aircraft-details">${ac.type}</div>
<div class="aircraft-time">${formatTimeOnly(ac.created_dt)}</div>
</div>
<div style="display: flex; gap: 0.5rem;">
${buttons}
</div>
</div>
<div style="display: flex; gap: 0.5rem;">
<button class="status-btn" onclick="event.stopPropagation(); updateLocalFlightStatusFromTable('${ac.id}', 'LOCAL')">LOCAL</button>
<button class="status-btn" onclick="event.stopPropagation(); currentLocalFlightId = '${ac.id}'; showCircuitModal()">T&G</button>
<button class="status-btn" onclick="event.stopPropagation(); currentLocalFlightId = '${ac.id}'; showTimestampModal('LANDED', ${ac.id}, true)">LAND</button>
</div>
</div>
`).join('');
`;
}).join('');
}
// Load pending PPRs