06/18 Fix inspection start has Areas list if any
This commit is contained in:
@@ -383,6 +383,16 @@ def start():
|
|||||||
if not form.facility_id.choices:
|
if not form.facility_id.choices:
|
||||||
form.facility_id.choices = [('', '— no facilities —')]
|
form.facility_id.choices = [('', '— no facilities —')]
|
||||||
|
|
||||||
|
# Seed area choices for the currently selected facility
|
||||||
|
selected_facility_id = form.facility_id.data if form.is_submitted() else (
|
||||||
|
_session.get('reinspect_facility_id') or (facilities[0].id if facilities else None)
|
||||||
|
)
|
||||||
|
if selected_facility_id:
|
||||||
|
areas = Area.query.filter_by(facility_id=selected_facility_id).order_by(Area.name).all()
|
||||||
|
else:
|
||||||
|
areas = []
|
||||||
|
form.area_id.choices = [(0, '— No specific area —')] + [(a.id, a.name) for a in areas]
|
||||||
|
|
||||||
if not form.is_submitted():
|
if not form.is_submitted():
|
||||||
if _session.get('reinspect_template_id'):
|
if _session.get('reinspect_template_id'):
|
||||||
form.template_id.data = _session['reinspect_template_id']
|
form.template_id.data = _session['reinspect_template_id']
|
||||||
@@ -409,10 +419,11 @@ def start():
|
|||||||
|
|
||||||
from flask import session as _session
|
from flask import session as _session
|
||||||
parent_id = _session.pop('reinspect_parent_id', None)
|
parent_id = _session.pop('reinspect_parent_id', None)
|
||||||
|
chosen_area_id = form.area_id.data if form.area_id.data and form.area_id.data != 0 else None
|
||||||
inspection = Inspection(
|
inspection = Inspection(
|
||||||
template_id = template.id,
|
template_id = template.id,
|
||||||
facility_id = form.facility_id.data,
|
facility_id = form.facility_id.data,
|
||||||
area_id = None,
|
area_id = chosen_area_id,
|
||||||
inspector_id = current_user.id,
|
inspector_id = current_user.id,
|
||||||
inspection_date = now_eastern(),
|
inspection_date = now_eastern(),
|
||||||
status = 'in_progress',
|
status = 'in_progress',
|
||||||
|
|||||||
@@ -35,6 +35,11 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3" id="areaGroup" style="display:none;">
|
||||||
|
{{ form.area_id.label(class="form-label fw-semibold") }}
|
||||||
|
{{ form.area_id(class="form-select", id="areaSelect") }}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="d-grid">
|
<div class="d-grid">
|
||||||
<button type="submit" class="btn btn-primary btn-lg">
|
<button type="submit" class="btn btn-primary btn-lg">
|
||||||
<i class="bi bi-play-fill"></i> Begin Inspection
|
<i class="bi bi-play-fill"></i> Begin Inspection
|
||||||
@@ -52,14 +57,49 @@
|
|||||||
const facilitySel = document.getElementById('facilitySelect');
|
const facilitySel = document.getElementById('facilitySelect');
|
||||||
const spinner = document.getElementById('facilitySpinner');
|
const spinner = document.getElementById('facilitySpinner');
|
||||||
const emptyMsg = document.getElementById('facilityEmpty');
|
const emptyMsg = document.getElementById('facilityEmpty');
|
||||||
|
const areaGroup = document.getElementById('areaGroup');
|
||||||
|
const areaSel = document.getElementById('areaSelect');
|
||||||
|
|
||||||
function loadFacilities(projectId, selectedFacilityId) {
|
const FACILITIES_URL = `{{ url_for('inspections.facilities_for_project', project_id=0) }}`.replace('/0', '/');
|
||||||
|
const AREAS_URL = `{{ url_for('inspections.areas_for_facility', facility_id=0) }}`.replace('/0', '/');
|
||||||
|
|
||||||
|
function loadAreas(facilityId, selectedAreaId) {
|
||||||
|
if (!facilityId) {
|
||||||
|
areaGroup.style.display = 'none';
|
||||||
|
areaSel.innerHTML = '<option value="0">— No specific area —</option>';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fetch(AREAS_URL + facilityId)
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.length === 0) {
|
||||||
|
areaGroup.style.display = 'none';
|
||||||
|
areaSel.innerHTML = '<option value="0">— No specific area —</option>';
|
||||||
|
} else {
|
||||||
|
areaSel.innerHTML = '<option value="0">— No specific area —</option>';
|
||||||
|
data.forEach(a => {
|
||||||
|
const opt = document.createElement('option');
|
||||||
|
opt.value = a.id;
|
||||||
|
opt.textContent = a.name;
|
||||||
|
if (selectedAreaId && a.id === selectedAreaId) opt.selected = true;
|
||||||
|
areaSel.appendChild(opt);
|
||||||
|
});
|
||||||
|
areaGroup.style.display = '';
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
areaGroup.style.display = 'none';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadFacilities(projectId, selectedFacilityId, selectedAreaId) {
|
||||||
if (!projectId) return;
|
if (!projectId) return;
|
||||||
spinner.classList.remove('d-none');
|
spinner.classList.remove('d-none');
|
||||||
emptyMsg.classList.add('d-none');
|
emptyMsg.classList.add('d-none');
|
||||||
facilitySel.disabled = true;
|
facilitySel.disabled = true;
|
||||||
|
areaGroup.style.display = 'none';
|
||||||
|
|
||||||
fetch(`{{ url_for('inspections.facilities_for_project', project_id=0) }}`.replace('/0', '/' + projectId))
|
fetch(FACILITIES_URL + projectId)
|
||||||
.then(r => r.json())
|
.then(r => r.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
facilitySel.innerHTML = '';
|
facilitySel.innerHTML = '';
|
||||||
@@ -74,6 +114,7 @@
|
|||||||
if (selectedFacilityId && f.id === selectedFacilityId) opt.selected = true;
|
if (selectedFacilityId && f.id === selectedFacilityId) opt.selected = true;
|
||||||
facilitySel.appendChild(opt);
|
facilitySel.appendChild(opt);
|
||||||
});
|
});
|
||||||
|
loadAreas(facilitySel.value, selectedAreaId);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
@@ -86,18 +127,25 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
projectSel.addEventListener('change', function () {
|
projectSel.addEventListener('change', function () {
|
||||||
loadFacilities(this.value, null);
|
loadFacilities(this.value, null, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
facilitySel.addEventListener('change', function () {
|
||||||
|
loadAreas(this.value, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
// On page load: if project already selected (e.g. validation error or reinspect),
|
// On page load: if project already selected (e.g. validation error or reinspect),
|
||||||
// reload facility list preserving the currently selected facility value.
|
// reload facility list preserving the currently selected facility and area values.
|
||||||
const initProject = projectSel.value;
|
const initProject = projectSel.value;
|
||||||
const initFacility = facilitySel.value ? parseInt(facilitySel.value, 10) : null;
|
const initFacility = facilitySel.value ? parseInt(facilitySel.value, 10) : null;
|
||||||
|
const initArea = areaSel.value ? parseInt(areaSel.value, 10) : null;
|
||||||
if (initProject) {
|
if (initProject) {
|
||||||
// Only reload if facility list looks like a placeholder (no real numeric id in options)
|
|
||||||
const hasRealOptions = Array.from(facilitySel.options).some(o => parseInt(o.value, 10) > 0);
|
const hasRealOptions = Array.from(facilitySel.options).some(o => parseInt(o.value, 10) > 0);
|
||||||
if (!hasRealOptions) {
|
if (!hasRealOptions) {
|
||||||
loadFacilities(initProject, initFacility);
|
loadFacilities(initProject, initFacility, initArea);
|
||||||
|
} else {
|
||||||
|
// Facilities already rendered server-side — just load areas for the selected facility
|
||||||
|
if (initFacility) loadAreas(initFacility, initArea);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -144,6 +144,7 @@ class StartInspectionForm(FlaskForm):
|
|||||||
template_id = SelectField('Template', coerce=int, validators=[DataRequired()])
|
template_id = SelectField('Template', coerce=int, validators=[DataRequired()])
|
||||||
project_id = SelectField('Contract', coerce=int, validators=[DataRequired()])
|
project_id = SelectField('Contract', coerce=int, validators=[DataRequired()])
|
||||||
facility_id = SelectField('Facility', coerce=int, validators=[DataRequired()])
|
facility_id = SelectField('Facility', coerce=int, validators=[DataRequired()])
|
||||||
|
area_id = SelectField('Area', coerce=int, validators=[Optional()])
|
||||||
|
|
||||||
|
|
||||||
class ChecklistResultForm(FlaskForm):
|
class ChecklistResultForm(FlaskForm):
|
||||||
|
|||||||
Reference in New Issue
Block a user