import React, { useState, useEffect, useCallback } from 'react' import { useLocation } from 'react-router-dom' import axios from 'axios' import { Descriptions, Spin, Space, Button, message, Typography, Flex, Form, Input, Collapse, Dropdown, Popover, Card, Checkbox } from 'antd' import { LoadingOutlined, ExportOutlined, CaretLeftOutlined } from '@ant-design/icons' import IdText from '../../common/IdText' import CountrySelect from '../../common/CountrySelect' import CountryDisplay from '../../common/CountryDisplay' import TimeDisplay from '../../common/TimeDisplay' import ReloadIcon from '../../../Icons/ReloadIcon' import EditIcon from '../../../Icons/EditIcon.jsx' import XMarkIcon from '../../../Icons/XMarkIcon.jsx' import CheckIcon from '../../../Icons/CheckIcon.jsx' import useCollapseState from '../../hooks/useCollapseState' import AuditLogTable from '../../common/AuditLogTable' import DashboardNotes from '../../common/DashboardNotes' import InfoCircleIcon from '../../../Icons/InfoCircleIcon.jsx' import NoteIcon from '../../../Icons/NoteIcon.jsx' import AuditLogIcon from '../../../Icons/AuditLogIcon.jsx' import config from '../../../../config.js' const { Title, Link, Text } = Typography const VendorInfo = () => { const [vendorData, setVendorData] = useState(null) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const location = useLocation() const [messageApi, contextHolder] = message.useMessage() const vendorId = new URLSearchParams(location.search).get('vendorId') const [isEditing, setIsEditing] = useState(false) const [form] = Form.useForm() const [fetchLoading, setFetchLoading] = useState(true) const [collapseState, updateCollapseState] = useCollapseState('VendorInfo', { info: true, notes: true, auditLogs: true }) useEffect(() => { if (vendorId) { fetchVendorDetails() } }, [vendorId, fetchVendorDetails]) useEffect(() => { if (vendorData) { form.setFieldsValue({ name: vendorData.name || '', website: vendorData.website || '', contact: vendorData.contact || '', country: vendorData.country || '', phone: vendorData.phone || '', email: vendorData.email || '' }) } }, [vendorData, form]) const fetchVendorDetails = useCallback(async () => { try { setFetchLoading(true) const response = await axios.get( `${config.backendUrl}/vendors/${vendorId}`, { headers: { Accept: 'application/json' }, withCredentials: true } ) setVendorData(response.data) setError(null) } catch (err) { setError('Failed to fetch vendor details') messageApi.error('Failed to fetch vendor details') } finally { setFetchLoading(false) } }, [messageApi, vendorId]) const startEditing = () => { updateCollapseState('info', true) setIsEditing(true) } const cancelEditing = () => { // Reset form values to original data if (vendorData) { form.setFieldsValue({ name: vendorData.name || '', website: vendorData.website || '', contact: vendorData.contact || '', country: vendorData.country || '', phone: vendorData.phone || '', email: vendorData.email || '' }) } setIsEditing(false) } const updateInfo = async () => { try { const values = await form.validateFields() setLoading(true) await axios.put(`${config.backendUrl}/vendors/${vendorId}`, values, { headers: { 'Content-Type': 'application/json' }, withCredentials: true }) setVendorData({ ...vendorData, ...values }) setIsEditing(false) messageApi.success('Vendor information updated successfully') } catch (err) { if (err.errorFields) { return } messageApi.error('Failed to update vendor information') } finally { fetchVendorDetails() setLoading(false) } } const actionItems = { items: [ { label: 'Reload Vendor', key: 'reload', icon: } ], onClick: ({ key }) => { if (key === 'reload') { fetchVendorDetails() } } } const getViewDropdownItems = () => { const sections = [ { key: 'info', label: 'Vendor Information' }, { key: 'notes', label: 'Notes' }, { key: 'auditLogs', label: 'Audit Logs' } ] return ( {sections.map((section) => ( { updateCollapseState(section.key, e.target.checked) }} > {section.label} ))} ) } if (error) { return ( {error || 'Vendor not found'} } onClick={fetchVendorDetails}> Retry ) } return ( <> {contextHolder} Actions View {isEditing ? ( <> } type='primary' onClick={updateInfo} loading={loading} disabled={loading} /> } onClick={cancelEditing} disabled={loading} /> > ) : ( } onClick={startEditing} /> )} {error ? ( {error || 'Vendor not found'} } onClick={fetchVendorDetails}> Retry ) : ( updateCollapseState('info', keys.length > 0) } expandIcon={({ isActive }) => ( )} className='no-h-padding-collapse no-t-padding-collapse' > Vendor Information } key='1' > } spinning={fetchLoading} > {vendorData?._id ? ( ) : ( n/a )} {vendorData?.createdAt ? ( ) : ( n/a )} {isEditing ? ( ) : vendorData?.name ? ( {vendorData.name} ) : ( n/a )} {vendorData?.updatedAt ? ( ) : ( n/a )} {isEditing ? ( ) : vendorData?.website ? ( {new URL(vendorData.website).hostname + ' '} ) : ( n/a )} {isEditing ? ( ) : vendorData?.country ? ( ) : ( n/a )} {isEditing ? ( ) : vendorData?.contact ? ( {vendorData.contact} ) : ( n/a )} {isEditing ? ( ) : vendorData?.phone ? ( {vendorData.phone} ) : ( n/a )} {isEditing ? ( ) : vendorData?.email ? ( {vendorData.email + ' '} ) : ( n/a )} updateCollapseState('notes', keys.length > 0) } expandIcon={({ isActive }) => ( )} className='no-h-padding-collapse' > Notes } key='notes' > updateCollapseState('auditLogs', keys.length > 0) } expandIcon={({ isActive }) => ( )} className='no-h-padding-collapse' > Audit Logs } key='auditLogs' > )} > ) } export default VendorInfo
{error || 'Vendor not found'}