import React, { useState } from 'react'; import type { SuggestedCorrection, MasterGroceryItem } from '../types'; import { approveCorrection, rejectCorrection } from '../services/supabaseClient'; import { logger } from '../services/logger'; import { CheckIcon } from './icons/CheckIcon'; import { XMarkIcon } from './icons/XMarkIcon'; import { LoadingSpinner } from './LoadingSpinner'; import { ConfirmationModal } from './ConfirmationModal'; import { ExclamationTriangleIcon } from './icons/ExclamationTriangleIcon'; interface CorrectionRowProps { correction: SuggestedCorrection; masterItems: MasterGroceryItem[]; onProcessed: (correctionId: number) => void; } export const CorrectionRow: React.FC = ({ correction, masterItems, onProcessed }) => { const [isProcessing, setIsProcessing] = useState(false); const [error, setError] = useState(null); const [isModalOpen, setIsModalOpen] = useState(false); const [actionToConfirm, setActionToConfirm] = useState<'approve' | 'reject' | null>(null); // Helper to make the suggested value more readable for the admin. const formatSuggestedValue = () => { const { correction_type, suggested_value } = correction; if (correction_type === 'WRONG_PRICE') { const priceInCents = parseInt(suggested_value, 10); if (!isNaN(priceInCents)) { return `$${(priceInCents / 100).toFixed(2)}`; } } if (correction_type === 'INCORRECT_ITEM_LINK') { const masterItemId = parseInt(suggested_value, 10); const item = masterItems.find(mi => mi.id === masterItemId); return item ? `${item.name} (ID: ${masterItemId})` : `Unknown Item (ID: ${masterItemId})`; } return suggested_value; }; const handleConfirm = async () => { if (!actionToConfirm) return; setIsProcessing(true); setIsModalOpen(false); setError(null); try { if (actionToConfirm === 'approve') { await approveCorrection(correction.id); logger.info(`Correction ${correction.id} approved.`); } else { await rejectCorrection(correction.id); logger.info(`Correction ${correction.id} rejected.`); } onProcessed(correction.id); } catch (err: any) { logger.error(`Failed to ${actionToConfirm} correction ${correction.id}`, err); setError(err.message); setIsProcessing(false); } setActionToConfirm(null); }; return ( <> setIsModalOpen(false)} onConfirm={handleConfirm} title={actionToConfirm === 'approve' ? 'Approve Correction' : 'Reject Correction'} message={ actionToConfirm === 'approve' ? ( <> Are you sure you want to approve this correction? This will permanently modify the original flyer item. ) : ( 'Are you sure you want to reject this correction?' ) } confirmButtonText={actionToConfirm === 'approve' ? 'Approve' : 'Reject'} confirmButtonClass={ actionToConfirm === 'approve' ? 'bg-green-600 hover:bg-green-700 focus:ring-green-500' : 'bg-red-600 hover:bg-red-700 focus:ring-red-500' } />
{correction.flyer_item_name}
Original Price: {correction.flyer_item_price_display}
{correction.correction_type} {formatSuggestedValue()} {correction.user_email || 'Unknown'} {new Date(correction.created_at).toLocaleDateString()} {isProcessing ? (
) : (
)} {error &&

{error}

} ); };