@bigbinary/neeto-filters-frontend 4.3.31 → 4.3.33

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 (80) hide show
  1. package/README.md +0 -2
  2. package/app/javascript/src/translations/hu.json +80 -0
  3. package/dist/.ready +1 -0
  4. package/dist/Bar.js +9 -22
  5. package/dist/Bar.js.map +1 -1
  6. package/dist/Filters.js +6 -9
  7. package/dist/Filters.js.map +1 -1
  8. package/dist/Segments.js +9 -8
  9. package/dist/Segments.js.map +1 -1
  10. package/dist/cjs/Bar.js +11 -24
  11. package/dist/cjs/Bar.js.map +1 -1
  12. package/dist/cjs/Filters.js +6 -9
  13. package/dist/cjs/Filters.js.map +1 -1
  14. package/dist/cjs/Segments.js +6 -5
  15. package/dist/cjs/Segments.js.map +1 -1
  16. package/dist/cjs/hooks.js +1 -1
  17. package/dist/cjs/index.js +7 -4
  18. package/dist/cjs/index.js.map +1 -1
  19. package/dist/cjs/utils.js +2 -1
  20. package/dist/cjs/utils.js.map +1 -1
  21. package/dist/cjs/v2/Bar.js +673 -0
  22. package/dist/cjs/v2/Bar.js.map +1 -0
  23. package/dist/cjs/v2/Filters.js +1282 -0
  24. package/dist/cjs/v2/Filters.js.map +1 -0
  25. package/dist/cjs/v2/Segments.js +532 -0
  26. package/dist/cjs/v2/Segments.js.map +1 -0
  27. package/dist/cjs/v2/index.js +70 -0
  28. package/dist/cjs/v2/index.js.map +1 -0
  29. package/dist/cjs/v2/utils.js +70 -0
  30. package/dist/cjs/v2/utils.js.map +1 -0
  31. package/dist/dayjs-9rsrbiYS.js +6 -0
  32. package/dist/dayjs-9rsrbiYS.js.map +1 -0
  33. package/dist/dayjs-DNGB9yJD.js +4 -0
  34. package/dist/dayjs-DNGB9yJD.js.map +1 -0
  35. package/dist/hooks.js +1 -1
  36. package/dist/{index-Bq32eLIG.js → index-00KCfktU.js} +4 -4
  37. package/dist/{index-Bq32eLIG.js.map → index-00KCfktU.js.map} +1 -1
  38. package/dist/{index-Rn6zFKPg.js → index-4MKjSNsp.js} +28 -27
  39. package/dist/{index-Rn6zFKPg.js.map → index-4MKjSNsp.js.map} +1 -1
  40. package/dist/{index-CzUm_HaL.js → index-BiqUQPwR.js} +11 -10
  41. package/dist/{index-CzUm_HaL.js.map → index-BiqUQPwR.js.map} +1 -1
  42. package/dist/index-BzkSk3Jh.js +5 -0
  43. package/dist/index-BzkSk3Jh.js.map +1 -0
  44. package/dist/{index-BYRIatjo.js → index-D5wjkRUM.js} +4 -4
  45. package/dist/{index-BYRIatjo.js.map → index-D5wjkRUM.js.map} +1 -1
  46. package/dist/index-DX0qF4mf.js +7 -0
  47. package/dist/index-DX0qF4mf.js.map +1 -0
  48. package/dist/index.js +7 -4
  49. package/dist/index.js.map +1 -1
  50. package/dist/keyboardShortcuts-DcgoM1dR.js +20 -0
  51. package/dist/keyboardShortcuts-DcgoM1dR.js.map +1 -0
  52. package/dist/keyboardShortcuts-oiJyIaFr.js +18 -0
  53. package/dist/keyboardShortcuts-oiJyIaFr.js.map +1 -0
  54. package/dist/{useSegmentsApi-wYy0UPCH.js → useSegmentsApi-0Le3BZOo.js} +3 -3
  55. package/dist/{useSegmentsApi-wYy0UPCH.js.map → useSegmentsApi-0Le3BZOo.js.map} +1 -1
  56. package/dist/{useSegmentsApi-CDE1HWEv.js → useSegmentsApi-DlvMy4A9.js} +2 -2
  57. package/dist/{useSegmentsApi-CDE1HWEv.js.map → useSegmentsApi-DlvMy4A9.js.map} +1 -1
  58. package/dist/utils-ClTYBS1C.js +865 -0
  59. package/dist/utils-ClTYBS1C.js.map +1 -0
  60. package/dist/{utils-BuCOQsNv.js → utils-CzRCzZ3I.js} +3 -4
  61. package/dist/utils-CzRCzZ3I.js.map +1 -0
  62. package/dist/utils-Dqmvcb8M.js +802 -0
  63. package/dist/utils-Dqmvcb8M.js.map +1 -0
  64. package/dist/{utils-CxVWpPGf.js → utils-l0QK_DBo.js} +7 -8
  65. package/dist/utils-l0QK_DBo.js.map +1 -0
  66. package/dist/utils.js +2 -1
  67. package/dist/utils.js.map +1 -1
  68. package/dist/v2/Bar.js +671 -0
  69. package/dist/v2/Bar.js.map +1 -0
  70. package/dist/v2/Filters.js +1280 -0
  71. package/dist/v2/Filters.js.map +1 -0
  72. package/dist/v2/Segments.js +530 -0
  73. package/dist/v2/Segments.js.map +1 -0
  74. package/dist/v2/index.js +54 -0
  75. package/dist/v2/index.js.map +1 -0
  76. package/dist/v2/utils.js +12 -0
  77. package/dist/v2/utils.js.map +1 -0
  78. package/package.json +43 -30
  79. package/dist/utils-BuCOQsNv.js.map +0 -1
  80. package/dist/utils-CxVWpPGf.js.map +0 -1
