Unknown type supprt

This commit is contained in:
2026-03-23 12:47:08 -04:00
parent bddbe1451f
commit d2c9bc0370
7 changed files with 345 additions and 13 deletions
+137 -4
View File
@@ -163,15 +163,26 @@ function createLookup(fieldId, resultsId, selectCallback, options = {}) {
const resultsDiv = document.getElementById(resultsId);
if (config.isAircraft) {
// Aircraft lookup: auto-populate on single match, format input on no match
// Aircraft lookup: auto-populate on single match, mark form for auto-save on no match
if (!results || results.length === 0) {
// Format the aircraft registration and auto-populate
// Format the aircraft registration
const formatted = formatAircraftRegistration(searchTerm);
const field = document.getElementById(fieldId);
if (field) {
field.value = formatted;
// Mark the form for auto-saving this aircraft
const form = field.closest('form');
if (form) {
form.setAttribute('data-unsaved-aircraft', fieldId);
}
}
resultsDiv.innerHTML = ''; // Clear results, field is auto-populated
// Show message that type will be saved
resultsDiv.innerHTML = `
<div class="aircraft-no-match">
No match found - aircraft type will be saved automatically when you submit
</div>
`;
} else if (results.length === 1) {
// Single match - auto-populate
const aircraft = results[0];
@@ -183,7 +194,14 @@ function createLookup(fieldId, resultsId, selectCallback, options = {}) {
// Auto-populate the form fields
const field = document.getElementById(fieldId);
if (field) field.value = aircraft.registration;
if (field) {
field.value = aircraft.registration;
// Clear the unsaved aircraft flag since we found a match
const form = field.closest('form');
if (form) {
form.removeAttribute('data-unsaved-aircraft');
}
}
// Also populate type field
let typeFieldId;
@@ -208,6 +226,14 @@ function createLookup(fieldId, resultsId, selectCallback, options = {}) {
Multiple matches found (${results.length}) - please be more specific
</div>
`;
// Clear the unsaved aircraft flag since multiple matches found
const field = document.getElementById(fieldId);
if (field) {
const form = field.closest('form');
if (form) {
form.removeAttribute('data-unsaved-aircraft');
}
}
}
} else {
// Airport lookup: show list of options with keyboard navigation
@@ -501,3 +527,110 @@ function selectBookInAircraft(registration) {
function selectBookInArrivalAirport(icaoCode) {
lookupManager.selectItem('book-in-arrival-airport-lookup-results', 'book_in_from', icaoCode);
}
// Save user aircraft type for future lookups
async function saveUserAircraft(registrationFieldId, resultsDivId) {
const regField = document.getElementById(registrationFieldId);
if (!regField || !regField.value.trim()) {
showNotification('Please enter a registration first', true);
return;
}
// Determine the type field ID based on registration field
let typeFieldId;
if (registrationFieldId === 'ac_reg') {
typeFieldId = 'ac_type';
} else if (registrationFieldId === 'local_registration') {
typeFieldId = 'local_type';
} else if (registrationFieldId === 'book_in_registration') {
typeFieldId = 'book_in_type';
} else if (registrationFieldId === 'overflight_registration') {
typeFieldId = 'overflight_type';
}
const typeField = document.getElementById(typeFieldId);
if (!typeField || !typeField.value.trim()) {
showNotification('Please enter an aircraft type first', true);
return;
}
try {
const response = await authenticatedFetch('/api/v1/aircraft/user-aircraft', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
registration: regField.value.trim(),
type_code: typeField.value.trim()
})
});
if (response.ok) {
const data = await response.json();
showNotification('Aircraft type saved for future use');
// Clear the results div
const resultsDiv = document.getElementById(resultsDivId);
if (resultsDiv) {
resultsDiv.innerHTML = '';
}
} else {
const error = await response.json();
showNotification(error.detail || 'Failed to save aircraft', true);
}
} catch (error) {
console.error('Error saving aircraft:', error);
showNotification('Error saving aircraft', true);
}
}
// Auto-save unsaved aircraft before form submission
async function autoSaveUnsavedAircraft(form) {
const unsavedFieldId = form.getAttribute('data-unsaved-aircraft');
if (!unsavedFieldId) return; // No unsaved aircraft to save
const regField = document.getElementById(unsavedFieldId);
if (!regField || !regField.value.trim()) return;
// Determine the type field ID
let typeFieldId;
if (unsavedFieldId === 'ac_reg') {
typeFieldId = 'ac_type';
} else if (unsavedFieldId === 'local_registration') {
typeFieldId = 'local_type';
} else if (unsavedFieldId === 'book_in_registration') {
typeFieldId = 'book_in_type';
} else if (unsavedFieldId === 'overflight_registration') {
typeFieldId = 'overflight_type';
}
const typeField = document.getElementById(typeFieldId);
if (!typeField || !typeField.value.trim()) return;
try {
const response = await authenticatedFetch('/api/v1/aircraft/user-aircraft', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
registration: regField.value.trim(),
type_code: typeField.value.trim()
})
});
if (response.ok) {
// Successfully saved, remove the flag
form.removeAttribute('data-unsaved-aircraft');
console.log('Auto-saved aircraft type for', regField.value.trim());
} else if (response.status === 400) {
// Already exists, just remove the flag
form.removeAttribute('data-unsaved-aircraft');
} else {
console.error('Failed to auto-save aircraft');
}
} catch (error) {
console.error('Error auto-saving aircraft:', error);
}
}