import { useState, useCallback } from 'react'; // @mui import Box from '@mui/material/Box'; import Stack from '@mui/material/Stack'; import Dialog from '@mui/material/Dialog'; import TextField from '@mui/material/TextField'; import Typography from '@mui/material/Typography'; import InputAdornment from '@mui/material/InputAdornment'; import ListItemButton, { listItemButtonClasses } from '@mui/material/ListItemButton'; // types import { Customer } from 'src/schemas'; // components import Iconify from 'src/components/iconify'; import SearchNotFound from 'src/components/search-not-found'; import { createFullAddress } from 'src/utils/create-full-address'; import { Tenant } from 'src/schemas'; // ---------------------------------------------------------------------- type Props = { title?: string; tenant?: Tenant; list?: Customer[]; action?: React.ReactNode; // open: boolean; onClose: VoidFunction; // selected: (selectedId: string) => boolean; onSelect: (address: Customer | Tenant | null) => void; }; export default function AddressListDialog({ title = 'Address Book', tenant, list, action, // open, onClose, // selected, onSelect, }: Props) { const [searchCompany, setSearchCompany] = useState(''); const dataFiltered = applyFilter({ inputData: list || (tenant ? [tenant] : []), query: searchCompany, }); const notFound = !dataFiltered.length && !!searchCompany; const handleSearchCompany = useCallback((event: React.ChangeEvent) => { setSearchCompany(event.target.value); }, []); const handleSelectCompany = useCallback( (company: Customer | Tenant | null) => { onSelect(company); setSearchCompany(''); onClose(); }, [onClose, onSelect] ); const renderList = ( {dataFiltered.map((company) => ( handleSelectCompany(company)} sx={{ py: 1, px: 1.5, borderRadius: 1, flexDirection: 'column', alignItems: 'flex-start', [`&.${listItemButtonClasses.selected}`]: { bgcolor: 'action.selected', '&:hover': { bgcolor: 'action.selected', }, }, }} > {company.name} {/* {address.primary && } */} {/* {} */} {company.name && ( {company.email} )} {createFullAddress(company.address)} {company.phoneNumber && ( {company.phoneNumber} )} ))} ); return ( {title} {action && action} ), }} /> {notFound ? ( ) : ( renderList )} ); } // ---------------------------------------------------------------------- function applyFilter({ inputData, query }: { inputData: Tenant[] | Customer[]; query: string }) { if (query) { return inputData.filter( (company) => company.name.toLowerCase().indexOf(query.toLowerCase()) !== -1 || createFullAddress(company.address).toLowerCase().indexOf(query.toLowerCase()) !== -1 ); } return inputData; }