4.9 KiB
Public Complaint Form - Domain Dropdown Fix
Issue Summary
The domain dropdown in the public complaint form was empty on page load, preventing users from selecting a complaint category.
Root Cause Analysis
1. JavaScript Initialization Problem
The loadDomains() function was only called when the hospital dropdown changed:
$('#id_hospital').on('change', function() {
loadDomains(hospitalId);
});
This meant domains were never loaded when the form first rendered.
2. API Endpoint Behavior
The api_load_categories() endpoint had this logic:
if hospital_id:
categories_queryset = ComplaintCategory.objects.filter(
Q(hospitals__id=hospital_id) | Q(hospitals__isnull=True),
is_active=True
)
else:
categories_queryset = ComplaintCategory.objects.filter(
hospitals__isnull=True,
is_active=True
)
While this was correct, the JavaScript never called the API without a hospital ID initially.
3. Category Data Status
All 106 categories in the database are system-wide (no hospital assigned):
- 3 Domains (Level 1): CLINICAL, MANAGEMENT, RELATIONSHIPS
- 8 Categories (Level 2)
- Many Subcategories (Level 3)
- Many Classifications (Level 4)
Solution Implemented
1. Updated API Endpoint (apps/complaints/ui_views.py)
Added clarifying comment to the api_load_categories() function:
Updated: Always returns system-wide categories even without hospital_id,
to support initial form loading.
2. Modified JavaScript (templates/complaints/public_complaint_form.html)
Change 1: Simplified loadDomains() function
function loadDomains(hospitalId) {
// Always load system-wide categories, even without a hospital
$.ajax({
url: '{% url "complaints:api_load_categories" %}',
type: 'GET',
data: { hospital_id: hospitalId || '' },
success: function(response) {
// ... populate dropdown
}
});
}
Key change: Removed the early return when no hospital is selected. Now always loads system-wide categories.
Change 2: Added page load initialization
// Load domains on page initialization
$(document).ready(function() {
loadDomains(''); // Load system-wide categories on page load
});
Key change: Added document ready handler to load domains immediately when the page loads.
Technical Details
Data Flow
- Page loads →
$(document).ready()triggers loadDomains('')calls API with empty hospital_id- API returns all system-wide categories (all 106 categories)
- JavaScript populates domain dropdown with Level 1 categories (3 domains)
- User selects domain →
loadCategories()populates Level 2 - User selects category →
loadSubcategories()populates Level 3 - User selects subcategory →
loadClassifications()populates Level 4 (optional)
Why This Works
- All categories are system-wide (not hospital-specific)
- The API returns the same data whether a hospital is selected or not
- Loading on page initialization provides immediate user feedback
- Hospital selection still triggers a refresh to support future hospital-specific categories
Files Modified
-
apps/complaints/ui_views.py
- Updated
api_load_categories()function with clarifying comment
- Updated
-
templates/complaints/public_complaint_form.html
- Modified
loadDomains()to always load categories - Added
$(document).ready()handler to load domains on page load
- Modified
Testing Recommendations
-
Basic Functionality
- Open the public complaint form
- Verify domain dropdown is populated with 3 options (Clinical, Management, Relationships)
- Select a domain and verify category dropdown appears with options
- Select a category and verify subcategory dropdown appears
- Verify classification dropdown appears if applicable
-
Hospital Selection
- Select a hospital
- Verify domain dropdown remains populated
- Verify cascading dropdowns still work
-
Form Submission
- Fill out all required fields
- Submit the form
- Verify complaint is created with correct taxonomy levels
-
Arabic Support
- Switch to Arabic
- Verify dropdown labels appear in Arabic
- Verify descriptions appear in Arabic (if available)
Future Considerations
If hospital-specific categories are added in the future:
- The current implementation will still work
- When a hospital is selected, hospital-specific categories will be shown first
- System-wide categories will always be available as fallback
- The dropdown will refresh on hospital selection to show the correct categories
Related Files
apps/complaints/models.py- ComplaintCategory model with 4-level hierarchyapps/complaints/management/commands/load_shct_taxonomy.py- SHCT taxonomy loaderCOMPLAINT_TAXONOMY_EXAMINATION.md- Taxonomy structure documentationcheck_hierarchy.py- Script to verify category hierarchy