source: src/sections/company/company-list-dialog.tsx@ 057453c

main
Last change on this file since 057453c was 057453c, checked in by Naum Shapkarovski <naumshapkarovski@…>, 5 weeks ago

feat: implement employees

  • Property mode set to 100644
File size: 4.6 KB
Line 
1import { useState, useCallback } from 'react';
2// @mui
3import Box from '@mui/material/Box';
4import Stack from '@mui/material/Stack';
5import Dialog from '@mui/material/Dialog';
6import TextField from '@mui/material/TextField';
7import Typography from '@mui/material/Typography';
8import InputAdornment from '@mui/material/InputAdornment';
9import ListItemButton, { listItemButtonClasses } from '@mui/material/ListItemButton';
10// types
11import { Customer } from 'src/schemas';
12// components
13import Iconify from 'src/components/iconify';
14import SearchNotFound from 'src/components/search-not-found';
15import { createFullAddress } from 'src/utils/create-full-address';
16import { Tenant } from 'src/schemas';
17
18// ----------------------------------------------------------------------
19
20type Props = {
21 title?: string;
22 tenant?: Tenant;
23 list?: Customer[];
24 action?: React.ReactNode;
25 //
26 open: boolean;
27 onClose: VoidFunction;
28 //
29 selected: (selectedId: string) => boolean;
30 onSelect: (address: Customer | Tenant | null) => void;
31};
32
33export default function AddressListDialog({
34 title = 'Address Book',
35 tenant,
36 list,
37 action,
38 //
39 open,
40 onClose,
41 //
42 selected,
43 onSelect,
44}: Props) {
45 const [searchCompany, setSearchCompany] = useState('');
46
47 const dataFiltered = applyFilter({
48 inputData: list || (tenant ? [tenant] : []),
49 query: searchCompany,
50 });
51
52 const notFound = !dataFiltered.length && !!searchCompany;
53
54 const handleSearchCompany = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
55 setSearchCompany(event.target.value);
56 }, []);
57
58 const handleSelectCompany = useCallback(
59 (company: Customer | Tenant | null) => {
60 onSelect(company);
61 setSearchCompany('');
62 onClose();
63 },
64 [onClose, onSelect]
65 );
66
67 const renderList = (
68 <Stack
69 spacing={0.5}
70 sx={{
71 p: 0.5,
72 maxHeight: 80 * 8,
73 overflowX: 'hidden',
74 }}
75 >
76 {dataFiltered.map((company) => (
77 <Stack
78 key={company.id}
79 spacing={0.5}
80 component={ListItemButton}
81 selected={selected(`${company.id}`)}
82 onClick={() => handleSelectCompany(company)}
83 sx={{
84 py: 1,
85 px: 1.5,
86 borderRadius: 1,
87 flexDirection: 'column',
88 alignItems: 'flex-start',
89 [`&.${listItemButtonClasses.selected}`]: {
90 bgcolor: 'action.selected',
91 '&:hover': {
92 bgcolor: 'action.selected',
93 },
94 },
95 }}
96 >
97 <Stack direction="row" alignItems="center" spacing={1}>
98 <Typography variant="subtitle2">{company.name}</Typography>
99
100 {/* {address.primary && <Label color="info">Default</Label>} */}
101 {/* {<Label color="info">Default</Label>} */}
102 </Stack>
103
104 {company.name && (
105 <Box sx={{ color: 'primary.main', typography: 'caption' }}>{company.email}</Box>
106 )}
107
108 <Typography variant="body2" sx={{ color: 'text.secondary' }}>
109 {createFullAddress(company.address)}
110 </Typography>
111
112 {company.phoneNumber && (
113 <Typography variant="body2" sx={{ color: 'text.secondary' }}>
114 {company.phoneNumber}
115 </Typography>
116 )}
117 </Stack>
118 ))}
119 </Stack>
120 );
121
122 return (
123 <Dialog fullWidth maxWidth="xs" open={open} onClose={onClose}>
124 <Stack
125 direction="row"
126 alignItems="center"
127 justifyContent="space-between"
128 sx={{ p: 3, pr: 1.5 }}
129 >
130 <Typography variant="h6"> {title} </Typography>
131
132 {action && action}
133 </Stack>
134
135 <Stack sx={{ p: 2, pt: 0 }}>
136 <TextField
137 value={searchCompany}
138 onChange={handleSearchCompany}
139 placeholder="Search..."
140 InputProps={{
141 startAdornment: (
142 <InputAdornment position="start">
143 <Iconify icon="eva:search-fill" sx={{ color: 'text.disabled' }} />
144 </InputAdornment>
145 ),
146 }}
147 />
148 </Stack>
149
150 {notFound ? (
151 <SearchNotFound query={searchCompany} sx={{ px: 3, pt: 5, pb: 10 }} />
152 ) : (
153 renderList
154 )}
155 </Dialog>
156 );
157}
158
159// ----------------------------------------------------------------------
160
161function applyFilter({ inputData, query }: { inputData: Tenant[] | Customer[]; query: string }) {
162 if (query) {
163 return inputData.filter(
164 (company) =>
165 company.name.toLowerCase().indexOf(query.toLowerCase()) !== -1 ||
166 createFullAddress(company.address).toLowerCase().indexOf(query.toLowerCase()) !== -1
167 );
168 }
169
170 return inputData;
171}
Note: See TracBrowser for help on using the repository browser.