-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAdvancedSearchPage.tsx
200 lines (183 loc) · 5.84 KB
/
AdvancedSearchPage.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
198
199
200
import {
AdvancedSearchResultAttrInfoFilterKeyEnum,
EntityList,
} from "@dmm-com/airone-apiclient-typescript-fetch";
import {
Autocomplete,
AutocompleteInputChangeReason,
Box,
Button,
Checkbox,
Container,
TextField,
Typography,
} from "@mui/material";
import { styled } from "@mui/material/styles";
import React, { FC, SyntheticEvent, useMemo, useState } from "react";
import { Link } from "react-router-dom";
import { AutocompleteWithAllSelector } from "../components/common/AutocompleteWithAllSelector";
import { PageHeader } from "../components/common/PageHeader";
import { useAsyncWithThrow } from "../hooks/useAsyncWithThrow";
import { aironeApiClient } from "../repository/AironeApiClient";
import { formatAdvancedSearchParams } from "../services/entry/AdvancedSearch";
import { advancedSearchResultPath, topPath } from "Routes";
import { AironeBreadcrumbs } from "components/common/AironeBreadcrumbs";
const StyledFlexBox = styled(Box)({
display: "flex",
justifyContent: "center",
});
const StyledFlexColumnBox = styled(Box)({
display: "flex",
flexDirection: "column",
alignItems: "center",
marginBottom: "48px",
});
const StyledTypography = styled(Typography)({
marginBottom: "16px",
});
export const AdvancedSearchPage: FC = () => {
const [selectedEntities, setSelectedEntities] = useState<Array<EntityList>>(
[]
);
const [selectedAttrs, setSelectedAttrs] = useState<Array<string>>([]);
const [searchAllEntities, setSearchAllEntities] = useState(false);
const [hasReferral, setHasReferral] = useState(false);
const [entityName, setEntityName] = useState("");
const [attrName, setAttrName] = useState("");
const entities = useAsyncWithThrow(async () => {
const entities = await aironeApiClient.getEntities();
return entities.results;
});
const attrs = useAsyncWithThrow(async () => {
if (selectedEntities.length > 0 || searchAllEntities) {
return await aironeApiClient.getEntityAttrs(
selectedEntities.map((e) => e.id),
searchAllEntities
);
}
return [];
}, [selectedEntities, searchAllEntities]);
const searchParams = useMemo(() => {
return formatAdvancedSearchParams({
attrsFilter: Object.fromEntries(
selectedAttrs.map((attr) => [
attr,
{
filterKey: AdvancedSearchResultAttrInfoFilterKeyEnum.CLEARED,
keyword: "",
},
])
),
entityIds: selectedEntities.map((e) => e.id.toString()),
searchAllEntities,
hasReferral,
});
}, [selectedEntities, searchAllEntities, selectedAttrs, hasReferral]);
const handleChangeInputEntityName = (
event: SyntheticEvent,
value: string,
reason: AutocompleteInputChangeReason
) => {
// Not to clear input value on selecting an item
if (reason === "reset") {
return;
}
setEntityName(value);
};
const handleChangeInputAttrName = (
event: SyntheticEvent,
value: string,
reason: AutocompleteInputChangeReason
) => {
// Not to clear input value on selecting an item
if (reason === "reset") {
return;
}
setAttrName(value);
};
return (
<Box className="container-fluid">
<AironeBreadcrumbs>
<Typography component={Link} to={topPath()}>
Top
</Typography>
<Typography color="textPrimary">高度な検索</Typography>
</AironeBreadcrumbs>
<PageHeader title="高度な検索">
<StyledFlexBox>
<Button
variant="contained"
color="secondary"
component={Link}
to={`${advancedSearchResultPath()}?${searchParams}`}
disabled={selectedEntities.length === 0 && !searchAllEntities}
>
検索
</Button>
</StyledFlexBox>
</PageHeader>
<Container>
<StyledFlexColumnBox>
<StyledTypography variant="h4">検索対象のモデル</StyledTypography>
<Autocomplete
options={entities.value ?? []}
getOptionLabel={(option: EntityList) => option.name}
value={selectedEntities}
inputValue={entityName}
disabled={entities.loading}
onChange={(_, value: Array<EntityList>) =>
setSelectedEntities(value)
}
onInputChange={handleChangeInputEntityName}
renderInput={(params) => (
<TextField
{...params}
variant="outlined"
placeholder="モデルを選択"
/>
)}
multiple
disableCloseOnSelect
fullWidth
/>
<Box>
検索対象を絞り込まない
<Checkbox
checked={searchAllEntities}
onChange={(e) => setSearchAllEntities(e.target.checked)}
/>
</Box>
</StyledFlexColumnBox>
<StyledFlexColumnBox>
<StyledTypography variant="h4">属性</StyledTypography>
<AutocompleteWithAllSelector
selectAllLabel="すべて選択"
options={attrs.value ?? []}
value={selectedAttrs}
inputValue={attrName}
disabled={attrs.loading}
onChange={(_, value: Array<string>) => setSelectedAttrs(value)}
onInputChange={handleChangeInputAttrName}
renderInput={(params) => (
<TextField
{...params}
variant="outlined"
placeholder="属性を選択"
/>
)}
multiple
disableCloseOnSelect
fullWidth
/>
<Box>
参照アイテムも含める
<Checkbox
checked={hasReferral}
onChange={(e) => setHasReferral(e.target.checked)}
/>
</Box>
</StyledFlexColumnBox>
</Container>
</Box>
);
};