Table of Contents
- Overview
- Architecture Principles
- System Components
- State Management
- Data Flow
- Navigation System
- Step Handler Registry Pattern
- Entity Lifecycle Management
- Persistence Strategy
- Error Handling
- Performance Optimizations
- Key Design Patterns
- Extension Points
Overview
The Instructor Course Editor is a multi-step wizard for creating and editing courses. It implements a sophisticated state-driven architecture using Zustand for state management, with a step-based navigation system, optimistic UI updates, and robust error handling.Key Features
- 5-Step Wizard: Basic Info → Advanced Info → Curriculum → Pricing → Publish
- Optimistic Updates: UI responds immediately, syncs async with backend
- Persistence: Critical data persists across sessions
- Permission Control: Owner/manager authorization checks
- Unsaved Changes Protection: Prevents data loss on navigation
- Real-time Validation: Per-step validation with error tracking
- Entity Status Tracking: NEW → UPDATED → SYNCED → DELETED states
Technology Stack
- State Management: Zustand with slices pattern
- Forms: React Hook Form + Zod validation
- Data Fetching: React Query
- UI Framework: Chakra UI v3
- Navigation: React Router v6
- Animations: Framer Motion
Architecture Principles
1. Separation of Concerns
- Parent Component: Initial data fetch, routing, permission checks
- Step Components: Individual UI, validation, save logic
- Store Slices: Domain-specific state management
- Hooks: Reusable logic encapsulation
2. Single Source of Truth
- Zustand store is the canonical source for all editor state
- React Query for server state caching
- LocalStorage for session persistence
3. Loose Coupling
- Steps don’t know about each other
- Navigation logic separated from step logic
- Save handlers registered dynamically via registry pattern
4. Progressive Enhancement
- Works with partial data
- Handles network failures gracefully
- Supports continue-previous-work flow
5. Type Safety
- Full TypeScript coverage
- Zod schemas for runtime validation
- Interface contracts between layers
System Components
Component Hierarchy
Core Files
Pages (instructor/src/pages/editor/)
CourseEditor.tsx- Main wrapper with data fetchingBasicInformation.tsx- Course title, category, level, languageAdvanceInformation.tsx- Thumbnail, video, description, requirementsCurriculum.tsx- Course structure (sections/subsections/items)Pricing.tsx- Pricing configurationPublishCourse.tsx- Final review and submission
Store Slices (instructor/src/stores/course-editor/)
index.ts- Main store creator with persistencestate.ts- Initial state definitiontypes.ts- Initial values for each stepnavigation.slice.ts- Step navigation logicform.slice.ts- Form state managementcurriculum.slice.ts- Course structure operationssync.slice.ts- Backend synchronizationstep.slice.ts- Step completion trackingstep-handler-registry.ts- Handler registry implementation
Hooks (instructor/src/hooks/)
useEditorNavigation.ts- Enhanced navigation with validationuseCourseEditorInitialization.ts- Editor initialization logicuseStepSubmission.ts- Step handler registration
State Management
Zustand Store Structure
Slice Pattern
The store is composed of 5 specialized slices:1. Navigation Slice
- Step index management
- Accessibility validation
- Mode switching (CREATE → EDIT)
- Step completion checking
2. Form Slice
- Field-level updates
- Real-time validation
- Form state tracking
3. Curriculum Slice
- Hierarchical structure management
- Optimistic UI updates with temp IDs
- Order management with automatic reindexing
- Cascade deletion
4. Sync Slice
- Initial data hydration
- Temp ID → Real ID replacement
- Entity status updates (SYNCED/ERROR)
- Error tracking and recovery
5. Step Slice
- Step state lifecycle
- Completion tracking
- Unsaved changes detection
- Step accessibility calculation
Data Flow
1. Initial Load (Edit Mode)
2. Step Save Flow
3. Curriculum Entity Creation Flow
4. Curriculum Entity Update Flow
5. Curriculum Entity Deletion Flow
Navigation System
Navigation Hook (useEditorNavigation)
The useEditorNavigation hook is the central navigation controller for the editor.
Core Methods
Navigation Rules
URL Synchronization
Unsaved Changes Protection
Step States
Each step tracks its own state:Visual Feedback
Steps are visually styled based on their state:Step Handler Registry Pattern
Overview
The Step Handler Registry decouples navigation logic from step-specific business logic. Each step registers a handler that implements a common interface, allowing the navigation system to call step-specific functions without knowing implementation details.Handler Interface
Registry Implementation
Step Registration Hook
Usage in Step Components
Benefits
- Decoupling: Navigation doesn’t know about step internals
- Testability: Each handler can be tested independently
- Flexibility: Easy to add new steps without modifying navigation
- Type Safety: Common interface ensures consistency
- Reusability: Handler logic can be shared across steps
Entity Lifecycle Management
Entity Status Enum
Entity Structure
Lifecycle Transitions
Update Flow
Delete Flow
Temporary IDs
Error Recovery
Persistence Strategy
LocalStorage Persistence
Only critical data is persisted to avoid bloating localStorage:sections,subsections,items(curriculum)stepStates(recomputed on mount)navigationState(derived)optimisticUpdates(transient)- UI state flags
- Curriculum can be large and is fetched from server
- Step states are derived from validated data
- Reduces localStorage quota usage
- Prevents stale curriculum data
Continue Work Modal
When the editor mounts:Error Handling
Multi-Layer Error Boundaries
1. Application Level
2. Step Level
- React Hook Form validation errors
- Zod schema validation
- Displayed inline near fields
3. API Level
4. Navigation Guards
Retry Logic
Permission Errors
Performance Optimizations
1. Memoization
2. Shallow Equality Checking
3. Debounced Validation
4. Conditional Queries
5. Optimistic UI Updates
6. Selective Re-renders
7. Animation Performance
Key Design Patterns
1. Command Pattern (Step Handlers)
- Encapsulates save/discard operations as objects
- Allows queuing, undo, and retry
2. Registry Pattern (Handler Registry)
- Dynamic registration of step handlers
- Loose coupling between navigation and steps
3. Observer Pattern (Zustand Subscriptions)
- Components subscribe to specific slices
- Automatic re-renders on state changes
4. State Machine (Step Progression)
- Finite states: pending → current → completed → error
- Well-defined transitions
5. Optimistic Concurrency (Entity Status)
- UI updates immediately
- Backend syncs asynchronously
- Handles conflicts gracefully
6. Repository Pattern (Services)
- CourseService, CourseSectionService, etc.
- Abstracts API communication
7. Facade Pattern (Navigation Hook)
- Simplifies complex navigation logic
- Single interface for step navigation
8. Strategy Pattern (Validation)
- Different validation strategies per step
- Zod schemas define validation rules
Extension Points
Adding a New Step
- Create step component (
pages/editor/NewStep.tsx) - Add to initial state (
stores/course-editor/types.ts) - Create form slice methods (if needed)
- Add validation schema (
utils/schema/course.schema.ts) - Register in navigation (
EditorStepenum) - Add to tabs array (
CourseEditor.tsx) - Implement step handler (using
useStepSubmission)
Adding a New Entity Type
- Define interface (
utils/types.ts) - Add to store state (
stores/course-editor/state.ts) - Create CRUD methods (new or existing slice)
- Add sync methods (
sync.slice.ts) - Create service (
services/new-entity.service.ts) - Build UI component (
components/course/NewEntity.tsx)
Custom Validation
Additional Hooks
Conclusion
The Instructor Course Editor demonstrates a sophisticated, production-ready architecture that balances:- User Experience: Instant feedback, clear progression, unsaved changes protection
- Data Integrity: Validation at multiple levels, optimistic updates with rollback
- Performance: Memoization, shallow equality, debounced validation
- Maintainability: Clear separation of concerns, typed interfaces, extensible patterns
- Resilience: Retry logic, error boundaries, graceful degradation
Document History:
- v1.0 (2025-11-02): Initial architecture documentation