-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathSearchResults.tsx
197 lines (188 loc) · 6.13 KB
/
SearchResults.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {
AdvancedSearchJoinAttrInfo,
AdvancedSearchResult,
} from "@dmm-com/airone-apiclient-typescript-fetch";
import {
Box,
Checkbox,
List,
ListItem,
Paper,
Table,
TableBody,
TableCell,
TableContainer,
TableRow,
Typography,
} from "@mui/material";
import { styled } from "@mui/material/styles";
import React, { FC, useMemo } from "react";
import { Link } from "react-router-dom";
import { SearchResultsTableHead } from "./SearchResultsTableHead";
import { entryDetailsPath } from "Routes";
import { PaginationFooter } from "components/common/PaginationFooter";
import { AttributeValue } from "components/entry/AttributeValue";
import { AdvancedSerarchResultList } from "services/Constants";
import { AttrsFilter } from "services/entry/AdvancedSearch";
const StyledBox = styled(Box)({
display: "table",
overflowX: "inherit",
padding: "24px",
});
const StyledTableRow = styled(TableRow)({
"&:nth-of-type(odd)": {
backgroundColor: "#F9FAFA",
},
"&:nth-of-type(even)": {
backgroundColor: "#FFFFFF",
},
"&:last-child td, &:last-child th": {
border: 0,
},
"& td": {
padding: "8px 16px",
},
});
interface Props {
results: AdvancedSearchResult;
page: number;
changePage: (page: number) => void;
hasReferral: boolean;
defaultEntryFilter?: string;
defaultReferralFilter?: string;
defaultAttrsFilter?: AttrsFilter;
bulkOperationEntryIds: Array<number>;
handleChangeBulkOperationEntryId: (id: number, checked: boolean) => void;
entityIds: number[];
searchAllEntities: boolean;
joinAttrs: AdvancedSearchJoinAttrInfo[];
disablePaginationFooter: boolean;
setSearchResults: () => void;
isReadonly?: boolean;
}
export const SearchResults: FC<Props> = ({
results,
page,
changePage,
hasReferral,
defaultEntryFilter,
defaultReferralFilter,
defaultAttrsFilter = {},
bulkOperationEntryIds,
handleChangeBulkOperationEntryId,
entityIds,
searchAllEntities,
joinAttrs,
disablePaginationFooter,
setSearchResults,
isReadonly = false,
}) => {
// NOTE attrTypes are guessed by the first element on the results. So if it has no appropriate attr,
// the type guess doesn't work well. We should improve attr type API if more accurate type is needed.
const [attrNames, attrTypes] = useMemo(() => {
const _attrNames = Object.keys(defaultAttrsFilter);
const _attrTypes = Object.fromEntries(
_attrNames.map((attrName) => [
attrName,
results.values[0]?.attrs[attrName]?.type,
])
);
return [_attrNames, _attrTypes];
}, [defaultAttrsFilter, results.values]);
return (
<Box display="flex" flexDirection="column">
<StyledBox>
<TableContainer component={Paper}>
<Table id="table_result_list">
<SearchResultsTableHead
hasReferral={hasReferral}
attrTypes={attrTypes}
defaultEntryFilter={defaultEntryFilter}
defaultReferralFilter={defaultReferralFilter}
defaultAttrsFilter={defaultAttrsFilter}
entityIds={entityIds}
searchAllEntities={searchAllEntities}
joinAttrs={joinAttrs}
refreshSearchResults={setSearchResults}
isReadonly={isReadonly}
/>
<TableBody>
{results.values?.map((result) => (
<StyledTableRow key={result.entry.id}>
{/* Bulk operation checkbox would be invisible when Readonly mode is true */}
{!isReadonly && (
<TableCell sx={{ padding: 0 }}>
<Checkbox
checked={bulkOperationEntryIds.includes(
result.entry.id
)}
onChange={(e) =>
handleChangeBulkOperationEntryId(
result.entry.id,
e.target.checked
)
}
/>
</TableCell>
)}
<TableCell>
<Box
component={Link}
to={entryDetailsPath(result.entity.id, result.entry.id)}
>
{result.entry.name}
</Box>
</TableCell>
{attrNames.map((attrName) => (
<TableCell key={attrName}>
{(() => {
const info = result.attrs[attrName];
if (info != null) {
return (
<>
{info.isReadable ? (
<AttributeValue attrInfo={info} />
) : (
<Typography>Permission denied.</Typography>
)}
</>
);
} else if (!result.isReadable) {
return <Typography>Permission denied.</Typography>;
}
})()}
</TableCell>
))}
{hasReferral && (
<TableCell>
<List>
{result.referrals?.map((referral) => (
<ListItem key={referral.id}>
<Box
component={Link}
to={entryDetailsPath(0, referral.id)}
>
{referral.name}
</Box>
</ListItem>
))}
</List>
</TableCell>
)}
</StyledTableRow>
))}
</TableBody>
</Table>
</TableContainer>
{!disablePaginationFooter && (
<PaginationFooter
count={results.count}
maxRowCount={AdvancedSerarchResultList.MAX_ROW_COUNT}
page={page}
changePage={changePage}
/>
)}
</StyledBox>
</Box>
);
};