Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-sonnet-4-20250514· AnthropicHugging Face LMArena Human Preference 140K

create filter component using react mobx and graphql

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with grok-3-preview-02-24’s answer.
  1. User

    create filter component using react mobx and graphql

  2. Claude Assistant

    I'll help you create a filter component using React, MobX, and GraphQL. Here's a complete implementation:

    MobX Store

    // stores/FilterStore.js
    import { makeAutoObservable } from 'mobx';
    
    class FilterStore {
      filters = {
        search: '',
        category: '',
        status: '',
        dateRange: { start: null, end: null }
      };
    
      constructor() {
        makeAutoObservable(this);
      }
    
      setFilter(key, value) {
        this.filters[key] = value;
      }
    
      setDateRange(start, end) {
        this.filters.dateRange = { start, end };
      }
    
      clearFilters() {
        this.filters = {
          search: '',
          category: '',
          status: '',
          dateRange: { start: null, end: null }
        };
      }
    
      get activeFiltersCount() {
        let count = 0;
        if (this.filters.search) count++;
        if (this.filters.category) count++;
        if (this.filters.status) count++;
        if (this.filters.dateRange.start || this.filters.dateRange.end) count++;
        return count;
      }
    
      get filterVariables() {
        const variables = {};
        
        if (this.filters.search) {
          variables.search = this.filters.search;
        }
        if (this.filters.category) {
          variables.category = this.filters.category;
        }
        if (this.filters.status) {
          variables.status = this.filters.status;
        }
        if (this.filters.dateRange.start) {
          variables.startDate = this.filters.dateRange.start;
        }
        if (this.filters.dateRange.end) {
          variables.endDate = this.filters.dateRange.end;
        }
        
        return variables;
      }
    }
    
    export default new FilterStore();
    

    GraphQL Query

    // queries/itemQueries.js
    import { gql } from '@apollo/client';
    
    export const GET_FILTERED_ITEMS = gql`
      query GetFilteredItems(
        $search: String
        $category: String
        $status: String
        $startDate: DateTime
        $endDate: DateTime
      ) {
        items(
          search: $search
          category: $category
          status: $status
          startDate: $startDate
          endDate: $endDate
        ) {
          id
          name
          category
          status
          createdAt
          description
        }
      }
    `;
    
    export const GET_FILTER_OPTIONS = gql`
      query GetFilterOptions {
        categories {
          id
          name
        }
        statuses {
          value
          label
        }
      }
    `;
    

    Filter Component

    // components/FilterComponent.jsx
    import React, { useEffect } from 'react';
    import { observer } from 'mobx-react-lite';
    import { useQuery } from '@apollo/client';
    import filterStore from '../stores/FilterStore';
    import { GET_FILTER_OPTIONS } from '../queries/itemQueries';
    
    const FilterComponent = observer(() => {
      const { data: filterOptions, loading } = useQuery(GET_FILTER_OPTIONS);
    
      const handleInputChange = (key, value) => {
        filterStore.setFilter(key, value);
      };
    
      const handleDateChange = (type, value) => {
        const { start, end } = filterStore.filters.dateRange;
        if (type === 'start') {
          filterStore.setDateRange(value, end);
        } else {
          filterStore.setDateRange(start, value);
        }
      };
    
      if (loading) return <div>Loading filters...</div>;
    
      return (
        <div className="filter-container">
          <div className="filter-header">
            <h3>Filters</h3>
            {filterStore.activeFiltersCount > 0 && (
              <button 
                onClick={() => filterStore.clearFilters()}
                className="clear-filters-btn"
              >
                Clear All ({filterStore.activeFiltersCount})
              </button>
            )}
          </div>
    
          <div className="filter-group">
            <label>Search</label>
            <input
              type="text"
              placeholder="Search items..."
              value={filterStore.filters.search}
              onChange={(e) => handleInputChange('search', e.target.value)}
              className="filter-input"
            />
          </div>
    
          <div className="filter-group">
            <label>Category</label>
            <select
              value={filterStore.filters.category}
              onChange={(e) => handleInputChange('category', e.target.value)}
              className="filter-select"
            >
              <option value="">All Categories</option>
              {filterOptions?.categories?.map(category => (
                <option key={category.id} value={category.id}>
                  {category.name}
                </option>
              ))}
            </select>
          </div>
    
          <div className="filter-group">
            <label>Status</label>
            <select
              value={filterStore.filters.status}
              onChange={(e) => handleInputChange('status', e.target.value)}
              className="filter-select"
            >
              <option value="">All Statuses</option>
              {filterOptions?.statuses?.map(status => (
                <option key={status.value} value={status.value}>
                  {status.label}
                </option>
              ))}
            </select>
          </div>
    
          <div className="filter-group">
            <label>Date Range</label>
            <div className="date-range">
              <input
                type="date"
                value={filterStore.filters.dateRange.start || ''}
                onChange={(e) => handleDateChange('start', e.target.value)}
                className="filter-input"
                placeholder="Start date"
              />
              <input
                type="date"
                value={filterStore.filters.dateRange.end || ''}
                onChange={(e) => handleDateChange('end', e.target.value)}
                className="filter-input"
                placeholder="End date"
              />
            </div>
          </div>
        </div>
      );
    });
    
    export default FilterComponent;
    

    Items List Component (using the filters)

    // components/ItemsList.jsx
    import React from 'react';
    import { observer } from 'mobx-react-lite';
    import { useQuery } from '@apollo/client';
    import filterStore from '../stores/FilterStore';
    import { GET_FILTERED_ITEMS } from '../queries/itemQueries';
    
    const ItemsList = observer(() => {
      const { data, loading, error } = useQuery(GET_FILTERED_ITEMS, {
        variables: filterStore.filterVariables,
        fetchPolicy: 'cache-and-network'
      });
    
      if (loading) return <div>Loading items...</div>;
      if (error) return <div>Error: {error.message}</div>;
    
      return (
        <div className="items-list">
          <h2>Items ({data?.items?.length || 0})</h2>
          {data?.items?.length === 0 ? (
            <div className="no-results">No items found matching your filters.</div>
          ) : (
            <div className="items-grid">
              {data?.items?.map(item => (
                <div key={item.id} className="item-card">
                  <h3>{item.name}</h3>
                  <p>{item.description}</p>
                  <div className="item-meta">
                    <span className="category">{item.category}</span>
                    <span className="status">{item.status}</span>
                    <span className="date">
                      {new Date(item.createdAt).toLocaleDateString()}
                    </span>
                  </div>
                </div>
              ))}
            </div>
          )}
        </div>
      );
    });
    
    export default ItemsList;
    

    Main App Component

    // App.jsx
    import React from 'react';
    import { ApolloProvider } from '@apollo/client';
    import client from './apollo/client'; // Your Apollo client setup
    import FilterComponent from './components/FilterComponent';
    import ItemsList from './components/ItemsList';
    import './App.css';
    
    function App() {
      return (
        <ApolloProvider client={client}>
          <div className="app">
            <div className="sidebar">
              <FilterComponent />
            </div>
            <div className="main-content">
              <ItemsList />
            </div>
          </div>
        </ApolloProvider>
      );
    }
    
    export default App;
    

    Basic CSS

    /* App.css */
    .app {
      display: flex;
      min-height: 100vh;
    }
    
    .sidebar {
      width: 300px;
      padding: 20px;
      background-color: #f5f5f5;
      border-right: 1px solid #ddd;
    }
    
    .main-content {
      flex: 1;
      padding: 20px;
    }
    
    .filter-container {
      display: flex;
      flex-direction: column;
      gap: 20px;
    }
    
    .filter-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .clear-filters-btn {
      background: #ff6b6b;
      color: white;
      border: none;
      padding: 5px 10px;
      border-radius: 4px;
      cursor: pointer;
      font-size: 12px;
    }
    
    .filter-group {
      display: flex;
      flex-direction: column;
      gap: 8px;
    }
    
    .filter-group label {
      font-weight: bold;
      color: #333;
    }
    
    .filter-input, .filter-select {
      padding: 8px;
      border: 1px solid #ddd;
      border-radius: 4px;
      font-size: 14px;
    }
    
    .date-range {
      display: flex;
      gap: 10px;
    }
    
    .items-grid {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
      gap: 20px;
      margin-top: 20px;
    }
    
    .item-card {
      border: 1px solid #ddd;
      border-radius: 8px;
      padding: 16px;
      background: white;
    }
    
    .item-meta {
      display: flex;
      gap: 10px;
      margin-top: 10px;
      font-size: 12px;
    }
    
    .category, .status {
      padding: 2px 8px;
      border-radius: 12px;
      background: #e9ecef;
    }
    
    .no-results {
      text-align: center;
      color: #666;
      margin-top: 40px;
    }
    

    This implementation provides a complete filter system where the MobX store manages filter state, GraphQL handles data fetching with dynamic variables, and React components render the UI. The filters automatically trigger new GraphQL queries when changed, and the MobX store provides computed values for clean integration between the filter state and GraphQL variables.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.