-
Notifications
You must be signed in to change notification settings - Fork 590
/
InventoryEntitiesTypeahead.js
206 lines (191 loc) · 5.57 KB
/
InventoryEntitiesTypeahead.js
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
201
202
203
204
205
206
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/
import type {InventoryEntitiesTypeaheadQuery} from './__generated__/InventoryEntitiesTypeaheadQuery.graphql';
import type {Suggestion} from '@fbcnms/ui/components/Typeahead';
import type {Theme, WithStyles} from '@material-ui/core';
import * as React from 'react';
import EquipmentBreadcrumbs from './equipment/EquipmentBreadcrumbs';
import LocationBreadcrumbsTitle from './location/LocationBreadcrumbsTitle';
import RelayEnvironment from '../common/RelayEnvironment.js';
import Text from '@fbcnms/ui/components/design-system/Text';
import Typeahead from '@fbcnms/ui/components/Typeahead';
import {NAVIGATION_OPTIONS} from './location/LocationBreadcrumbsTitle';
import {debounce} from 'lodash';
import {fetchQuery, graphql} from 'relay-runtime';
import {withStyles} from '@material-ui/core/styles';
const inventoryEntitiesTypeaheadQuery = graphql`
query InventoryEntitiesTypeaheadQuery($name: String!) {
searchForNode(name: $name, first: 10) {
edges {
node {
__typename
... on Location {
id
externalId
name
locationType {
name
}
locationHierarchy {
id
name
locationType {
name
}
}
}
... on Equipment {
id
externalId
name
equipmentType {
name
}
...EquipmentBreadcrumbs_equipment
}
}
}
}
}
`;
const styles = (theme: Theme) => ({
container: {
minWidth: '250px',
},
suggestionRoot: {
display: 'flex',
},
suggestionType: {
color: theme.palette.text.secondary,
fontSize: theme.typography.pxToRem(13),
lineHeight: '21px',
marginLeft: theme.spacing(),
},
breadcrumbsContainer: {
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
maxWidth: '600px',
overflow: 'hidden',
},
externalId: {
marginLeft: '6px',
},
});
type EntityType = 'location' | 'equipment';
type Props = {
onEntitySelected: (entityId: string, entityType: EntityType) => void,
} & WithStyles<typeof styles>;
type State = {
suggestions: Array<Suggestion>,
};
const SEARCH_DEBOUNCE_TIMEOUT_MS = 200;
class InventoryEntitiesTypeahead extends React.Component<Props, State> {
_debounceFetchSuggestions = debounce(
(searchTerm: string) => this.fetchNewSuggestions(searchTerm),
SEARCH_DEBOUNCE_TIMEOUT_MS,
{
trailing: true,
leading: true,
},
);
state = {
suggestions: [],
};
fetchNewSuggestions(searchTerm: string) {
const {classes} = this.props;
fetchQuery<InventoryEntitiesTypeaheadQuery>(
RelayEnvironment,
inventoryEntitiesTypeaheadQuery,
{
name: searchTerm,
},
).then(response => {
if (!response || !response.searchForNode) {
return;
}
const mapToSuggestion = (node): ?Suggestion => {
if (node.__typename === 'Equipment') {
return {
entityId: node.id,
entityType: 'equipment',
name: node.name,
type: node.equipmentType.name,
render: () => {
return (
<div className={classes.breadcrumbsContainer}>
<EquipmentBreadcrumbs equipment={node} size="small" />
</div>
);
},
};
} else if (node.__typename === 'Location') {
return {
entityId: node.id,
entityType: 'location',
name: node.name,
type: node.locationType.name,
render: () => (
<div className={classes.breadcrumbsContainer}>
<LocationBreadcrumbsTitle
locationDetails={node}
size="small"
hideTypes={true}
navigateOnClick={NAVIGATION_OPTIONS.NONE}
/>
{node.externalId && (
<Text
variant="caption"
color="gray"
className={classes.externalId}>
({node.externalId})
</Text>
)}
</div>
),
};
}
return (null: ?Suggestion);
};
const suggestions: Array<Suggestion> = (
response.searchForNode.edges?.map(edge => {
if (edge.node == null) {
return null;
}
return mapToSuggestion(edge.node);
}) ?? []
).filter(Boolean);
this.setState({suggestions});
});
}
onSuggestionsFetchRequested = searchTerm => {
this._debounceFetchSuggestions(searchTerm);
};
render() {
const {classes, onEntitySelected} = this.props;
const {suggestions} = this.state;
return (
<div className={classes.container}>
<Typeahead
required={false}
suggestions={suggestions}
getSuggestionValue={(suggestion: Suggestion) => suggestion.name}
onSuggestionsFetchRequested={this.onSuggestionsFetchRequested}
onEntitySelected={suggestion => {
const entityType =
suggestion.entityType === 'location' ? 'location' : 'equipment';
onEntitySelected(suggestion.entityId, entityType);
}}
/>
</div>
);
}
}
export default withStyles(styles)(InventoryEntitiesTypeahead);