/** * Component: Adjust Search Terms Modal * Documentation: documentation/admin-dashboard.md */ 'use client'; import { useState } from 'react'; import { Modal } from '@/components/ui/Modal'; import { fetchWithAuth } from '@/lib/utils/api'; import { useToast } from '@/components/ui/Toast'; interface AdjustSearchTermsModalProps { isOpen: boolean; onClose: () => void; requestId: string; title: string; author: string; currentSearchTerms?: string | null; onSuccess?: () => void; } export function AdjustSearchTermsModal({ isOpen, onClose, requestId, title, author, currentSearchTerms, onSuccess, }: AdjustSearchTermsModalProps) { const toast = useToast(); const [searchTerms, setSearchTerms] = useState(currentSearchTerms || title); const [isSaving, setIsSaving] = useState(false); const [isSavingAndSearching, setIsSavingAndSearching] = useState(false); // Reset state when modal opens const handleClose = () => { setSearchTerms(currentSearchTerms || title); onClose(); }; const save = async (triggerSearch: boolean) => { const setter = triggerSearch ? setIsSavingAndSearching : setIsSaving; setter(true); try { // If terms match the original title, clear the override const termsToSave = searchTerms.trim() === title ? null : searchTerms.trim() || null; const response = await fetchWithAuth(`/api/admin/requests/${requestId}/search-terms`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ searchTerms: termsToSave, triggerSearch }), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || 'Failed to update search terms'); } const data = await response.json(); if (data.searchTriggered) { toast.success('Search terms saved and search triggered'); } else { toast.success('Search terms saved'); } onSuccess?.(); onClose(); } catch (error) { toast.error(`Failed to save: ${error instanceof Error ? error.message : 'Unknown error'}`); } finally { setter(false); } }; const handleReset = () => { setSearchTerms(title); }; const isLoading = isSaving || isSavingAndSearching; const hasChanges = searchTerms.trim() !== (currentSearchTerms || title); const isCustom = searchTerms.trim() !== title; return (
{/* Original info */}
Original Title
{title}
by {author}
{/* Search terms input */}
setSearchTerms(e.target.value)} disabled={isLoading} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 text-sm focus:ring-2 focus:ring-blue-500 focus:border-transparent disabled:opacity-50" placeholder="Enter custom search terms..." /> {isCustom && ( )}
{/* Actions */}
); }