diff --git a/src/pages/admin/AdminContact.tsx b/src/pages/admin/AdminContact.tsx index 55f9927b..3b949723 100644 --- a/src/pages/admin/AdminContact.tsx +++ b/src/pages/admin/AdminContact.tsx @@ -104,6 +104,7 @@ export default function AdminContact() { const [loadingThreads, setLoadingThreads] = useState(false); const [copiedTicket, setCopiedTicket] = useState(null); const [activeTab, setActiveTab] = useState('details'); + const [replyStatus, setReplyStatus] = useState(''); // Fetch contact submissions const { data: submissions, isLoading } = useQuery({ @@ -143,6 +144,7 @@ export default function AdminContact() { useEffect(() => { if (selectedSubmission) { setLoadingThreads(true); + // Force fresh fetch - no caching supabase .from('contact_email_threads') .select('*') @@ -161,6 +163,7 @@ export default function AdminContact() { setAdminNotes(selectedSubmission.admin_notes || ''); setReplyBody(''); setShowReplyForm(false); + setReplyStatus(selectedSubmission.status); // Initialize reply status } else { // Reset tab to details when dialog closes setActiveTab('details'); @@ -169,19 +172,47 @@ export default function AdminContact() { // Send reply mutation const sendReplyMutation = useMutation({ - mutationFn: async ({ submissionId, body }: { submissionId: string, body: string }) => { + mutationFn: async ({ submissionId, body, newStatus }: { submissionId: string, body: string, newStatus?: string }) => { const { data, error } = await invokeWithTracking( 'send-admin-email-reply', { submissionId, replyBody: body }, undefined ); if (error) throw error; + + // Update status if changed + if (newStatus && selectedSubmission && newStatus !== selectedSubmission.status) { + const updateData: Record = { status: newStatus }; + + if (newStatus === 'resolved' || newStatus === 'closed') { + const { data: { user } } = await supabase.auth.getUser(); + updateData.resolved_at = new Date().toISOString(); + updateData.resolved_by = user?.id; + } + + const { error: statusError } = await supabase + .from('contact_submissions') + .update(updateData) + .eq('id', submissionId); + + if (statusError) { + logger.error('Failed to update status', { error: statusError }); + throw statusError; + } + } + return data; }, onSuccess: (_data, variables) => { const submission = submissions?.find(s => s.id === variables.submissionId); const ticketNumber = submission?.ticket_number || 'Unknown'; - handleSuccess('Reply Sent', `Your email response has been sent for ticket ${ticketNumber}`); + + let message = `Your email response has been sent for ticket ${ticketNumber}`; + if (variables.newStatus && selectedSubmission && variables.newStatus !== selectedSubmission.status) { + message += ` and status updated to ${variables.newStatus.replace('_', ' ')}`; + } + + handleSuccess('Reply Sent', message); setReplyBody(''); setShowReplyForm(false); // Refetch threads @@ -287,6 +318,27 @@ export default function AdminContact() { setAdminNotes(submission.admin_notes || ''); setActiveTab('thread'); setShowReplyForm(true); + setReplyStatus(submission.status); + }; + + const handleRefreshThreads = () => { + if (!selectedSubmission) return; + setLoadingThreads(true); + supabase + .from('contact_email_threads') + .select('*') + .eq('submission_id', selectedSubmission.id) + .order('created_at', { ascending: true }) + .then(({ data, error }) => { + if (error) { + logger.error('Failed to refresh email threads', { error }); + handleError(error, { action: 'Refresh Email Threads' }); + } else { + setEmailThreads((data as EmailThread[]) || []); + handleSuccess('Refreshed', 'Email thread updated'); + } + setLoadingThreads(false); + }); }; // Show loading state while roles are being fetched @@ -601,9 +653,20 @@ export default function AdminContact() { Details - + Email Thread ({emailThreads.length}) + {activeTab === 'thread' && ( + + )} @@ -734,11 +797,34 @@ export default function AdminContact() { rows={6} className="mb-3 resize-none" /> + + {/* Status Update Dropdown */} +
+ + + {replyStatus !== selectedSubmission.status && ( +

+ Status will be updated to {replyStatus.replace('_', ' ')} +

+ )} +
+