package/dist/v2/Bar.js ADDED
@@ -0,0 +1,671 @@
1
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
+ import { Fragment as Fragment$1, useRef, useState, useEffect } from 'react';
3
+ import { useQueryClient } from '@tanstack/react-query';
4
+ import classnames from 'classnames';
5
+ import { t } from 'i18next';
6
+ import { findBy, removeBy, humanize, hyphenate, isPresent, isNotEmpty, noop, findById, isNotPresent } from '@bigbinary/neeto-cist';
7
+ import { useQueryParams } from '@bigbinary/neeto-commons-frontend/react-utils';
8
+ import { reject, sortBy, prop, project, modify, has, omit, isEmpty, identity, assoc } from 'ramda';
9
+ import { S as SegmentForm } from '../index-00KCfktU.js';
10
+ import { c as useCreateSegment, b as useUpdateSegment, Q as QUERY_KEYS } from '../useSegmentsApi-0Le3BZOo.js';
11
+ import { Tooltip, Typography, Button, DropdownMenu } from '@bigbinary/neeto-atoms';
12
+ import { dateFormat, dayjs, buildUrl, showThumbsUpToastr } from '@bigbinary/neeto-commons-frontend/utils';
13
+ import { useTranslation } from 'react-i18next';
14
+ import { useHistory } from 'react-router-dom';
15
+ import { S as SEARCH_TERM_KEY, d as SINGLE_DATE_OPTIONS, e as SINGLE_TIME_OPTIONS } from '../constants-8DtAPJaA.js';
16
+ import { XIcon } from 'lucide-react';
17
+ import useHotkeys from '@bigbinary/neeto-hotkeys';
18
+ import { i as isRuleEmpty } from '../index-4MKjSNsp.js';
19
+ import { S as SHORTCUTS } from '../keyboardShortcuts-oiJyIaFr.js';
20
+ import { i as isDate, f as isCustomDateRange, g as isTime, h as isCustomTimeRange, j as isMulti, k as isTreeSelect, l as isSingleDate, m as isSingleTime, n as isBoolean, o as getLabelFromBool, p as isText, T as TEXT_RULES, q as isDigit, N as NUMBER_RULES, r as isSingle, s as getSnakeCasedQueryParams, t as isKeywordFilterPresent } from '../utils-Dqmvcb8M.js';
21
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
22
+ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
23
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
24
+ import { f as buildFiltersFromSearchParams, i as isKeywordFilterPresent$1, d as buildUrlFromFilters, g as getSearchTermFromUrl, B as buildSegmentsPayload, e as buildUrlFromSegment, j as getSnakeCasedQueryParams$1 } from '../utils-l0QK_DBo.js';
25
+ import '@bigbinary/neeto-icons/Delete';
26
+ import '@bigbinary/neeto-molecules/HelpPopover';
27
+ import '@bigbinary/neetoui/Accordion';
28
+ import '@bigbinary/neetoui/Button';
29
+ import '@bigbinary/neetoui/Pane';
30
+ import '@bigbinary/neetoui/Typography';
31
+ import '@bigbinary/neetoui/formik/ActionBlock';
32
+ import '@bigbinary/neetoui/formik/Form';
33
+ import '@bigbinary/neetoui/formik/Input';
34
+ import 'react-router-dom/cjs/react-router-dom.min';
35
+ import '@bigbinary/neeto-icons/Plus';
36
+ import '@bigbinary/neetoui/Select';
37
+ import 'yup';
38
+ import 'axios';
39
+ import '../dayjs-DNGB9yJD.js';
40
+ import '@bigbinary/neetoui/DatePicker';
41
+ import '@bigbinary/neetoui/TimePicker';
42
+ import '@bigbinary/neetoui/Input';
43
+ import '@bigbinary/neetoui/TreeSelect';
44
+ import '@bigbinary/neeto-icons/Help';
45
+
46
+ var DEFAULT_SEGMENTS_CACHE = {
47
+ segments: []
48
+ };
49
+ var SEGMENT_ACTIONS = {
50
+ EDIT: "edit",
51
+ CREATE: "create"
52
+ };
53
+
54
+ var buildCustomDateValue = function buildCustomDateValue(value) {
55
+ var isBeforeOrAfter = Math.abs(dayjs(value[1]).diff(dayjs(value[0]), "year")) >= 90;
56
+ if (isBeforeOrAfter) {
57
+ var currentMoment = dayjs();
58
+ var diffWithStart = Math.abs(currentMoment.diff(dayjs(value[0]), "year"));
59
+ var diffWithEnd = Math.abs(currentMoment.diff(dayjs(value[1]), "year"));
60
+ return diffWithStart < diffWithEnd ? "after-".concat(dateFormat.date(dayjs(value[0]).subtract(1, "day"))) : "before-".concat(dateFormat.date(dayjs(value[1]).add(1, "day")));
61
+ }
62
+ return value.map(dateFormat.date).join(" - ");
63
+ };
64
+ var buildFilterValue = function buildFilterValue(filter) {
65
+ var _findBy2;
66
+ var _filter$defaultType = filter.defaultType,
67
+ defaultType = _filter$defaultType === void 0 ? filter.type : _filter$defaultType,
68
+ value = filter.value,
69
+ rule = filter.rule,
70
+ _filter$values = filter.values,
71
+ initialValues = _filter$values === void 0 ? [] : _filter$values;
72
+ var values = initialValues.map(modify("value", function (value) {
73
+ return value.toString();
74
+ }));
75
+ if (isDate(defaultType) && isCustomDateRange(value)) {
76
+ return buildCustomDateValue(value);
77
+ } else if (isTime(defaultType) && isCustomTimeRange(value)) {
78
+ return value.map(dateFormat.time).join(" - ");
79
+ } else if (isDate(defaultType) || isTime(defaultType)) {
80
+ return humanize(value);
81
+ } else if (isMulti(defaultType) || isTreeSelect(defaultType)) {
82
+ return value.map(function (value) {
83
+ var _findBy;
84
+ return ((_findBy = findBy({
85
+ value: value
86
+ }, values)) === null || _findBy === void 0 ? void 0 : _findBy.label) || value;
87
+ }).join(", ");
88
+ } else if (isSingleDate(defaultType)) {
89
+ return rule === "between" ? value.map(dateFormat.date).join(" - ") : dateFormat.date(value);
90
+ } else if (isSingleTime(defaultType)) {
91
+ return rule === "between" ? value.map(dateFormat.time).join(" - ") : dateFormat.time(value);
92
+ } else if (isBoolean(defaultType)) {
93
+ return getLabelFromBool({
94
+ value: value,
95
+ values: values
96
+ });
97
+ }
98
+ return ((_findBy2 = findBy({
99
+ value: value
100
+ }, values)) === null || _findBy2 === void 0 ? void 0 : _findBy2.label) || value;
101
+ };
102
+ var buildRuleValue = function buildRuleValue(_ref) {
103
+ var rule = _ref.rule,
104
+ defaultType = _ref.defaultType;
105
+ try {
106
+ if (isText(defaultType)) {
107
+ return findBy({
108
+ value: rule
109
+ }, TEXT_RULES).label;
110
+ } else if (isDigit(defaultType)) {
111
+ return findBy({
112
+ value: rule
113
+ }, NUMBER_RULES).label;
114
+ } else if (isSingleDate(defaultType)) {
115
+ return findBy({
116
+ value: rule
117
+ }, SINGLE_DATE_OPTIONS).label;
118
+ } else if (isSingleTime(defaultType)) {
119
+ return findBy({
120
+ value: rule
121
+ }, SINGLE_TIME_OPTIONS).label;
122
+ } else if (isMulti(defaultType) || isSingle(defaultType) || isTreeSelect(defaultType)) {
123
+ return isRuleEmpty(rule) ? t("neetoFilters.rules.isEmpty") : "";
124
+ }
125
+ } catch (_unused) {
126
+ // This block is to handle the cases where the user modifies the URL and adds an invalid rule.
127
+ }
128
+ return "";
129
+ };
130
+ var hasOnlyKeywordFilter = function hasOnlyKeywordFilter(filters) {
131
+ return filters.length === 1 && filters[0].key === SEARCH_TERM_KEY;
132
+ };
133
+ var deepCompare = function deepCompare(segments, filters) {
134
+ var comparisonHelper = function comparisonHelper(filters) {
135
+ return sortBy(prop("key"), removeBy({
136
+ key: SEARCH_TERM_KEY
137
+ }, project(["key", "rule", "value", "label"], filters)));
138
+ };
139
+ return reject(function (filters) {
140
+ var segment = findBy({
141
+ key: filters.key
142
+ }, segments);
143
+ return segment && segment.value === filters.value;
144
+ }, comparisonHelper(filters));
145
+ };
146
+
147
+ var Block = function Block(_ref) {
148
+ var filter = _ref.filter,
149
+ handleDelete = _ref.handleDelete,
150
+ setIsPaneOpen = _ref.setIsPaneOpen;
151
+ var key = filter.key,
152
+ _filter$defaultType = filter.defaultType,
153
+ defaultType = _filter$defaultType === void 0 ? filter.type : _filter$defaultType,
154
+ label = filter.label,
155
+ rule = filter.rule,
156
+ defaultValue = filter.defaultValue;
157
+ var filterValue = buildFilterValue(filter);
158
+ var ruleContent = buildRuleValue({
159
+ rule: rule,
160
+ defaultType: defaultType
161
+ });
162
+ var blockLength = label.length + ruleContent.length + filterValue.length;
163
+ var toolTipDisabled = blockLength <= 50;
164
+ var blockContent = !toolTipDisabled ? "".concat(filterValue.slice(0, 50 - blockLength), "...") : filterValue;
165
+ var deleteBlockHotKeyRef = useHotkeys(SHORTCUTS.deleteBlock.sequence, function () {
166
+ return handleDelete(key);
167
+ }, {
168
+ mode: "scoped"
169
+ });
170
+ var openFilterPaneHotKeyRef = useHotkeys(SHORTCUTS.openFilterPane.sequence, function () {
171
+ return setIsPaneOpen(key);
172
+ }, {
173
+ mode: "scoped"
174
+ });
175
+ return /*#__PURE__*/jsx(Tooltip, {
176
+ content: filterValue,
177
+ disabled: toolTipDisabled,
178
+ position: "bottom",
179
+ children: /*#__PURE__*/jsxs("div", {
180
+ className: "neeto-filters-bar__block",
181
+ "data-testid": "neeto-filters-".concat(hyphenate(key), "-block"),
182
+ tabIndex: 0,
183
+ ref: function ref(node) {
184
+ deleteBlockHotKeyRef.current = node;
185
+ openFilterPaneHotKeyRef.current = node;
186
+ },
187
+ onClick: function onClick() {
188
+ return setIsPaneOpen(key);
189
+ },
190
+ children: [/*#__PURE__*/jsxs(Typography, {
191
+ variant: "body2",
192
+ children: [label, ":"]
193
+ }), /*#__PURE__*/jsx(Typography, {
194
+ className: "neeto-ui-text-gray-500",
195
+ variant: "body2",
196
+ children: ruleContent
197
+ }), !isRuleEmpty(rule) && /*#__PURE__*/jsx(Typography, {
198
+ variant: "body2",
199
+ children: blockContent
200
+ }), !defaultValue && /*#__PURE__*/jsx("div", {
201
+ className: "neeto-filters-bar__block-close",
202
+ children: /*#__PURE__*/jsx(XIcon, {
203
+ "data-testid": "neeto-filters-".concat(hyphenate(key), "-block-close-btn"),
204
+ size: 16,
205
+ strokeWidth: 2,
206
+ onClick: function onClick(event) {
207
+ event.stopPropagation();
208
+ handleDelete(key);
209
+ }
210
+ })
211
+ })]
212
+ })
213
+ });
214
+ };
215
+
216
+ var ChildBlock = function ChildBlock(_ref) {
217
+ var childKey = _ref.childKey,
218
+ setIsPaneOpen = _ref.setIsPaneOpen,
219
+ handleDelete = _ref.handleDelete;
220
+ var deleteBlockHotKeyRef = useHotkeys(SHORTCUTS.deleteBlock.sequence, function () {
221
+ return handleDelete(childKey);
222
+ }, {
223
+ mode: "scoped"
224
+ });
225
+ var openFilterPaneHotKeyRef = useHotkeys(SHORTCUTS.openFilterPane.sequence, function () {
226
+ return setIsPaneOpen(childKey);
227
+ }, {
228
+ mode: "scoped"
229
+ });
230
+ return /*#__PURE__*/jsxs("div", {
231
+ className: "neeto-filters-bar__block",
232
+ "data-testid": "neeto-filters-".concat(hyphenate(childKey), "-block"),
233
+ tabIndex: 0,
234
+ ref: function ref(node) {
235
+ deleteBlockHotKeyRef.current = node;
236
+ openFilterPaneHotKeyRef.current = node;
237
+ },
238
+ onClick: function onClick() {
239
+ return setIsPaneOpen(childKey);
240
+ },
241
+ children: [/*#__PURE__*/jsx(Typography, {
242
+ variant: "body2",
243
+ weight: "medium",
244
+ children: childKey
245
+ }), /*#__PURE__*/jsx("div", {
246
+ className: "neeto-filters-bar__block-close",
247
+ children: /*#__PURE__*/jsx(XIcon, {
248
+ "data-testid": "neeto-filters-".concat(hyphenate(childKey), "-block-close-btn"),
249
+ size: 16,
250
+ strokeWidth: 2,
251
+ onClick: function onClick(event) {
252
+ event.stopPropagation();
253
+ handleDelete(childKey);
254
+ }
255
+ })
256
+ })]
257
+ });
258
+ };
259
+
260
+ var FilterBar = function FilterBar(_ref) {
261
+ var entity = _ref.entity,
262
+ filters = _ref.filters,
263
+ setIsSavePaneOpen = _ref.setIsSavePaneOpen,
264
+ setIsPaneOpen = _ref.setIsPaneOpen,
265
+ childrenKeys = _ref.childrenKeys,
266
+ nonHiddenFilters = _ref.nonHiddenFilters,
267
+ childrenFiltersApplied = _ref.childrenFiltersApplied,
268
+ keywordFilter = _ref.keywordFilter,
269
+ handleApplyFilters = _ref.handleApplyFilters;
270
+ var _useTranslation = useTranslation(),
271
+ t = _useTranslation.t;
272
+ var history = useHistory();
273
+ var searchParams = useQueryParams({
274
+ toCamelCase: false
275
+ });
276
+ var handleDelete = function handleDelete(key) {
277
+ if (key === SEARCH_TERM_KEY) {
278
+ var params = omit([SEARCH_TERM_KEY], getSnakeCasedQueryParams());
279
+ var pathname = window.location.pathname;
280
+ history.push(buildUrl(pathname, params));
281
+ return;
282
+ }
283
+ if (childrenKeys.includes(key)) {
284
+ var _params = omit([key], getSnakeCasedQueryParams());
285
+ var _pathname = window.location.pathname;
286
+ history.push(buildUrl(_pathname, _params));
287
+ }
288
+ var newFilters = removeBy({
289
+ key: key
290
+ }, filters);
291
+ handleApplyFilters(newFilters);
292
+ };
293
+ var handleClearAll = function handleClearAll() {
294
+ var params = isKeywordFilterPresent(keywordFilter) ? omit([SEARCH_TERM_KEY], getSnakeCasedQueryParams()) : getSnakeCasedQueryParams();
295
+ var pathname = window.location.pathname;
296
+ history.push(buildUrl(pathname, params));
297
+ handleApplyFilters([], true);
298
+ };
299
+ var hideClearButton = nonHiddenFilters.every(function (_ref2) {
300
+ var defaultValue = _ref2.defaultValue,
301
+ isClearable = _ref2.isClearable;
302
+ return defaultValue && !isClearable;
303
+ }) && !childrenFiltersApplied;
304
+ return /*#__PURE__*/jsxs(Fragment, {
305
+ children: [nonHiddenFilters.map(function (filter) {
306
+ var _filter$suffixCompone;
307
+ return /*#__PURE__*/jsxs(Fragment$1, {
308
+ children: [/*#__PURE__*/jsx(Block, {
309
+ filter: filter,
310
+ handleDelete: handleDelete,
311
+ setIsPaneOpen: setIsPaneOpen
312
+ }), (_filter$suffixCompone = filter.suffixComponent) === null || _filter$suffixCompone === void 0 ? void 0 : _filter$suffixCompone.call(filter), /*#__PURE__*/jsx("div", {
313
+ className: "neeto-filters-bar--separator"
314
+ })]
315
+ }, filter.key);
316
+ }), childrenKeys.filter(function (childKey) {
317
+ return has(childKey, searchParams);
318
+ }).map(function (childKey) {
319
+ return /*#__PURE__*/jsxs(Fragment$1, {
320
+ children: [/*#__PURE__*/jsx(ChildBlock, {
321
+ childKey: childKey,
322
+ handleDelete: handleDelete,
323
+ setIsPaneOpen: setIsPaneOpen
324
+ }), /*#__PURE__*/jsx("div", {
325
+ className: "neeto-filters-bar--separator"
326
+ })]
327
+ }, childKey);
328
+ }), /*#__PURE__*/jsxs("div", {
329
+ className: "flex items-center gap-x-2",
330
+ children: [isPresent(entity) && !hasOnlyKeywordFilter(filters) && /*#__PURE__*/jsx(Button, {
331
+ "data-testid": "save-segment-button",
332
+ label: t("neetoFilters.views.saveAsNewView"),
333
+ variant: "secondary",
334
+ onClick: function onClick() {
335
+ setIsPaneOpen(false);
336
+ setIsSavePaneOpen({
337
+ action: SEGMENT_ACTIONS.CREATE
338
+ });
339
+ }
340
+ }), !hideClearButton && /*#__PURE__*/jsx(Button, {
341
+ "data-testid": "neeto-filters-bar-clear-btn",
342
+ label: t("neetoFilters.common.clearFilters"),
343
+ size: "sm",
344
+ variant: "secondary",
345
+ onClick: handleClearAll
346
+ })]
347
+ })]
348
+ });
349
+ };
350
+
351
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
352
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
353
+ var useSegment = function useSegment(_ref) {
354
+ var entity = _ref.entity,
355
+ keyword = _ref.keyword,
356
+ columns = _ref.columns,
357
+ childrenKeys = _ref.childrenKeys,
358
+ segmentsBaseUrl = _ref.segmentsBaseUrl;
359
+ var history = useHistory();
360
+ var _useTranslation = useTranslation(),
361
+ t = _useTranslation.t;
362
+ var initialRenderRef = useRef(true);
363
+ var _useQueryParams = useQueryParams(),
364
+ segmentId = _useQueryParams.segmentId;
365
+ var _useState = useState({
366
+ action: undefined
367
+ }),
368
+ _useState2 = _slicedToArray(_useState, 2),
369
+ isSavePaneOpen = _useState2[0],
370
+ setIsSavePaneOpen = _useState2[1];
371
+ var _useCreateSegment = useCreateSegment(),
372
+ createSegment = _useCreateSegment.mutate,
373
+ isCreating = _useCreateSegment.isPending;
374
+ var _useUpdateSegment = useUpdateSegment(),
375
+ updateSegment = _useUpdateSegment.mutate,
376
+ isUpdating = _useUpdateSegment.isPending;
377
+ var isSubmitting = isCreating || isUpdating;
378
+ var searchParams = useQueryParams({
379
+ toCamelCase: false
380
+ });
381
+ var keywordValue = searchParams[SEARCH_TERM_KEY] || "";
382
+ var isEdit = isSavePaneOpen.action === SEGMENT_ACTIONS.EDIT;
383
+ var keywordFilter = _objectSpread(_objectSpread({
384
+ type: "text",
385
+ rule: "contains",
386
+ label: t("neetoFilters.common.searchTerm")
387
+ }, keyword), {}, {
388
+ key: SEARCH_TERM_KEY,
389
+ value: keywordValue
390
+ });
391
+ var filters = buildFiltersFromSearchParams(isKeywordFilterPresent$1(keywordFilter) && isNotEmpty(keywordValue) ? [keywordFilter].concat(_toConsumableArray(columns)) : columns);
392
+ var nonHiddenFilters = removeBy({
393
+ hidden: true
394
+ }, filters);
395
+ var handleApplyFilters = function handleApplyFilters(newFilters, isClearAll) {
396
+ var params = buildUrlFromFilters(_objectSpread({
397
+ columns: columns,
398
+ filters: isKeywordFilterPresent$1(keywordFilter) && isNotEmpty(getSearchTermFromUrl()) ? [].concat(_toConsumableArray(newFilters), [keywordFilter]) : newFilters
399
+ }, isClearAll && {
400
+ isClearAll: true,
401
+ childrenKeys: childrenKeys
402
+ }));
403
+ history.push({
404
+ search: params
405
+ });
406
+ };
407
+ var childrenFiltersApplied = childrenKeys.some(function (key) {
408
+ return has(key, searchParams);
409
+ });
410
+ useEffect(function () {
411
+ if (initialRenderRef.current) {
412
+ initialRenderRef.current = false;
413
+ return noop;
414
+ }
415
+ var handler = setTimeout(function () {
416
+ handleApplyFilters(filters);
417
+ }, 500);
418
+ return function () {
419
+ return clearTimeout(handler);
420
+ };
421
+ }, [keywordValue]);
422
+ var handleCreateSuccess = function handleCreateSuccess(_ref2) {
423
+ var _filters$;
424
+ var _ref2$segment = _ref2.segment,
425
+ id = _ref2$segment.id,
426
+ filters = _ref2$segment.filters;
427
+ history.push({
428
+ pathname: segmentsBaseUrl,
429
+ search: buildUrlFromSegment({
430
+ columns: columns,
431
+ filters: (_filters$ = filters[0]) === null || _filters$ === void 0 ? void 0 : _filters$.conditions,
432
+ id: id
433
+ })
434
+ });
435
+ setIsSavePaneOpen({
436
+ action: undefined
437
+ });
438
+ showThumbsUpToastr();
439
+ };
440
+ var handleCreate = function handleCreate(_ref3, _ref4) {
441
+ var name = _ref3.name;
442
+ var setFieldError = _ref4.setFieldError;
443
+ var filtersPayload = buildSegmentsPayload(filters, childrenKeys);
444
+ createSegment({
445
+ name: name,
446
+ entity: entity,
447
+ filters: filtersPayload
448
+ }, {
449
+ onSuccess: handleCreateSuccess,
450
+ onError: function onError(error) {
451
+ return setFieldError("name", error.response.data.error);
452
+ }
453
+ });
454
+ };
455
+ var handleUpdate = function handleUpdate(_ref5, _ref6) {
456
+ var name = _ref5.name;
457
+ var setFieldError = _ref6.setFieldError;
458
+ var filtersPayload = buildSegmentsPayload(filters, childrenKeys);
459
+ updateSegment({
460
+ id: segmentId,
461
+ payload: {
462
+ name: name,
463
+ filters: filtersPayload
464
+ }
465
+ }, {
466
+ onSuccess: function onSuccess() {
467
+ setIsSavePaneOpen({
468
+ action: undefined
469
+ });
470
+ showThumbsUpToastr();
471
+ },
472
+ onError: function onError(error) {
473
+ return setFieldError("name", error.response.data.error);
474
+ }
475
+ });
476
+ };
477
+ return {
478
+ isEdit: isEdit,
479
+ filters: filters,
480
+ keywordFilter: keywordFilter,
481
+ isSubmitting: isSubmitting,
482
+ nonHiddenFilters: nonHiddenFilters,
483
+ isSavePaneOpen: isSavePaneOpen,
484
+ setIsSavePaneOpen: setIsSavePaneOpen,
485
+ handleApplyFilters: handleApplyFilters,
486
+ childrenFiltersApplied: childrenFiltersApplied,
487
+ handleSubmit: isEdit ? handleUpdate : handleCreate
488
+ };
489
+ };
490
+
491
+ var SegmentBar = function SegmentBar(_ref) {
492
+ var entity = _ref.entity,
493
+ filters = _ref.filters,
494
+ setIsSavePaneOpen = _ref.setIsSavePaneOpen,
495
+ setIsPaneOpen = _ref.setIsPaneOpen,
496
+ handleApplyFilters = _ref.handleApplyFilters,
497
+ childrenKeys = _ref.childrenKeys,
498
+ showSegmentName = _ref.showSegmentName;
499
+ var _useTranslation = useTranslation(),
500
+ t = _useTranslation.t;
501
+ var history = useHistory();
502
+ var _useQueryParams = useQueryParams(),
503
+ segmentId = _useQueryParams.segmentId;
504
+ var queryClient = useQueryClient();
505
+ var cachedSegments = queryClient.getQueryData([QUERY_KEYS.SEGMENTS_LIST, entity, ""]) || DEFAULT_SEGMENTS_CACHE;
506
+ var segment = findById(segmentId, cachedSegments.segments);
507
+ if (isNotPresent(segment)) return null;
508
+ var handleDelete = function handleDelete(key) {
509
+ if (key === SEARCH_TERM_KEY) {
510
+ var params = omit([SEARCH_TERM_KEY], getSnakeCasedQueryParams$1());
511
+ var pathname = window.location.pathname;
512
+ history.push(buildUrl(pathname, params));
513
+ return;
514
+ }
515
+ if (childrenKeys.includes(key)) {
516
+ var _params = omit([key], getSnakeCasedQueryParams$1());
517
+ var _pathname = window.location.pathname;
518
+ history.push(buildUrl(_pathname, _params));
519
+ }
520
+ var newFilters = removeBy({
521
+ key: key
522
+ }, filters);
523
+ handleApplyFilters(newFilters);
524
+ };
525
+ var changedFilters = deepCompare(segment.filters[0].conditions, filters);
526
+ return /*#__PURE__*/jsxs(Fragment, {
527
+ children: [showSegmentName && /*#__PURE__*/jsx(Typography, {
528
+ className: "neeto-ui-text-gray-600 me-4 px-2",
529
+ "data-testid": "neeto-filters-segment-name",
530
+ variant: "h4",
531
+ children: segment.name
532
+ }), isNotEmpty(changedFilters) && /*#__PURE__*/jsxs(Fragment, {
533
+ children: [/*#__PURE__*/jsx(DropdownMenu, {
534
+ buttonProps: {
535
+ className: "me-2"
536
+ },
537
+ buttonStyle: "secondary",
538
+ label: t("neetoFilters.views.updateView"),
539
+ children: /*#__PURE__*/jsxs(DropdownMenu.Menu, {
540
+ children: [/*#__PURE__*/jsx(DropdownMenu.MenuItem, {
541
+ children: /*#__PURE__*/jsx(DropdownMenu.MenuItem.Button, {
542
+ onClick: function onClick() {
543
+ return setIsSavePaneOpen({
544
+ action: SEGMENT_ACTIONS.EDIT
545
+ });
546
+ },
547
+ children: t("neetoFilters.views.updateView")
548
+ })
549
+ }), /*#__PURE__*/jsx(DropdownMenu.MenuItem, {
550
+ children: /*#__PURE__*/jsx(DropdownMenu.MenuItem.Button, {
551
+ onClick: function onClick() {
552
+ setIsPaneOpen(false);
553
+ setIsSavePaneOpen({
554
+ action: SEGMENT_ACTIONS.CREATE
555
+ });
556
+ },
557
+ children: t("neetoFilters.views.saveAsNewView")
558
+ })
559
+ })]
560
+ })
561
+ }), changedFilters.map(function (filter) {
562
+ var _filter$suffixCompone;
563
+ return /*#__PURE__*/jsxs(Fragment$1, {
564
+ children: [/*#__PURE__*/jsx(Block, {
565
+ filter: filter,
566
+ handleDelete: handleDelete,
567
+ setIsPaneOpen: setIsPaneOpen
568
+ }), (_filter$suffixCompone = filter.suffixComponent) === null || _filter$suffixCompone === void 0 ? void 0 : _filter$suffixCompone.call(filter), /*#__PURE__*/jsx("div", {
569
+ className: "neeto-filters-bar--separator"
570
+ })]
571
+ }, filter.key);
572
+ })]
573
+ })]
574
+ });
575
+ };
576
+
577
+ var Bar = function Bar(_ref) {
578
+ var entity = _ref.entity,
579
+ className = _ref.className,
580
+ _ref$columns = _ref.columns,
581
+ columns = _ref$columns === void 0 ? [] : _ref$columns,
582
+ segmentHelpPopoverProps = _ref.segmentHelpPopoverProps,
583
+ _ref$keyword = _ref.keyword,
584
+ keyword = _ref$keyword === void 0 ? {
585
+ node: "",
586
+ value: "",
587
+ setValue: noop
588
+ } : _ref$keyword,
589
+ _ref$setIsPaneOpen = _ref.setIsPaneOpen,
590
+ setIsPaneOpen = _ref$setIsPaneOpen === void 0 ? identity : _ref$setIsPaneOpen,
591
+ _ref$childrenKeys = _ref.childrenKeys,
592
+ childrenKeys = _ref$childrenKeys === void 0 ? [] : _ref$childrenKeys,
593
+ _ref$defaultSegmentNa = _ref.defaultSegmentName,
594
+ defaultSegmentName = _ref$defaultSegmentNa === void 0 ? t("neetoFilters.common.allResults") : _ref$defaultSegmentNa,
595
+ _ref$segmentsBaseUrl = _ref.segmentsBaseUrl,
596
+ segmentsBaseUrl = _ref$segmentsBaseUrl === void 0 ? window.location.pathname : _ref$segmentsBaseUrl,
597
+ customFilters = _ref.customFilters,
598
+ _ref$showSegmentName = _ref.showSegmentName,
599
+ showSegmentName = _ref$showSegmentName === void 0 ? true : _ref$showSegmentName;
600
+ var queryClient = useQueryClient();
601
+ var _useQueryParams = useQueryParams(),
602
+ segmentId = _useQueryParams.segmentId;
603
+ var segmentFormInitialRef = useRef(null);
604
+ var _useSegment = useSegment({
605
+ entity: entity,
606
+ keyword: keyword,
607
+ columns: columns,
608
+ childrenKeys: childrenKeys,
609
+ segmentsBaseUrl: segmentsBaseUrl
610
+ }),
611
+ isEdit = _useSegment.isEdit,
612
+ filters = _useSegment.filters,
613
+ handleSubmit = _useSegment.handleSubmit,
614
+ isSubmitting = _useSegment.isSubmitting,
615
+ keywordFilter = _useSegment.keywordFilter,
616
+ isSavePaneOpen = _useSegment.isSavePaneOpen,
617
+ nonHiddenFilters = _useSegment.nonHiddenFilters,
618
+ setIsSavePaneOpen = _useSegment.setIsSavePaneOpen,
619
+ handleApplyFilters = _useSegment.handleApplyFilters,
620
+ childrenFiltersApplied = _useSegment.childrenFiltersApplied;
621
+ var BarComponent = segmentId ? SegmentBar : FilterBar;
622
+ var cachedSegments = queryClient.getQueryData([QUERY_KEYS.SEGMENTS_LIST, entity, ""]) || DEFAULT_SEGMENTS_CACHE;
623
+ var segment = findById(segmentId, cachedSegments.segments);
624
+ if (isEmpty(nonHiddenFilters) && !childrenFiltersApplied) return null;
625
+ return /*#__PURE__*/jsxs(Fragment, {
626
+ children: [/*#__PURE__*/jsx("div", {
627
+ className: classnames("neeto-filters-bar", _defineProperty({}, className, className)),
628
+ children: /*#__PURE__*/jsx(BarComponent, {
629
+ childrenFiltersApplied: childrenFiltersApplied,
630
+ childrenKeys: childrenKeys,
631
+ entity: entity,
632
+ filters: filters,
633
+ handleApplyFilters: handleApplyFilters,
634
+ keywordFilter: keywordFilter,
635
+ nonHiddenFilters: nonHiddenFilters,
636
+ setIsPaneOpen: setIsPaneOpen,
637
+ setIsSavePaneOpen: setIsSavePaneOpen,
638
+ showSegmentName: showSegmentName
639
+ })
640
+ }), /*#__PURE__*/jsx(SegmentForm, {
641
+ childrenKeys: childrenKeys,
642
+ columns: columns,
643
+ customFilters: customFilters,
644
+ defaultSegmentName: defaultSegmentName,
645
+ filters: filters,
646
+ handleSubmit: handleSubmit,
647
+ isEdit: isEdit,
648
+ isSubmitting: isSubmitting,
649
+ keywordFilter: keywordFilter,
650
+ segmentFormInitialRef: segmentFormInitialRef,
651
+ segmentHelpPopoverProps: segmentHelpPopoverProps,
652
+ isOpen: isPresent(isSavePaneOpen.action),
653
+ initialValues: isEdit ? assoc("filters", [{
654
+ conditions: filters
655
+ }], segment) : {
656
+ name: "",
657
+ filters: [{
658
+ conditions: filters
659
+ }]
660
+ },
661
+ onClose: function onClose() {
662
+ return setIsSavePaneOpen({
663
+ action: undefined
664
+ });
665
+ }
666
+ })]
667
+ });
668
+ };
669
+
670
+ export { Bar as default };
671
+ //# sourceMappingURL=Bar.js.map