@blaze-cms/plugin-data-ui 0.146.0-node18.1 → 0.146.0-rc-eagle-3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/CHANGELOG.md +35 -12
  2. package/README.md +64 -0
  3. package/lib/components/EntityDataListing/EntityDataListing.js +21 -19
  4. package/lib/components/EntityDataListing/EntityDataListing.js.map +1 -1
  5. package/lib/components/EntityDataListing/index.js +2 -3
  6. package/lib/components/EntityDataListing/index.js.map +1 -1
  7. package/lib/components/EntityManager/Entity/EntitiyNavLinks/EntityNavLinks.js +18 -20
  8. package/lib/components/EntityManager/Entity/EntitiyNavLinks/EntityNavLinks.js.map +1 -1
  9. package/lib/components/EntityManager/Entity/EntitiyNavLinks/index.js +2 -3
  10. package/lib/components/EntityManager/Entity/EntitiyNavLinks/index.js.map +1 -1
  11. package/lib/components/EntityManager/Entity/Entity.js +290 -452
  12. package/lib/components/EntityManager/Entity/Entity.js.map +1 -1
  13. package/lib/components/EntityManager/Entity/EntityHeader/EntityHeader.js +62 -66
  14. package/lib/components/EntityManager/Entity/EntityHeader/EntityHeader.js.map +1 -1
  15. package/lib/components/EntityManager/Entity/EntityHeader/HeaderPreviewButton/HeaderPreviewButton.js +34 -50
  16. package/lib/components/EntityManager/Entity/EntityHeader/HeaderPreviewButton/HeaderPreviewButton.js.map +1 -1
  17. package/lib/components/EntityManager/Entity/EntityHeader/HeaderPreviewButton/index.js +2 -3
  18. package/lib/components/EntityManager/Entity/EntityHeader/HeaderPreviewButton/index.js.map +1 -1
  19. package/lib/components/EntityManager/Entity/EntityHeader/index.js +2 -3
  20. package/lib/components/EntityManager/Entity/EntityHeader/index.js.map +1 -1
  21. package/lib/components/EntityManager/Entity/SideBarRelations/container/CustomSidebarInfoContainer.js +20 -26
  22. package/lib/components/EntityManager/Entity/SideBarRelations/container/CustomSidebarInfoContainer.js.map +1 -1
  23. package/lib/components/EntityManager/Entity/SideBarRelations/helpers/build-dynamic-query.js +20 -18
  24. package/lib/components/EntityManager/Entity/SideBarRelations/helpers/build-dynamic-query.js.map +1 -1
  25. package/lib/components/EntityManager/Entity/SideBarRelations/hooks/useCustomSidebarData.js +34 -32
  26. package/lib/components/EntityManager/Entity/SideBarRelations/hooks/useCustomSidebarData.js.map +1 -1
  27. package/lib/components/EntityManager/Entity/SideBarRelations/index.js +105 -150
  28. package/lib/components/EntityManager/Entity/SideBarRelations/index.js.map +1 -1
  29. package/lib/components/EntityManager/Entity/SideBarRelations/presentational/CustomSidebarInfo.js +24 -24
  30. package/lib/components/EntityManager/Entity/SideBarRelations/presentational/CustomSidebarInfo.js.map +1 -1
  31. package/lib/components/EntityManager/Entity/actions-handlers/create/index.js +2 -3
  32. package/lib/components/EntityManager/Entity/actions-handlers/create/index.js.map +1 -1
  33. package/lib/components/EntityManager/Entity/actions-handlers/create/submit.js +34 -59
  34. package/lib/components/EntityManager/Entity/actions-handlers/create/submit.js.map +1 -1
  35. package/lib/components/EntityManager/Entity/actions-handlers/delete/delete.js +29 -45
  36. package/lib/components/EntityManager/Entity/actions-handlers/delete/delete.js.map +1 -1
  37. package/lib/components/EntityManager/Entity/actions-handlers/delete/index.js +2 -3
  38. package/lib/components/EntityManager/Entity/actions-handlers/delete/index.js.map +1 -1
  39. package/lib/components/EntityManager/Entity/actions-handlers/index.js +7 -8
  40. package/lib/components/EntityManager/Entity/actions-handlers/index.js.map +1 -1
  41. package/lib/components/EntityManager/Entity/actions-handlers/shared/index.js +2 -3
  42. package/lib/components/EntityManager/Entity/actions-handlers/shared/index.js.map +1 -1
  43. package/lib/components/EntityManager/Entity/actions-handlers/shared/publish.js +36 -57
  44. package/lib/components/EntityManager/Entity/actions-handlers/shared/publish.js.map +1 -1
  45. package/lib/components/EntityManager/Entity/actions-handlers/update/index.js +2 -3
  46. package/lib/components/EntityManager/Entity/actions-handlers/update/index.js.map +1 -1
  47. package/lib/components/EntityManager/Entity/actions-handlers/update/submit.js +47 -66
  48. package/lib/components/EntityManager/Entity/actions-handlers/update/submit.js.map +1 -1
  49. package/lib/components/EntityManager/Entity/index.js +2 -3
  50. package/lib/components/EntityManager/Entity/index.js.map +1 -1
  51. package/lib/components/EntityManager/EntityManager.js +92 -166
  52. package/lib/components/EntityManager/EntityManager.js.map +1 -1
  53. package/lib/components/EntityManager/index.js +2 -3
  54. package/lib/components/EntityManager/index.js.map +1 -1
  55. package/lib/components/EntityManager/utils/RecordEditContext/index.js +3 -4
  56. package/lib/components/EntityManager/utils/RecordEditContext/index.js.map +1 -1
  57. package/lib/components/EntityManager/utils/data-mappers.js +34 -46
  58. package/lib/components/EntityManager/utils/data-mappers.js.map +1 -1
  59. package/lib/components/EntityManager/utils/entity.js +36 -75
  60. package/lib/components/EntityManager/utils/entity.js.map +1 -1
  61. package/lib/components/EntityManager/utils/entityAvailableActions.js +51 -110
  62. package/lib/components/EntityManager/utils/entityAvailableActions.js.map +1 -1
  63. package/lib/components/EntityManager/utils/is-form-empty.js +6 -10
  64. package/lib/components/EntityManager/utils/is-form-empty.js.map +1 -1
  65. package/lib/components/EntityManager/utils/query.js +17 -10
  66. package/lib/components/EntityManager/utils/query.js.map +1 -1
  67. package/lib/components/InfoBoxes/InfoBoxTooltip.js +43 -0
  68. package/lib/components/InfoBoxes/InfoBoxTooltip.js.map +1 -0
  69. package/lib/components/InfoBoxes/InfoBoxes.js +19 -20
  70. package/lib/components/InfoBoxes/InfoBoxes.js.map +1 -1
  71. package/lib/components/InfoBoxes/container/InfoBoxContainer.js +27 -38
  72. package/lib/components/InfoBoxes/container/InfoBoxContainer.js.map +1 -1
  73. package/lib/components/InfoBoxes/helpers/build-dynamic-query.js +21 -17
  74. package/lib/components/InfoBoxes/helpers/build-dynamic-query.js.map +1 -1
  75. package/lib/components/InfoBoxes/hooks/useData.js +33 -30
  76. package/lib/components/InfoBoxes/hooks/useData.js.map +1 -1
  77. package/lib/components/InfoBoxes/hooks/useInfoBox.js +10 -10
  78. package/lib/components/InfoBoxes/hooks/useInfoBox.js.map +1 -1
  79. package/lib/components/InfoBoxes/index.js +2 -3
  80. package/lib/components/InfoBoxes/index.js.map +1 -1
  81. package/lib/components/InfoBoxes/presentational/InfoBox.js +28 -32
  82. package/lib/components/InfoBoxes/presentational/InfoBox.js.map +1 -1
  83. package/lib/components/InfoBoxes/presentational/InfoBoxLabel.js +11 -12
  84. package/lib/components/InfoBoxes/presentational/InfoBoxLabel.js.map +1 -1
  85. package/lib/components/InfoBoxes/presentational/InfoBoxValue.js +12 -12
  86. package/lib/components/InfoBoxes/presentational/InfoBoxValue.js.map +1 -1
  87. package/lib/components/ListingTable/ListingTable.js +164 -303
  88. package/lib/components/ListingTable/ListingTable.js.map +1 -1
  89. package/lib/components/ListingTable/ListingTableContent/ListingTableContent.js +62 -40
  90. package/lib/components/ListingTable/ListingTableContent/ListingTableContent.js.map +1 -1
  91. package/lib/components/ListingTable/ListingTableContent/index.js +2 -3
  92. package/lib/components/ListingTable/ListingTableContent/index.js.map +1 -1
  93. package/lib/components/ListingTable/SearchFilter/RelationMultiSelectFilter.js +182 -0
  94. package/lib/components/ListingTable/SearchFilter/RelationMultiSelectFilter.js.map +1 -0
  95. package/lib/components/ListingTable/SearchFilter/SearchContainer.js +505 -0
  96. package/lib/components/ListingTable/SearchFilter/SearchContainer.js.map +1 -0
  97. package/lib/components/ListingTable/SearchFilter/SearchFilter.js +53 -0
  98. package/lib/components/ListingTable/SearchFilter/SearchFilter.js.map +1 -0
  99. package/lib/components/ListingTable/SearchFilter/helpers.js +138 -0
  100. package/lib/components/ListingTable/SearchFilter/helpers.js.map +1 -0
  101. package/lib/components/ListingTable/SearchFilter/querys.js +13 -0
  102. package/lib/components/ListingTable/SearchFilter/querys.js.map +1 -0
  103. package/lib/components/ListingTable/TableActions/TableActions.js +27 -31
  104. package/lib/components/ListingTable/TableActions/TableActions.js.map +1 -1
  105. package/lib/components/ListingTable/TableActions/index.js +2 -3
  106. package/lib/components/ListingTable/TableActions/index.js.map +1 -1
  107. package/lib/components/ListingTable/index.js +2 -3
  108. package/lib/components/ListingTable/index.js.map +1 -1
  109. package/lib/components/ListingTable/mappers/populate-rows.js +104 -120
  110. package/lib/components/ListingTable/mappers/populate-rows.js.map +1 -1
  111. package/lib/components/ListingTable/service/index.js +66 -84
  112. package/lib/components/ListingTable/service/index.js.map +1 -1
  113. package/lib/components/Tabs/index.js +27 -42
  114. package/lib/components/Tabs/index.js.map +1 -1
  115. package/lib/components/hooks/useCallbackDebounce.js +5 -9
  116. package/lib/components/hooks/useCallbackDebounce.js.map +1 -1
  117. package/lib/constants.js +31 -29
  118. package/lib/constants.js.map +1 -1
  119. package/lib/icons/ContentIcon.js +17 -20
  120. package/lib/icons/ContentIcon.js.map +1 -1
  121. package/lib/icons/SettingsIcon.js +12 -15
  122. package/lib/icons/SettingsIcon.js.map +1 -1
  123. package/lib/icons/TaxonomyIcon.js +15 -18
  124. package/lib/icons/TaxonomyIcon.js.map +1 -1
  125. package/lib/icons/UsersIcon.js +11 -14
  126. package/lib/icons/UsersIcon.js.map +1 -1
  127. package/lib/icons/index.js +8 -9
  128. package/lib/icons/index.js.map +1 -1
  129. package/lib/index.js +88 -121
  130. package/lib/index.js.map +1 -1
  131. package/lib/utils/add-content-menu-items.js +67 -93
  132. package/lib/utils/add-content-menu-items.js.map +1 -1
  133. package/lib/utils/build-create-entity-mutation.js +5 -18
  134. package/lib/utils/build-create-entity-mutation.js.map +1 -1
  135. package/lib/utils/build-delete-entity-mutation.js +11 -8
  136. package/lib/utils/build-delete-entity-mutation.js.map +1 -1
  137. package/lib/utils/build-listing-query.js +20 -17
  138. package/lib/utils/build-listing-query.js.map +1 -1
  139. package/lib/utils/build-update-data-query.js +15 -9
  140. package/lib/utils/build-update-data-query.js.map +1 -1
  141. package/lib/utils/build-update-publish-unpublish-mutation.js +15 -8
  142. package/lib/utils/build-update-publish-unpublish-mutation.js.map +1 -1
  143. package/lib/utils/get-default-query-params.js +23 -10
  144. package/lib/utils/get-default-query-params.js.map +1 -1
  145. package/lib/utils/hoc/withContext.js +4 -6
  146. package/lib/utils/hoc/withContext.js.map +1 -1
  147. package/lib/utils/hooks/useToggle.js +4 -12
  148. package/lib/utils/hooks/useToggle.js.map +1 -1
  149. package/lib/utils/index.js +2 -3
  150. package/lib/utils/index.js.map +1 -1
  151. package/lib-es/components/EntityManager/Entity/Entity.js +23 -30
  152. package/lib-es/components/EntityManager/Entity/Entity.js.map +1 -1
  153. package/lib-es/components/EntityManager/Entity/EntityHeader/HeaderPreviewButton/HeaderPreviewButton.js +6 -1
  154. package/lib-es/components/EntityManager/Entity/EntityHeader/HeaderPreviewButton/HeaderPreviewButton.js.map +1 -1
  155. package/lib-es/components/EntityManager/Entity/SideBarRelations/helpers/build-dynamic-query.js +2 -0
  156. package/lib-es/components/EntityManager/Entity/SideBarRelations/helpers/build-dynamic-query.js.map +1 -1
  157. package/lib-es/components/EntityManager/Entity/SideBarRelations/index.js +26 -7
  158. package/lib-es/components/EntityManager/Entity/SideBarRelations/index.js.map +1 -1
  159. package/lib-es/components/EntityManager/Entity/SideBarRelations/presentational/CustomSidebarInfo.js +8 -2
  160. package/lib-es/components/EntityManager/Entity/SideBarRelations/presentational/CustomSidebarInfo.js.map +1 -1
  161. package/lib-es/components/EntityManager/Entity/actions-handlers/create/submit.js +4 -14
  162. package/lib-es/components/EntityManager/Entity/actions-handlers/create/submit.js.map +1 -1
  163. package/lib-es/components/EntityManager/utils/entity.js +8 -5
  164. package/lib-es/components/EntityManager/utils/entity.js.map +1 -1
  165. package/lib-es/components/InfoBoxes/InfoBoxTooltip.js +36 -0
  166. package/lib-es/components/InfoBoxes/InfoBoxTooltip.js.map +1 -0
  167. package/lib-es/components/InfoBoxes/helpers/build-dynamic-query.js +2 -0
  168. package/lib-es/components/InfoBoxes/helpers/build-dynamic-query.js.map +1 -1
  169. package/lib-es/components/InfoBoxes/presentational/InfoBox.js +4 -1
  170. package/lib-es/components/InfoBoxes/presentational/InfoBox.js.map +1 -1
  171. package/lib-es/components/ListingTable/ListingTable.js +12 -11
  172. package/lib-es/components/ListingTable/ListingTable.js.map +1 -1
  173. package/lib-es/components/ListingTable/ListingTableContent/ListingTableContent.js +34 -16
  174. package/lib-es/components/ListingTable/ListingTableContent/ListingTableContent.js.map +1 -1
  175. package/lib-es/components/ListingTable/SearchFilter/RelationMultiSelectFilter.js +173 -0
  176. package/lib-es/components/ListingTable/SearchFilter/RelationMultiSelectFilter.js.map +1 -0
  177. package/lib-es/components/ListingTable/SearchFilter/SearchContainer.js +496 -0
  178. package/lib-es/components/ListingTable/SearchFilter/SearchContainer.js.map +1 -0
  179. package/lib-es/components/ListingTable/SearchFilter/SearchFilter.js +44 -0
  180. package/lib-es/components/ListingTable/SearchFilter/SearchFilter.js.map +1 -0
  181. package/lib-es/components/ListingTable/SearchFilter/helpers.js +127 -0
  182. package/lib-es/components/ListingTable/SearchFilter/helpers.js.map +1 -0
  183. package/lib-es/components/ListingTable/SearchFilter/querys.js +8 -0
  184. package/lib-es/components/ListingTable/SearchFilter/querys.js.map +1 -0
  185. package/lib-es/components/ListingTable/mappers/populate-rows.js +3 -0
  186. package/lib-es/components/ListingTable/mappers/populate-rows.js.map +1 -1
  187. package/lib-es/components/ListingTable/service/index.js +3 -2
  188. package/lib-es/components/ListingTable/service/index.js.map +1 -1
  189. package/lib-es/components/Tabs/index.js +6 -7
  190. package/lib-es/components/Tabs/index.js.map +1 -1
  191. package/lib-es/constants.js +4 -1
  192. package/lib-es/constants.js.map +1 -1
  193. package/lib-es/index.js +12 -3
  194. package/lib-es/index.js.map +1 -1
  195. package/lib-es/utils/build-create-entity-mutation.js +5 -19
  196. package/lib-es/utils/build-create-entity-mutation.js.map +1 -1
  197. package/lib-es/utils/build-listing-query.js +4 -4
  198. package/lib-es/utils/build-listing-query.js.map +1 -1
  199. package/lib-es/utils/get-default-query-params.js +17 -5
  200. package/lib-es/utils/get-default-query-params.js.map +1 -1
  201. package/lib-es/utils/hoc/withContext.js +1 -1
  202. package/lib-es/utils/hoc/withContext.js.map +1 -1
  203. package/package.json +21 -17
  204. package/src/components/EntityManager/Entity/Entity.js +35 -40
  205. package/src/components/EntityManager/Entity/EntityHeader/HeaderPreviewButton/HeaderPreviewButton.js +8 -1
  206. package/src/components/EntityManager/Entity/SideBarRelations/helpers/build-dynamic-query.js +2 -0
  207. package/src/components/EntityManager/Entity/SideBarRelations/index.js +51 -17
  208. package/src/components/EntityManager/Entity/SideBarRelations/presentational/CustomSidebarInfo.js +5 -3
  209. package/src/components/EntityManager/Entity/actions-handlers/create/submit.js +4 -11
  210. package/src/components/EntityManager/utils/entity.js +11 -6
  211. package/src/components/InfoBoxes/InfoBoxTooltip.js +32 -0
  212. package/src/components/InfoBoxes/helpers/build-dynamic-query.js +2 -0
  213. package/src/components/InfoBoxes/presentational/InfoBox.js +4 -2
  214. package/src/components/ListingTable/ListingTable.js +13 -18
  215. package/src/components/ListingTable/ListingTableContent/ListingTableContent.js +43 -25
  216. package/src/components/ListingTable/SearchFilter/RelationMultiSelectFilter.js +160 -0
  217. package/src/components/ListingTable/SearchFilter/SearchContainer.js +553 -0
  218. package/src/components/ListingTable/SearchFilter/SearchFilter.js +41 -0
  219. package/src/components/ListingTable/SearchFilter/helpers.js +146 -0
  220. package/src/components/ListingTable/SearchFilter/querys.js +9 -0
  221. package/src/components/ListingTable/mappers/populate-rows.js +3 -0
  222. package/src/components/ListingTable/service/index.js +2 -2
  223. package/src/components/Tabs/index.js +17 -21
  224. package/src/constants.js +9 -1
  225. package/src/index.js +11 -7
  226. package/src/utils/build-create-entity-mutation.js +5 -28
  227. package/src/utils/build-listing-query.js +4 -4
  228. package/src/utils/get-default-query-params.js +21 -9
  229. package/src/utils/hoc/withContext.js +5 -4
  230. package/babel.config.js +0 -3
  231. package/jest.config.js +0 -5
