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

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
  }
@@ -105,6 +106,17 @@ export function normalizeInteractionsOptions(options) {
105
106
  return { ...options, preprocess };
106
107
  }
107
108
 
109
+ export function normalizeAutocompleteOptions(options) {
110
+ if (options === undefined) {
111
+ throw new Error(`Expect autocomplete options to be an object or a boolean value: ${options}`);
112
+ }
113
+ if (typeof options === 'boolean') {
114
+ options = { actor: options };
115
+ }
116
+ const processData = normalizeDataProcessorOptions(options.processData);
117
+ return { ...options, processData };
118
+ }
119
+
108
120
  // merge //
109
121
  export function mergeApiOptions(...optionsList) {
110
122
  if (optionsList[optionsList.length - 1] === false) {
@@ -190,6 +202,17 @@ export function mergeInteractionsOptions(...optionsList) {
190
202
  }));
191
203
  }
192
204
 
205
+ export function mergeAutocompleteOptions(...optionsList) {
206
+ if (optionsList[optionsList.length - 1] === false) {
207
+ return false;
208
+ }
209
+ return mergeOptions(optionsList, (merged, options) => Object.assign(merged, {
210
+ ...options,
211
+ api: mergeApiOptions(merged.api, options.api),
212
+ processData: mergeDataProcessorOptions(merged.processData, options.processData),
213
+ }));
214
+ }
215
+
193
216
 
194
217
 
195
218
  // manifest //
@@ -219,6 +242,11 @@ const FEATURES = [
219
242
  normalize: normalizeInteractionsOptions,
220
243
  merge: mergeInteractionsOptions,
221
244
  },
245
+ {
246
+ key: 'autocomplete',
247
+ normalize: normalizeAutocompleteOptions,
248
+ merge: mergeAutocompleteOptions,
249
+ },
222
250
  ];
223
251
 
224
252
 
@@ -328,18 +356,27 @@ for (const { key, normalize, merge } of FEATURES) {
328
356
  });
329
357
  }
330
358
 
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']) {
359
+ const DEFAULT_FEATURES = ['api', 'layouts', 'dataProcessor', 'trackers', 'interactions'];
360
+
361
+ export function makeConfigurable(prototype, features = DEFAULT_FEATURES) {
362
+ for (const feature of features) {
363
+ injectConfigurableFeature(prototype, feature);
364
+ }
365
+ }
366
+
367
+ function injectConfigurableFeature(prototype, feature) {
368
+ if (feature === 'api') {
369
+ Object.assign(prototype, {
370
+ useApi(...args) {
371
+ this._options.api.merge(args);
372
+ return this;
373
+ },
374
+ clearApi() {
375
+ this._options.api.set();
376
+ return this;
377
+ },
378
+ });
379
+ } else {
343
380
  const upperCased = upperCase(feature);
344
381
  Object.assign(prototype, {
345
382
  [`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']);