import { useState, useEffect } from 'react'; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { useCallApi } from '@/hooks'; import { apiConfig } from '@/config/api.config'; import { getAuth } from '@/auth'; import { useManageFeedbackContext } from '../hooks/useManageFeedbackMemberContext'; import { Button } from '@/components/ui/button'; import { toast } from 'sonner'; import { X } from 'lucide-react'; const API_URL = apiConfig.service_feedback; // Define status types type StatusCode = 'W' | 'N' | 'Y'; // Status display mapping for Select component const statusDisplayMap: Record = { W: 'Waiting Follow Up', N: 'Rejected', Y: 'Accepted' }; const EditDialog = () => { const [loading, setLoading] = useState(false); const { PutData, GetData } = useCallApi(); const { handleEditDialog, showEditDialog, selectedFeedback, handleDetailDialog, refreshData } = useManageFeedbackContext(); const [reviewNotes, setReviewNotes] = useState(''); const [status, setStatus] = useState('Y'); // Default to Accepted useEffect(() => { if (showEditDialog && selectedFeedback) { fetchFeedbackDetail(); } }, [showEditDialog, selectedFeedback]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { const username = getAuth()?.user.username; const payload = { review_by: username, review_notes: reviewNotes, status: status }; const response = await PutData(`${API_URL}/feedback/update/${selectedFeedback}`, payload); if (response?.status) { toast.success('Feedback reviewed successfully'); handleEditDialog(false, null); handleDetailDialog(false, null); refreshData(); setTimeout(() => { handleDetailDialog(true, selectedFeedback); }, 300); } else { toast.error(response?.message || 'Failed to update feedback'); } } catch (error) { console.error('Error updating feedback:', error); toast.error('An error occurred while updating feedback'); } finally { setLoading(false); } }; const fetchFeedbackDetail = async () => { if (!selectedFeedback) return; try { setLoading(true); const response = await GetData(`${API_URL}/feedback/detail/${selectedFeedback}`, { id: selectedFeedback }); if (!response || !response.data) { console.error('Invalid response format'); return; } const data = response.data; setReviewNotes(data.review_notes || ''); const currentStatus = (data.status as StatusCode) || 'Y'; setStatus(currentStatus); } catch (error) { console.error('Error fetching feedback details:', error); } finally { setLoading(false); } }; return ( handleEditDialog(open, null)}>

Review Feedback

Review and update feedback status