@@ -0,0 +1,553 @@
1
+ import React, { useState, useMemo, useEffect, useCallback } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import Select from '@blaze-react/select';
4
+ import MultiSelect from '@blaze-react/multiselect';
5
+ import debounce from 'lodash.debounce';
6
+ import unionBy from 'lodash.unionby';
7
+
8
+ import { useQuery, useApolloClient } from '@apollo/client';
9
+ import {
10
+ getDynamicQuery,
11
+ getQuery,
12
+ fetchFilterByDataFromElastic,
13
+ fetchAllEntities
14
+ } from '@blaze-cms/admin-ui-utils';
15
+ import { getStringTypeProps } from '@blaze-cms/utils';
16
+
17
+ import { BsArrowCounterclockwise, BsSearch } from 'react-icons/bs';
18
+ import { updateListFilters, buildQueryFields, getTidyLabel } from './helpers';
19
+ import RelationMultiSelectFilter from './RelationMultiSelectFilter';
20
+ import { NOOP_QUERY } from './querys';
21
+ import { ENTER_KEY, DEFAULT_FILTER_OPTION_LABEL } from '../../../constants';
22
+
23
+ const SearchContainer = ({
24
+ setListFilters,
25
+ keywordFiltersToUse = [],
26
+ selectFiltersToDisplay = [],
27
+ multiSelectFiltersToDisplay = [],
28
+ schema,
29
+ index,
30
+ values
31
+ }) => {
32
+ const client = useApolloClient();
33
+
34
+ const [searchTerm, setSearchTerm] = useState('');
35
+ const [selectedFilters, setSelectedFilters] = useState({});
36
+ const [selectOptions, setSelectOptions] = useState([]);
37
+ const [multiSelectOptions, setMultiSelectOptions] = useState([]);
38
+ const [allEntitySchemas, setAllEntitySchemas] = useState([]);
39
+
40
+ const displaySearchFilter = keywordFiltersToUse.length > 0;
41
+ const displaySelectFilters = selectFiltersToDisplay.length > 0;
42
+ const displayMultiSelectFilters = multiSelectFiltersToDisplay.length > 0;
43
+ const displayAggregations = displaySelectFilters || displayMultiSelectFilters;
44
+
45
+ useEffect(() => {
46
+ (async () => {
47
+ try {
48
+ const all = await fetchAllEntities({ client });
49
+ setAllEntitySchemas(all || []);
50
+ } catch (error) {
51
+ setAllEntitySchemas([]);
52
+ }
53
+ })();
54
+ }, [client]);
55
+
56
+ const multiSelectPropertySet = useMemo(
57
+ () => new Set(multiSelectFiltersToDisplay.map(([property]) => property)),
58
+ [multiSelectFiltersToDisplay]
59
+ );
60
+
61
+ const relationMultiSelectFilters = useMemo(
62
+ () => multiSelectFiltersToDisplay.filter(([, config]) => config && config.relation),
63
+ [multiSelectFiltersToDisplay]
64
+ );
65
+
66
+ const nonRelationMultiSelectFilters = useMemo(
67
+ () => multiSelectFiltersToDisplay.filter(([, config]) => !config || !config.relation),
68
+ [multiSelectFiltersToDisplay]
69
+ );
70
+
71
+ const { keywordSearchProperties, keywordSearchLabels } = useMemo(() => {
72
+ const labels = [];
73
+ const properties = [];
74
+
75
+ keywordFiltersToUse.forEach(([property, { label }]) => {
76
+ if (label) labels.push(label);
77
+ properties.push(property);
78
+ });
79
+
80
+ return { keywordSearchProperties: properties, keywordSearchLabels: labels };
81
+ }, [keywordFiltersToUse]);
82
+
83
+ const { gqlFields, rawQuery } = useMemo(
84
+ () =>
85
+ buildQueryFields([...selectFiltersToDisplay, ...nonRelationMultiSelectFilters], schema.id),
86
+ [selectFiltersToDisplay, nonRelationMultiSelectFilters, schema.id]
87
+ );
88
+
89
+ const action = displayAggregations
90
+ ? getDynamicQuery('ADMIN_SEARCH')([schema], gqlFields, false, index)
91
+ : NOOP_QUERY;
92
+
93
+ const { data } = useQuery(action, {
94
+ skip: !displayAggregations,
95
+ variables: { where: rawQuery, limit: 0 },
96
+ fetchPolicy: 'cache-and-network'
97
+ });
98
+
99
+ const buildRelationsIndex = useCallback(schemaObj => {
100
+ const items = [];
101
+
102
+ if (schemaObj.properties) {
103
+ Object.entries(schemaObj.properties).forEach(([propName, definition]) => {
104
+ const { relation } = definition || {};
105
+ if (!relation) return;
106
+
107
+ const { localField: relationLocalField, entityIdentifier } = relation;
108
+
109
+ const localField = relationLocalField || propName;
110
+
111
+ if (localField) items.push({ localField, entityIdentifier });
112
+ });
113
+ }
114
+
115
+ if (Array.isArray(schemaObj.relations)) {
116
+ schemaObj.relations.forEach(relation => {
117
+ if (!relation.localField) return;
118
+
119
+ const { localField, entityIdentifier } = relation;
120
+
121
+ items.push({ localField, entityIdentifier });
122
+ });
123
+ }
124
+
125
+ const byKey = new Map();
126
+ items.forEach(item => {
127
+ const key = `${item.localField}|${item.entityIdentifier || ''}`;
128
+ if (!byKey.has(key)) byKey.set(key, item);
129
+ });
130
+
131
+ return Array.from(byKey.values());
132
+ }, []);
133
+
134
+ const relationsIndex = useMemo(() => buildRelationsIndex(schema), [schema, buildRelationsIndex]);
135
+
136
+ const stringTypeProps = useMemo(
137
+ () => getStringTypeProps(schema.id, relationsIndex, allEntitySchemas),
138
+ [schema.id, relationsIndex, allEntitySchemas]
139
+ );
140
+
141
+ const aggregationsData = useMemo(() => {
142
+ const { searchResults: { rawResults: { aggregations = {} } = {} } = {} } = data || {};
143
+ const processed = [];
144
+ Object.entries(aggregations).forEach(([key, { buckets }]) => {
145
+ processed.push(
146
+ Array.isArray(buckets) ? [key, buckets.map(bucket => bucket.key).filter(Boolean)] : []
147
+ );
148
+ });
149
+ return processed;
150
+ }, [data]);
151
+
152
+ const findFilterConfig = useCallback(
153
+ property =>
154
+ selectFiltersToDisplay.find(([prop]) => prop === property)?.[1] ||
155
+ multiSelectFiltersToDisplay.find(([prop]) => prop === property)?.[1],
156
+ [selectFiltersToDisplay, multiSelectFiltersToDisplay]
157
+ );
158
+
159
+ useEffect(() => {
160
+ if (!aggregationsData.length) {
161
+ setSelectOptions([]);
162
+ setMultiSelectOptions([]);
163
+ return;
164
+ }
165
+
166
+ (async () => {
167
+ const results = await Promise.all(
168
+ aggregationsData.map(async ([property, ids]) => {
169
+ try {
170
+ if (!ids || !ids.length) return null;
171
+
172
+ const config = findFilterConfig(property);
173
+ if (!config) return null;
174
+
175
+ const { adminListingOptions: { tidyLabel } = {} } = config;
176
+
177
+ if (!config.relation) {
178
+ const baseOptions = ids.map(value => [value, getTidyLabel(value, tidyLabel)]);
179
+ return {
180
+ property,
181
+ label: config.label,
182
+ type: multiSelectPropertySet.has(property) ? 'multi' : 'single',
183
+ options: baseOptions,
184
+ selected: values && values[property]
185
+ };
186
+ }
187
+
188
+ const schemaQuery = getQuery('GET_ENTITY_SCHEMA');
189
+ const {
190
+ data: {
191
+ getEntitySchemas: [relationSchema]
192
+ }
193
+ } = await client.query({
194
+ query: schemaQuery,
195
+ variables: { identifier: config.relation.entityIdentifier }
196
+ });
197
+
198
+ const {
199
+ data: { result: selected = [] }
200
+ } = await client
201
+ .query({
202
+ query: getDynamicQuery('GET_ALL_ENTITIES')(relationSchema),
203
+ variables: {
204
+ where: { id: { _in: ids } },
205
+ sort: [{ property: 'name', direction: 'asc' }]
206
+ }
207
+ })
208
+ // eslint-disable-next-line no-console
209
+ .catch(error => console.error(error));
210
+
211
+ const baseOptions = selected.map(item => [item.id, getTidyLabel(item.name, tidyLabel)]);
212
+
213
+ return {
214
+ property,
215
+ label: config.label || config.relation.label,
216
+ type: multiSelectPropertySet.has(property) ? 'multi' : 'single',
217
+ options: baseOptions,
218
+ selected: values && values[property]
219
+ };
220
+ } catch {
221
+ return null;
222
+ }
223
+ })
224
+ );
225
+
226
+ const valid = results.filter(Boolean);
227
+
228
+ setSelectOptions(
229
+ valid
230
+ .filter(option => option.type === 'single')
231
+ .map(({ property, label, options, selected }) => ({
232
+ id: property,
233
+ label,
234
+ options,
235
+ selected
236
+ }))
237
+ );
238
+
239
+ setMultiSelectOptions(
240
+ valid
241
+ .filter(option => option.type === 'multi')
242
+ .map(({ property, label, options, selected }) => ({
243
+ id: property,
244
+ label,
245
+ items: options.map(([id, name]) => ({ id, name })),
246
+ selected
247
+ }))
248
+ );
249
+ })();
250
+ }, [client, aggregationsData, findFilterConfig, multiSelectPropertySet, values]);
251
+
252
+ const updateFilters = useCallback(
253
+ (term, filters) => updateListFilters(term, filters, setListFilters, keywordSearchProperties),
254
+ [setListFilters, keywordSearchProperties]
255
+ );
256
+
257
+ const debouncedUpdateFilters = useMemo(
258
+ () => debounce((term, filters) => updateFilters(term, filters), 300),
259
+ [updateFilters]
260
+ );
261
+
262
+ useEffect(() => () => debouncedUpdateFilters.cancel(), [debouncedUpdateFilters]);
263
+
264
+ const handleSearch = useCallback(
265
+ () => updateFilters(searchTerm, selectedFilters),
266
+ [searchTerm, selectedFilters, updateFilters]
267
+ );
268
+
269
+ const handleSearchChange = useCallback(({ target: { value } }) => setSearchTerm(value), []);
270
+
271
+ const handleEnterKey = useCallback(
272
+ ({ key }) => {
273
+ if (key === ENTER_KEY) handleSearch();
274
+ },
275
+ [handleSearch]
276
+ );
277
+
278
+ const handleReset = useCallback(() => {
279
+ setSearchTerm('');
280
+ debouncedUpdateFilters.cancel();
281
+ setSelectedFilters(prev =>
282
+ Object.keys(prev).reduce((acc, key) => {
283
+ acc[key] = multiSelectPropertySet.has(key) ? [] : DEFAULT_FILTER_OPTION_LABEL;
284
+ return acc;
285
+ }, {})
286
+ );
287
+ updateFilters('', {});
288
+ }, [multiSelectPropertySet, updateFilters, debouncedUpdateFilters]);
289
+
290
+ const handleSelect = useCallback(
291
+ (property, { value }) => {
292
+ const next = { ...selectedFilters, [property]: value };
293
+ setSelectedFilters(next);
294
+ debouncedUpdateFilters(searchTerm, next);
295
+ },
296
+ [searchTerm, selectedFilters, debouncedUpdateFilters]
297
+ );
298
+
299
+ const handleMultiSelect = useCallback(
300
+ (property, ids) => {
301
+ const next = { ...selectedFilters, [property]: ids };
302
+ setSelectedFilters(next);
303
+ debouncedUpdateFilters(searchTerm, next);
304
+ },
305
+ [searchTerm, selectedFilters, debouncedUpdateFilters]
306
+ );
307
+
308
+ const findRelationByLocalField = useCallback(
309
+ localField => relationsIndex.find(relation => relation.localField === localField),
310
+ [relationsIndex]
311
+ );
312
+
313
+ const resolveAggProp = useCallback((property, config) => {
314
+ if (!config.relation) return property;
315
+ if (property.includes('.')) return property;
316
+ const localField = config.relation.localField || property;
317
+ return `${localField}.name`;
318
+ }, []);
319
+
320
+ const resolveEntityIdentifier = useCallback(
321
+ (property, config) => {
322
+ if (!config.relation) return undefined;
323
+ if (config.relation.entityIdentifier) return config.relation.entityIdentifier;
324
+
325
+ const localField = property.includes('.')
326
+ ? property.split('.')[0]
327
+ : config.relation.localField || property;
328
+
329
+ return findRelationByLocalField(localField).entityIdentifier;
330
+ },
331
+ [findRelationByLocalField]
332
+ );
333
+
334
+ const addEntitySuffixIfNeeded = useCallback((aggProp, entityIdentifier, config) => {
335
+ if (!config.relation || !entityIdentifier) return aggProp;
336
+ return `${aggProp}/${entityIdentifier}`;
337
+ }, []);
338
+
339
+ const makeOnSearchChange = useCallback(
340
+ property =>
341
+ async ({ event: { value }, currentOffset }) => {
342
+ try {
343
+ if (!value || currentOffset) return [];
344
+
345
+ const config = findFilterConfig(property);
346
+ if (!config) return [];
347
+
348
+ let aggProp = resolveAggProp(property, config);
349
+
350
+ if (config.relation && !config.relation.localField && !property.includes('.')) {
351
+ const matchingRelation = findRelationByLocalField(property);
352
+ if (matchingRelation.localField) aggProp = `${matchingRelation.localField}.name`;
353
+ }
354
+
355
+ const entityIdentifier =
356
+ resolveEntityIdentifier(property, config) || config.relation?.entityIdentifier;
357
+
358
+ const filterByEntry = addEntitySuffixIfNeeded(aggProp, entityIdentifier, config);
359
+
360
+ const currentValues = {
361
+ entity: schema.id,
362
+ filterByProperty: [filterByEntry]
363
+ };
364
+
365
+ const aggregations = await fetchFilterByDataFromElastic(
366
+ client,
367
+ currentValues,
368
+ relationsIndex,
369
+ stringTypeProps,
370
+ value
371
+ );
372
+
373
+ const key = aggProp;
374
+ const buckets = aggregations?.[key]?.buckets || [];
375
+
376
+ let items = [];
377
+ if (config.relation) {
378
+ const names = buckets.map(bucket => bucket.key).filter(Boolean);
379
+ if (names.length) {
380
+ const schemaQuery = getQuery('GET_ENTITY_SCHEMA');
381
+ const {
382
+ data: {
383
+ getEntitySchemas: [relationSchema]
384
+ }
385
+ } = await client.query({
386
+ query: schemaQuery,
387
+ variables: { identifier: entityIdentifier }
388
+ });
389
+
390
+ const {
391
+ data: { result: selected = [] }
392
+ } = await client
393
+ .query({
394
+ query: getDynamicQuery('GET_ALL_ENTITIES')(relationSchema),
395
+ variables: {
396
+ where: { name: { _in: names } },
397
+ sort: [{ property: 'name', direction: 'asc' }]
398
+ }
399
+ })
400
+ // eslint-disable-next-line no-console
401
+ .catch(error => console.error(error));
402
+
403
+ items = selected.map(item => ({ id: item.id, name: item.name }));
404
+ }
405
+ } else {
406
+ items = buckets
407
+ .map(bucket => ({ id: bucket.key, name: bucket.key }))
408
+ .filter(option => option.id != null);
409
+ }
410
+
411
+ const existingItems = multiSelectOptions.find(opt => opt.id === property)?.items || [];
412
+ return unionBy(items, existingItems, 'id');
413
+ } catch (error) {
414
+ return [];
415
+ }
416
+ },
417
+ [
418
+ client,
419
+ schema.id,
420
+ relationsIndex,
421
+ stringTypeProps,
422
+ multiSelectOptions,
423
+ findFilterConfig,
424
+ resolveAggProp,
425
+ resolveEntityIdentifier,
426
+ addEntitySuffixIfNeeded,
427
+ findRelationByLocalField
428
+ ]
429
+ );
430
+
431
+ const placeholderText = `Search ${keywordSearchLabels.join(', ')}`;
432
+
433
+ return (
434
+ <div className="search-container ms-filter">
435
+ {displaySearchFilter && (
436
+ <div className="search-container__search-input-container">
437
+ <input
438
+ type="search"
439
+ className="search-container__search-input-container__search-input"
440
+ placeholder={placeholderText}
441
+ value={searchTerm}
442
+ onChange={handleSearchChange}
443
+ onKeyDown={handleEnterKey}
444
+ />
445
+ <i role="button" onClick={handleSearch}>
446
+ <BsSearch />
447
+ </i>
448
+ </div>
449
+ )}
450
+
451
+ {displaySelectFilters &&
452
+ selectOptions.map(({ id, options, label, selected }) =>
453
+ options && options.length ? (
454
+ <div className="search-container__select-wrapper" key={`select-${id}`}>
455
+ <Select
456
+ label={label}
457
+ id={id}
458
+ name={id}
459
+ onChange={({ value }) => handleSelect(id, { value })}
460
+ options={options}
461
+ value={selectedFilters[id] !== undefined ? selectedFilters[id] : selected}
462
+ defaultTextValue={DEFAULT_FILTER_OPTION_LABEL}
463
+ />
464
+ </div>
465
+ ) : null
466
+ )}
467
+
468
+ {displayMultiSelectFilters &&
469
+ multiSelectOptions.map(({ id, label, items }) => {
470
+ const selectedIds = selectedFilters[id] ? selectedFilters[id] : [];
471
+ const multiSelectData = {
472
+ filterBy: ['name'],
473
+ identification: 'id',
474
+ keyValue: 'name',
475
+ data: items.map(({ id: itemId, name }) => ({
476
+ id: itemId,
477
+ name,
478
+ description: '',
479
+ checked: selectedIds.includes(itemId),
480
+ show: true
481
+ }))
482
+ };
483
+
484
+ return (
485
+ <div className="search-container__select-wrapper" key={`multiselect-${id}`}>
486
+ <MultiSelect
487
+ onChange={({ value, currentOffset }) =>
488
+ makeOnSearchChange(id)({ event: { value }, currentOffset })
489
+ }
490
+ name={id}
491
+ label={label}
492
+ data={multiSelectData}
493
+ limit={10}
494
+ getSelected={({
495
+ event: {
496
+ target: { value }
497
+ }
498
+ }) => handleMultiSelect(id, value)}
499
+ required={false}
500
+ checkedPreviewCount={1}
501
+ formatMoreLabel={n => `${n} more`}
502
+ />
503
+ </div>
504
+ );
505
+ })}
506
+
507
+ {relationMultiSelectFilters.map(([property, config]) => {
508
+ const entityIdentifier =
509
+ resolveEntityIdentifier(property, config) || config.relation?.entityIdentifier;
510
+
511
+ if (!entityIdentifier) return null;
512
+
513
+ return (
514
+ <div
515
+ className="search-container__select-wrapper"
516
+ key={`relation-multiselect-${property}`}>
517
+ <RelationMultiSelectFilter
518
+ id={property}
519
+ label={config.label || config.relation.label}
520
+ entityIdentifier={entityIdentifier}
521
+ selectedIds={selectedFilters[property] || []}
522
+ onChange={handleMultiSelect}
523
+ />
524
+ </div>
525
+ );
526
+ })}
527
+
528
+ <div className="search-container__reset-button">
529
+ <span role="button" onClick={handleReset}>
530
+ <BsArrowCounterclockwise />
531
+ </span>
532
+ </div>
533
+ </div>
534
+ );
535
+ };
536
+
537
+ SearchContainer.propTypes = {
538
+ setListFilters: PropTypes.func.isRequired,
539
+ keywordFiltersToUse: PropTypes.array,
540
+ selectFiltersToDisplay: PropTypes.array,
541
+ multiSelectFiltersToDisplay: PropTypes.array,
542
+ schema: PropTypes.object.isRequired,
543
+ index: PropTypes.string
544
+ };
545
+
546
+ SearchContainer.defaultProps = {
547
+ keywordFiltersToUse: [],
548
+ selectFiltersToDisplay: [],
549
+ multiSelectFiltersToDisplay: [],
550
+ index: 'admin'
551
+ };
552
+
553
+ export default SearchContainer;
@@ -0,0 +1,41 @@
1
+ import React, { useMemo } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { getKeywordSearchFilters, findSelectFilters, findMultiSelectFilters } from './helpers';
4
+ import SearchContainer from './SearchContainer';
5
+
6
+ const SearchFilter = ({ schema, setListFilters, values }) => {
7
+ const { displayProperties = {} } = schema || {};
8
+ const {
9
+ adminListings: { dataSource: { source, index } = {}, disableListingFilter = false } = {}
10
+ } = displayProperties;
11
+
12
+ const keywordFiltersToUse = useMemo(() => getKeywordSearchFilters(schema), [schema]);
13
+ const selectFiltersToDisplay = useMemo(() => findSelectFilters(schema), [schema]);
14
+ const multiSelectFiltersToDisplay = useMemo(() => findMultiSelectFilters(schema), [schema]);
15
+
16
+ if (disableListingFilter) return null;
17
+
18
+ const canSearch =
19
+ source === 'search' && (keywordFiltersToUse.length > 0 || selectFiltersToDisplay.length > 0);
20
+ if (!canSearch) return null;
21
+
22
+ return (
23
+ <SearchContainer
24
+ index={index}
25
+ key={schema.id}
26
+ setListFilters={setListFilters}
27
+ keywordFiltersToUse={keywordFiltersToUse}
28
+ selectFiltersToDisplay={selectFiltersToDisplay}
29
+ multiSelectFiltersToDisplay={multiSelectFiltersToDisplay}
30
+ schema={schema}
31
+ values={values}
32
+ />
33
+ );
34
+ };
35
+
36
+ SearchFilter.propTypes = {
37
+ setListFilters: PropTypes.func.isRequired,
38
+ schema: PropTypes.object.isRequired
39
+ };
40
+
41
+ export default SearchFilter;