/
getTagItems.ts
70 lines (66 loc) · 2.22 KB
/
getTagItems.ts
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
// import { IHubSearchOptions } from "../../../search/types/IHubSearchOptions";
// import { IQuery } from "../../../search/types/IHubCatalog";
import { ISearchOptions, searchItems } from "@esri/arcgis-rest-portal";
import { IHubRequestOptions } from "../../../types";
import { IUiSchemaComboboxItem } from "../types";
import { EntityEditorOptions } from "./EditorOptions";
/**
* Fetch the entity's org tags (limited to the top 200),
* merge with any configured on the entity itself, and convert
* into a format that can be consumed by the combobox field
*
* TODO: the search portion of this util should probably
* be hoisted to hub.js
*/
export async function getTagItems(
tags: string[],
orgId: string,
hubRequestOptions: IHubRequestOptions
): Promise<IUiSchemaComboboxItem[]> {
// TODO: Once we resolve issues consuming hubSearch in Hub.js we can swap back to this approach
// const query: IQuery = {
// targetEntity: "item",
// filters: [{ predicates: [{ orgid: orgId }] }],
// };
// const opts: IHubSearchOptions = {
// aggFields: ["tags"],
// num: 0,
// aggLimit: 200,
// requestOptions: hubRequestOptions,
// };
// const {
// aggregations: [tagsAgg],
// } = await hubSearch(query, opts);
// const {
// aggregations: [tagsAgg],
// } = await Promise.resolve({
// aggregations: [{ values: [{ value: "test" }] }],
// });
try {
const so: ISearchOptions = {
q: `orgid:${orgId}`,
countFields: "tags",
countSize: 200,
authentication: hubRequestOptions.authentication,
};
const response = await searchItems(so);
const [tagsAgg] = response.aggregations.counts.map((entry) => {
return {
mode: "terms",
field: entry.fieldName,
values: entry.fieldValues,
};
});
/**
* Because we allow custom tags, we need to merge the existing entity tags
* and the tags fetched from the orgs, then remove duplicates, filter out
* any empty values and convert them to the IUiSchemaComboboxItem format
*/
const entityTags = tags || [];
return [...new Set([...tagsAgg.values.map((t) => t.value), ...entityTags])]
.filter((t) => t)
.map((t) => ({ value: t }));
} catch (e) {
return [];
}
}