Skip to content

Latest commit

 

History

History
294 lines (223 loc) · 9.46 KB

File metadata and controls

294 lines (223 loc) · 9.46 KB

Electives Search and Filtering Implementation

Overview

This document details the implementation of the electives search and filtering feature for the StrellerMinds Frontend application. The feature provides comprehensive search, filtering, and pagination capabilities for elective courses using mock data and in-memory processing.

Features Implemented

Search Functionality

  • Real-time search with 300ms debouncing for optimal performance
  • Multi-field search across elective name, description, instructor, and tags
  • Case-insensitive matching for better user experience
  • Clear search functionality with visual feedback

Advanced Filtering

  • Category filter - Dropdown with all available categories
  • Credit hours filter - Options for 1-5+ credits
  • Difficulty level filter - Beginner, Intermediate, Advanced
  • Expandable filter panel to save screen space
  • Active filter display with individual removal options
  • Clear all filters functionality

Pagination

  • Client-side pagination with configurable items per page (6 items)
  • Smart pagination controls with ellipsis for large page counts
  • Page state preservation in URL parameters
  • Smooth scrolling to top when changing pages

URL State Management

  • Query parameter synchronization for all filters and pagination
  • Shareable URLs that preserve filter state
  • Browser history integration without page reloads
  • Deep linking support for specific filter combinations

Technical Implementation

Architecture

src/
├── app/electives/
│   ├── page.tsx          # Main electives page component
│   ├── loading.tsx       # Loading skeleton UI
│   └── error.tsx         # Error boundary component
├── components/
│   ├── ElectiveCard.tsx      # Individual elective display
│   ├── ElectiveFilters.tsx   # Search and filter controls
│   └── ElectivePagination.tsx # Pagination component
└── lib/
    └── electives-data.ts     # Mock data and interfaces

Data Structure

interface ElectiveData {
  id: string;
  name: string;
  description: string;
  category: string;
  creditHours: number;
  instructor: string;
  level: 'Beginner' | 'Intermediate' | 'Advanced';
  duration: string;
  prerequisites?: string[];
  studentsEnrolled: number;
  rating: number;
  reviewCount: number;
  imageUrl?: string;
  tags: string[];
  dateAdded: string;
  popularity: number;
  isActive: boolean;
}

Performance Optimizations

1. useMemo for Filtering

const { filteredElectives, totalPages, totalResults } = useMemo(() => {
  // Chain multiple filters efficiently
  let filtered = allElectives.filter((elective) => elective.isActive);

  // Apply search, category, credit hours, and level filters
  // Calculate pagination

  return { filteredElectives, totalPages, totalResults };
}, [
  searchTerm,
  selectedCategory,
  selectedCreditHours,
  selectedLevel,
  currentPage,
]);

2. Debounced Search

useEffect(() => {
  const timer = setTimeout(() => {
    onFilterChange.search(debouncedSearchTerm);
  }, 300);
  return () => clearTimeout(timer);
}, [debouncedSearchTerm, onFilterChange]);

3. Efficient Re-renders

  • Memoized filter functions prevent unnecessary recalculations
  • Optimized component structure minimizes re-render scope
  • Smart state management reduces cascading updates

Search Algorithm

The search functionality implements a comprehensive multi-field search:

if (searchTerm) {
  const searchLower = searchTerm.toLowerCase();
  filtered = filtered.filter(
    (elective) =>
      elective.name.toLowerCase().includes(searchLower) ||
      elective.description.toLowerCase().includes(searchLower) ||
      elective.instructor.toLowerCase().includes(searchLower) ||
      elective.tags.some((tag) => tag.toLowerCase().includes(searchLower)),
  );
}

Search Features:

  • Partial matching - Finds results containing the search term
  • Multi-field search - Searches across name, description, instructor, and tags
  • Case-insensitive - Works regardless of input case
  • Tag search - Includes hashtag-style tags in search results

Filtering Logic

Filters are applied in sequence for optimal performance:

  1. Base filter - Only active electives
  2. Search filter - Text-based matching
  3. Category filter - Exact category matching
  4. Credit hours filter - Numeric matching with special "5+" handling
  5. Level filter - Exact level matching
  6. Pagination - Data slicing for current page

URL State Management

The application maintains filter state in URL parameters:

