# Django Unicorn Refactoring Plan ## Overview This document outlines the comprehensive refactoring of ThrillWiki's Django templates from HTMX-based interactivity to Django Unicorn reactive components. The refactoring maintains the existing design and theme while providing modern reactive functionality. ## Completed Work ### 1. Django Unicorn Installation and Configuration ✅ **Installed Django Unicorn**: Added `django-unicorn==0.62.0` to project dependencies ✅ **Updated Django Settings**: Added `django_unicorn` to `INSTALLED_APPS` ✅ **URL Configuration**: Added Django Unicorn URLs to main URL configuration ✅ **Base Template Updates**: - Added `{% load unicorn %}` template tag - Included `{% unicorn_scripts %}` in head section - Added `{% csrf_token %}` to body as required by Django Unicorn ### 2. Park Search Component (Completed) ✅ **Created Component**: `backend/apps/parks/components/park_search.py` - Reactive search with debounced input (300ms delay) - Real-time filtering by name, location, operator - View mode switching (grid/list) - Pagination with navigation controls - Loading states and result counts ✅ **Created Template**: `backend/apps/parks/templates/unicorn/park-search.html` - Maintains existing TailwindCSS design - Responsive grid and list layouts - Interactive search with clear functionality - Pagination controls - Loading indicators ✅ **Refactored Park List Page**: `backend/templates/parks/park_list.html` - Replaced HTMX functionality with single Unicorn component - Simplified from 100+ lines to 10 lines - Maintains all existing functionality ## Refactoring Strategy ### Component Architecture Django Unicorn components follow this structure: ``` backend/apps/{app}/components/{component_name}.py # Python logic backend/apps/{app}/templates/unicorn/{component-name}.html # Template ``` ### Key Principles 1. **Design Preservation**: All existing TailwindCSS classes and design patterns are maintained 2. **Functionality Enhancement**: Replace HTMX with more powerful reactive capabilities 3. **Performance Optimization**: Debounced inputs, efficient database queries, pagination 4. **Code Simplification**: Reduce template complexity while maintaining functionality 5. **Progressive Enhancement**: Components work without JavaScript, enhanced with it ### Component Patterns #### 1. Search Components - Debounced search input (`unicorn:model.debounce-300`) - Real-time filtering and results updates - Loading states and result counts - Clear search functionality #### 2. Form Components - Two-way data binding with `unicorn:model` - Form validation with Django forms integration - Submit handling with `unicorn:click` or `unicorn:submit` - Error display and success messages #### 3. List/Grid Components - Dynamic view mode switching - Pagination with reactive controls - Sorting and filtering capabilities - Empty state handling #### 4. Modal Components - Dynamic content loading - Form submission within modals - Close/cancel functionality - Overlay management ## Planned Refactoring Phases ### Phase 1: Core Search and Listing Components (In Progress) #### Parks Domain - ✅ Park Search Component (Completed) - 🔄 Park Detail Component (Next) - 📋 Park Form Component - 📋 Park Photo Management Component #### Rides Domain - 📋 Ride Search Component - 📋 Ride Detail Component - 📋 Ride Form Component - 📋 Ride Photo Management Component ### Phase 2: Interactive Forms and Modals #### Authentication Components - 📋 Login Modal Component - 📋 Registration Modal Component - 📋 Password Reset Component - 📋 Profile Settings Component #### Content Management - 📋 Photo Upload Component - 📋 Review Form Component - 📋 Rating Component - 📋 Comment System Component ### Phase 3: Advanced Interactive Features #### Search and Filtering - 📋 Advanced Search Component - 📋 Filter Sidebar Component - 📋 Location Autocomplete Component - 📋 Tag Selection Component #### Maps and Visualization - 📋 Interactive Map Component - 📋 Location Picker Component - 📋 Route Planner Component - 📋 Statistics Dashboard Component ### Phase 4: Admin and Moderation #### Moderation Interface - 📋 Submission Review Component - 📋 Photo Approval Component - 📋 User Management Component - 📋 Content Moderation Component ## Component Examples ### Basic Component Structure ```python # backend/apps/parks/components/example.py from django_unicorn.components import UnicornView from apps.parks.models import Park class ExampleView(UnicornView): # Component state search_query: str = "" results = Park.objects.none() is_loading: bool = False def mount(self): """Initialize component""" self.load_data() def updated_search_query(self, query): """Called when search_query changes""" self.is_loading = True self.load_data() self.is_loading = False def load_data(self): """Load data based on current state""" # Database queries here pass ``` ### Template Integration ```html {% load unicorn %} {% unicorn 'component-name' %} {% unicorn 'component-name' arg1='value' arg2=variable %} ``` ### Reactive Elements ```html