HH/PUBLIC_FORM_DOMAIN_DROPDOWN_FIX.md

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

  1. Page loads → $(document).ready() triggers
  2. loadDomains('') calls API with empty hospital_id
  3. API returns all system-wide categories (all 106 categories)
  4. JavaScript populates domain dropdown with Level 1 categories (3 domains)
  5. User selects domain → loadCategories() populates Level 2
  6. User selects category → loadSubcategories() populates Level 3
  7. 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

  1. apps/complaints/ui_views.py

    • Updated api_load_categories() function with clarifying comment
  2. templates/complaints/public_complaint_form.html

    • Modified loadDomains() to always load categories
    • Added $(document).ready() handler to load domains on page load

Testing Recommendations

  1. 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
  2. Hospital Selection

    • Select a hospital
    • Verify domain dropdown remains populated
    • Verify cascading dropdowns still work
  3. Form Submission

    • Fill out all required fields
    • Submit the form
    • Verify complaint is created with correct taxonomy levels
  4. 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:

  1. The current implementation will still work
  2. When a hospital is selected, hospital-specific categories will be shown first
  3. System-wide categories will always be available as fallback
  4. The dropdown will refresh on hospital selection to show the correct categories
  • apps/complaints/models.py - ComplaintCategory model with 4-level hierarchy
  • apps/complaints/management/commands/load_shct_taxonomy.py - SHCT taxonomy loader
  • COMPLAINT_TAXONOMY_EXAMINATION.md - Taxonomy structure documentation
  • check_hierarchy.py - Script to verify category hierarchy