import React, { memo, useState, useEffect } from 'react'; import { Button, Row, Col, Input, Tabs, Space, ConfigProvider, Card, Tag, Switch } from 'antd'; import { PlusOutlined, EditOutlined, DeleteOutlined, SearchOutlined, UserOutlined, PhoneOutlined, } from '@ant-design/icons'; import { useNavigate } from 'react-router-dom'; import { NotifAlert, NotifConfirmDialog } from '../../../components/Global/ToastNotif'; import { getAllContact, deleteContact, updateContact } from '../../../api/contact'; const ContactCard = memo(function ContactCard({ contact, showEditModal, showDeleteModal, onStatusToggle, }) { const handleStatusToggle = async (checked) => { try { const updatedContact = { contact_name: contact.contact_name || contact.name, contact_phone: contact.contact_phone || contact.phone, is_active: checked, contact_type: contact.contact_type, }; await updateContact(contact.contact_id || contact.id, updatedContact); NotifAlert({ icon: 'success', title: 'Berhasil', message: `Status "${contact.contact_name || contact.name}" berhasil diperbarui.`, }); // Refresh contacts list onStatusToggle && onStatusToggle(); } catch (error) { console.error('Error updating contact status:', error); NotifAlert({ icon: 'error', title: 'Error', message: 'Gagal memperbarui status kontak', }); } }; return (
{ e.currentTarget.style.boxShadow = '0 4px 12px rgba(0,0,0,0.15)'; }} onMouseLeave={(e) => { e.currentTarget.style.boxShadow = '0 2px 8px rgba(0,0,0,0.1)'; }} >
{/* Type Badge - Top Left */} {/*
{contact.contact_type === 'operator' ? 'Operator' : contact.contact_type === 'gudang' ? 'Gudang' : 'Unknown'}
*/} {/* Status Slider - Top Right */}
{contact.status === 'active' ? 'Active' : 'Inactive'}
{/* Main Content */}
{contact.contact_name || contact.name}
{contact.contact_phone || contact.phone}
{/* Edit and Delete Buttons - Bottom Right */}
); }); const ListContact = memo(function ListContact(props) { const [activeTab, setActiveTab] = useState('all'); const [filteredContacts, setFilteredContacts] = useState([]); const [loading, setLoading] = useState(false); const navigate = useNavigate(); // Default filter object matching plantSection pattern const defaultFilter = { criteria: '' }; const [formDataFilter, setFormDataFilter] = useState(defaultFilter); // Fetch contacts from API const fetchContacts = async () => { setLoading(true); try { // Build search parameters matching database pattern const searchParams = { ...formDataFilter }; // Add specific filters if not "all" if (activeTab !== 'all') { if (activeTab === 'operator') { searchParams.code = 'operator'; } else if (activeTab === 'gudang') { searchParams.code = 'gudang'; } } const queryParams = new URLSearchParams(); Object.entries(searchParams).forEach(([key, value]) => { if (value !== '' && value !== null && value !== undefined) { queryParams.append(key, value); } }); const response = await getAllContact(queryParams); setFilteredContacts(response.data || []); } catch (error) { console.error('Error fetching contacts:', error); NotifAlert({ icon: 'error', title: 'Error', message: 'Gagal memuat data kontak', }); } finally { setLoading(false); } }; // Fetch contacts on component mount useEffect(() => { const token = localStorage.getItem('token'); if (!token) { navigate('/signin'); return; } fetchContacts(); }, []); // Refetch when filters change useEffect(() => { fetchContacts(); }, [formDataFilter, activeTab]); // Listen for saved contact data useEffect(() => { if (props.lastSavedContact) { fetchContacts(); } }, [props.lastSavedContact]); const getFilteredContacts = () => { return filteredContacts; }; const showEditModal = (param) => { props.setSelectedData(param); props.setActionMode('edit'); }; const showAddModal = () => { props.setSelectedData(null); props.setActionMode('add'); props.setContactType?.(activeTab); }; const showDeleteModal = (contact) => { NotifConfirmDialog({ icon: 'question', title: 'Konfirmasi Hapus', message: `Kontak "${contact.contact_name || contact.name}" akan dihapus?`, onConfirm: () => handleDelete(contact), onCancel: () => props.setSelectedData(null), }); }; const handleDelete = async (contact) => { try { await deleteContact(contact.contact_id || contact.id); NotifAlert({ icon: 'success', title: 'Berhasil', message: `Kontak "${contact.contact_name || contact.name}" berhasil dihapus.`, }); // Refetch contacts after deletion fetchContacts(); } catch (error) { console.error('Error deleting contact:', error); NotifAlert({ icon: 'error', title: 'Error', message: 'Gagal menghapus kontak', }); } }; return ( { const value = e.target.value; setFormDataFilter({ criteria: value }); if (value === '') { setFormDataFilter(defaultFilter); } }} onSearch={(value) => setFormDataFilter({ criteria: value })} allowClear={{ clearIcon: ( setFormDataFilter(defaultFilter)}> ✕ ), }} enterButton={ } size="large" />
{/* Tabs */} {/* */}
{getFilteredContacts().length === 0 ? (
{loading ? 'Loading contacts...' : 'No contacts found'}
) : ( {getFilteredContacts().map((contact) => ( ))} )}
); }); export default ListContact;