@miso.ai/client-sdk-ui 1.10.0-beta.0 → 1.10.0-beta.2

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.
@@ -3,6 +3,7 @@ import { ROLE } from '../constants.js';
3
3
 
4
4
  // normalize //
5
5
  export function normalizeApiOptions([name, payload] = []) {
6
+ // TODO: take object form as well
6
7
  if (name === false) {
7
8
  return { actor: false };
8
9
  }
@@ -10,10 +11,14 @@ export function normalizeApiOptions([name, payload] = []) {
10
11
  payload = name;
11
12
  name = undefined;
12
13
  }
14
+ let group = undefined;
15
+ if (name && name.indexOf('/') !== -1) {
16
+ [group, name] = name.split('/');
17
+ }
13
18
  if ((name && typeof name !== 'string') || (payload !== undefined && typeof payload !== 'object')) {
14
19
  throw new Error(`Invalid arguments for useApi(): ${name}, ${payload}`);
15
20
  }
16
- return trimObj({ name, payload });
21
+ return trimObj({ group, name, payload });
17
22
  }
18
23
 
19
24
  export function normalizeLayoutsOptions(options = {}) {
@@ -105,6 +110,17 @@ export function normalizeInteractionsOptions(options) {
105
110
  return { ...options, preprocess };
106
111
  }
107
112
 
113
+ export function normalizeAutocompleteOptions(options) {
114
+ if (options === undefined) {
115
+ throw new Error(`Expect autocomplete options to be an object or a boolean value: ${options}`);
116
+ }
117
+ if (typeof options === 'boolean') {
118
+ options = { actor: options };
119
+ }
120
+ const processData = normalizeDataProcessorOptions(options.processData);
121
+ return { ...options, processData };
122
+ }
123
+
108
124
  // merge //
109
125
  export function mergeApiOptions(...optionsList) {
110
126
  if (optionsList[optionsList.length - 1] === false) {
@@ -190,6 +206,17 @@ export function mergeInteractionsOptions(...optionsList) {
190
206
  }));
191
207
  }
192
208
 
209
+ export function mergeAutocompleteOptions(...optionsList) {
210
+ if (optionsList[optionsList.length - 1] === false) {
211
+ return false;
212
+ }
213
+ return mergeOptions(optionsList, (merged, options) => Object.assign(merged, {
214
+ ...options,
215
+ api: mergeApiOptions(merged.api, options.api),
216
+ processData: mergeDataProcessorOptions(merged.processData, options.processData),
217
+ }));
218
+ }
219
+
193
220
 
194
221
 
195
222
  // manifest //
@@ -219,6 +246,11 @@ const FEATURES = [
219
246
  normalize: normalizeInteractionsOptions,
220
247
  merge: mergeInteractionsOptions,
221
248
  },
249
+ {
250
+ key: 'autocomplete',
251
+ normalize: normalizeAutocompleteOptions,
252
+ merge: mergeAutocompleteOptions,
253
+ },
222
254
  ];
223
255
 
224
256
 
@@ -328,18 +360,27 @@ for (const { key, normalize, merge } of FEATURES) {
328
360
  });
329
361
  }
330
362
 
331
- export function makeConfigurable(prototype) {
332
- Object.assign(prototype, {
333
- useApi(...args) {
334
- this._options.api.merge(args);
335
- return this;
336
- },
337
- clearApi() {
338
- this._options.api.set();
339
- return this;
340
- },
341
- });
342
- for (const feature of ['layouts', 'dataProcessor', 'trackers', 'interactions']) {
363
+ const DEFAULT_FEATURES = ['api', 'layouts', 'dataProcessor', 'trackers', 'interactions'];
364
+
365
+ export function makeConfigurable(prototype, features = DEFAULT_FEATURES) {
366
+ for (const feature of features) {
367
+ injectConfigurableFeature(prototype, feature);
368
+ }
369
+ }
370
+
371
+ function injectConfigurableFeature(prototype, feature) {
372
+ if (feature === 'api') {
373
+ Object.assign(prototype, {
374
+ useApi(...args) {
375
+ this._options.api.merge(args);
376
+ return this;
377
+ },
378
+ clearApi() {
379
+ this._options.api.set();
380
+ return this;
381
+ },
382
+ });
383
+ } else {
343
384
  const upperCased = upperCase(feature);
344
385
  Object.assign(prototype, {
345
386
  [`use${upperCased}`](value) {
@@ -1,9 +1,11 @@
1
1
  import { API } from '@miso.ai/commons';
2
2
  import Workflow from './base.js';
3
3
  import { mergeApiOptions } from './options.js';
4
- import { fields } from '../actor/index.js';
4
+ import * as sources from '../source/index.js';
5
+ import { fields, AutocompleteActor } from '../actor/index.js';
5
6
  import { ROLE } from '../constants.js';
6
7
  import { ListLayout, SearchBoxLayout } from '../layout/index.js';
8
+ import { makeConfigurable } from './options.js';
7
9
 
8
10
  const DEFAULT_API_OPTIONS = Object.freeze({
9
11
  group: API.GROUP.SEARCH,
@@ -13,6 +15,19 @@ const DEFAULT_API_OPTIONS = Object.freeze({
13
15
  },
14
16
  });
15
17
 
18
+ const DEFAULT_AUTOCOMPLETE_OPTIONS = Object.freeze({
19
+ actor: false,
20
+ api: {
21
+ group: API.GROUP.SEARCH,
22
+ name: API.NAME.AUTOCOMPLETE,
23
+ payload: {
24
+ completion_fields: ['suggested_queries', 'title'],
25
+ fl: ['title', 'url', 'cover_image'],
26
+ },
27
+ },
28
+ throttle: 300,
29
+ });
30
+
16
31
  const DEFAULT_LAYOUTS = Object.freeze({
17
32
  [ROLE.QUERY]: SearchBoxLayout.type,
18
33
  [ROLE.PRODUCTS]: ListLayout.type,
@@ -24,6 +39,7 @@ const DEFAULT_TRACKERS = Object.freeze({
24
39
 
25
40
  const DEFAULT_OPTIONS = Object.freeze({
26
41
  api: DEFAULT_API_OPTIONS,
42
+ autocomplete: DEFAULT_AUTOCOMPLETE_OPTIONS,
27
43
  layouts: DEFAULT_LAYOUTS,
28
44
  trackers: DEFAULT_TRACKERS,
29
45
  });
@@ -39,6 +55,11 @@ export default class Search extends Workflow {
39
55
  defaults: DEFAULT_OPTIONS,
40
56
  });
41
57
 
58
+ this._autocomplete = new AutocompleteActor(this._hub, {
59
+ source: sources.api(this._client),
60
+ options: this._options,
61
+ });
62
+
42
63
  this._unsubscribes.push(this._hub.on(fields.query(), payload => this.query(payload)));
43
64
  }
44
65
 
@@ -66,9 +87,20 @@ export default class Search extends Workflow {
66
87
  return this;
67
88
  }
68
89
 
90
+ updateCompletions(event) {
91
+ // TODO: verify
92
+ this._hub.update(fields.completions(), {
93
+ ...event,
94
+ source: 'manual',
95
+ });
96
+ return this;
97
+ }
98
+
69
99
  notifyViewUpdate(role = ROLE.PRODUCTS, ...args) {
70
100
  super.notifyViewUpdate(role, ...args);
71
101
  return this;
72
102
  }
73
103
 
74
104
  }
105
+
106
+ makeConfigurable(Search.prototype, ['autocomplete']);