// URL format: /electives?search=blockchain&category=Development&credits=3&level=Advanced&page=2

useEffect(() => {
  const params = new URLSearchParams();

  if (searchTerm) params.set('search', searchTerm);
  if (selectedCategory) params.set('category', selectedCategory);
  if (selectedCreditHours) params.set('credits', selectedCreditHours);
  if (selectedLevel) params.set('level', selectedLevel);
  if (currentPage > 1) params.set('page', currentPage.toString());

  const queryString = params.toString();
  const newUrl = queryString ? `/electives?${queryString}` : '/electives';

  window.history.replaceState({}, '', newUrl);
}, [
  searchTerm,
  selectedCategory,
  selectedCreditHours,
  selectedLevel,
  currentPage,
]);

User Experience Features

Responsive Design

  • Mobile-first approach with responsive grid layouts
  • Collapsible filters on smaller screens
  • Touch-friendly controls for mobile devices
  • Adaptive pagination that works on all screen sizes

Accessibility

  • ARIA labels for screen readers
  • Keyboard navigation support
  • Focus management for interactive elements
  • Semantic HTML structure
  • Color contrast compliance

Error Handling

  • Empty state with helpful messaging and clear filters action
  • Error boundaries for graceful failure handling
  • Loading states with skeleton UI
  • Input validation with maximum length limits
  • Graceful degradation when JavaScript is disabled

Visual Feedback

  • Active filter indicators with count badges
  • Hover states for interactive elements
  • Loading animations during state changes
  • Clear visual hierarchy with proper typography
  • Consistent color coding for categories and levels

Mock Data

The implementation includes 12 diverse elective courses across various categories:

  • Ethics & Philosophy - Blockchain Ethics and Governance
  • Economics - Cryptocurrency Economics
  • Design - Web3 User Experience Design
  • Development - Advanced DeFi Protocol Development
  • Art & Creativity - NFT Art Creation and Marketplace
  • Law & Regulation - Blockchain and Legal Frameworks
  • Trading & Finance - Advanced Cryptocurrency Trading
  • Governance - DAO Creation and Governance
  • Sustainability - Sustainable Blockchain Technologies
  • Virtual Reality - Metaverse and Virtual Worlds
  • Security - Blockchain Security Auditing
  • Media & Communication - Cryptocurrency Journalism

Each elective includes comprehensive metadata for realistic filtering and search scenarios.

Testing Scenarios

Search Testing

  1. Search for "blockchain" - should return multiple results
  2. Search for instructor names - should filter by instructor
  3. Search for tags like "DeFi" or "NFT" - should match tag-based results
  4. Empty search - should show all electives

Filter Testing

  1. Select different categories - should filter appropriately
  2. Choose credit hour ranges - should match credit requirements
  3. Select difficulty levels - should filter by level
  4. Combine multiple filters - should apply all filters simultaneously

Pagination Testing

  1. Navigate through pages - should maintain filter state
  2. Change filters - should reset to page 1
  3. Direct URL access - should load correct page with filters

URL State Testing

  1. Share filtered URL - should load with same filters applied
  2. Browser back/forward - should maintain state
  3. Refresh page - should preserve current filter state

Performance Metrics

  • Initial load time - Optimized with skeleton loading
  • Search response time - 300ms debounce prevents excessive filtering
  • Filter application - Instant with useMemo optimization
  • Memory usage - Efficient with proper cleanup and memoization
  • Bundle size impact - Minimal additional dependencies

Future Enhancements

Potential Improvements

  1. Server-side filtering for larger datasets
  2. Advanced search operators (AND, OR, NOT)
  3. Saved search preferences with local storage
  4. Sort functionality (by rating, popularity, date)
  5. Infinite scroll as alternative to pagination
  6. Search analytics and popular searches
  7. Filter presets for common combinations
  8. Export functionality for filtered results

Scalability Considerations

  • Current implementation handles hundreds of electives efficiently
  • For thousands of items, consider virtual scrolling or server-side pagination
  • Search indexing would improve performance for large datasets
  • Caching strategies could optimize repeated filter operations

Conclusion

The electives search and filtering implementation provides a comprehensive, performant, and user-friendly solution for browsing educational content. The architecture is scalable, maintainable, and follows React/Next.js best practices while delivering an excellent user experience across all devices.