All files / src/pages/admin CorrectionsPage.tsx

100% Statements 11/11
100% Branches 19/19
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                    1x             6x   6x   6x   6x 6x   6x   1x     6x                               1x                                                                                                                                               6x                                
// src/pages/admin/CorrectionsPage.tsx
import React from 'react';
import { Link } from 'react-router-dom';
import { LoadingSpinner } from '../../components/LoadingSpinner';
import { ArrowPathIcon } from '../../components/icons/ArrowPathIcon';
import { CorrectionRow } from './components/CorrectionRow';
import { useSuggestedCorrectionsQuery } from '../../hooks/queries/useSuggestedCorrectionsQuery';
import { useMasterItemsQuery } from '../../hooks/queries/useMasterItemsQuery';
import { useCategoriesQuery } from '../../hooks/queries/useCategoriesQuery';
 
export const CorrectionsPage: React.FC = () => {
  // Use TanStack Query for data fetching (ADR-0005 Phase 5)
  const {
    data: corrections = [],
    isLoading: isLoadingCorrections,
    error: correctionsError,
    refetch: refetchCorrections,
  } = useSuggestedCorrectionsQuery();
 
  const { data: masterItems = [], isLoading: isLoadingMasterItems } = useMasterItemsQuery();
 
  const { data: categories = [], isLoading: isLoadingCategories } = useCategoriesQuery();
 
  const isLoading = isLoadingCorrections || isLoadingMasterItems || isLoadingCategories;
  const error = correctionsError?.message || null;
 
  const handleCorrectionProcessed = () => {
    // Refetch corrections after processing
    refetchCorrections();
  };
 
  return (
    <div className="max-w-7xl mx-auto py-8 px-4">
      <div className="mb-8">
        <Link to="/admin" className="text-brand-primary hover:underline">
          &larr; Back to Admin Dashboard
        </Link>
        <div className="flex justify-between items-center mt-2">
          <div>
            <h1 className="text-3xl font-bold text-gray-800 dark:text-white">
              User-Submitted Corrections
            </h1>
            <p className="text-gray-500 dark:text-gray-400">
              Review and approve or reject pending data corrections.
            </p>
          </div>
          <button
            onClick={() => refetchCorrections()}
            disabled={isLoading}
            className="p-2 rounded-md bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 disabled:opacity-50"
            title="Refresh Corrections"
          >
            <ArrowPathIcon className={`w-5 h-5 ${isLoading ? 'animate-spin' : ''}`} />
          </button>
        </div>
      </div>
 
      {isLoading && (
        <div
          role="status"
          aria-label="Loading corrections"
          className="flex justify-center items-center h-64"
        >
          <LoadingSpinner />
        </div>
      )}
      {error && (
        <div className="text-red-500 bg-red-100 dark:bg-red-900/20 p-4 rounded-lg">{error}</div>
      )}
 
      {!isLoading && !error && (
        <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-x-auto">
          <table className="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
            <thead className="bg-gray-50 dark:bg-gray-700/50">
              <tr>
                <th
                  scope="col"
                  className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider"
                >
                  Item
                </th>
                <th
                  scope="col"
                  className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider"
                >
                  Correction Type
                </th>
                <th
                  scope="col"
                  className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider"
                >
                  Suggested Value
                </th>
                <th
                  scope="col"
                  className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider"
                >
                  Submitted By
                </th>
                <th
                  scope="col"
                  className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider"
                >
                  Date
                </th>
                <th scope="col" className="relative px-6 py-3">
                  <span className="sr-only">Actions</span>
                </th>
              </tr>
            </thead>
            <tbody className="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
              {corrections.length === 0 ? (
                <tr>
                  <td colSpan={6} className="text-center py-8 text-gray-500">
                    No pending corrections. Great job!
                  </td>
                </tr>
              ) : (
                corrections.map((correction) => (
                  <CorrectionRow
                    key={correction.suggested_correction_id}
                    correction={correction}
                    masterItems={masterItems}
                    categories={categories}
                    onProcessed={handleCorrectionProcessed}
                  />
                ))
              )}
            </tbody>
          </table>
        </div>
      )}
    </div>
  );
};