@shopify/app-bridge-host
Version:
App Bridge Host contains components and middleware to be consumed by the app's host, as well as the host itself. The middleware and `Frame` component are responsible for facilitating communication between the client and host, and used to act on actions se
60 lines (57 loc) • 2.67 kB
JavaScript
import { __assign } from 'tslib';
import { Action } from '@shopify/app-bridge-core/actions/Picker';
import generateUuid from '@shopify/app-bridge-core/actions/uuid';
import { getMergedProps } from '@shopify/app-bridge-core/actions/helper';
import { resetStateReducer } from '../utilities.js';
import { createEventHandlers } from '../../../../eventHandlers.js';
import { pickerActionCreatorsMap } from './actionCreators.js';
import pickerReducer, { defaultPickerStore } from './reducer.js';
/**
* An object containing the key, actions, initial state and reducer of the Picker feature
* Can be used with the `withFeature` decorator to add the reducer
* and then make its actions and store available to the wrapped component
* @public
* */
var feature = {
actions: pickerActionCreatorsMap,
key: 'unstablePicker',
initialState: defaultPickerStore,
reducer: resetStateReducer(pickerReducer),
getApi: function (_a) {
var actions = _a.actions, subscribe = _a.subscribe;
var id = generateUuid();
var open = false;
var currentOptions = { items: [] };
var eventHandlers = createEventHandlers(function (event, listener) {
switch (event) {
case 'cancel':
return subscribe(Action.CANCEL, listener, id);
case 'loadMore':
return subscribe(Action.LOAD_MORE, listener, id);
case 'select':
return subscribe(Action.SELECT, listener, id);
case 'search':
return subscribe(Action.SEARCH, listener, id);
default:
throw Error("".concat(String(event), " not supported"));
}
});
var api = __assign(__assign({}, eventHandlers), { open: function (options) {
currentOptions = getMergedProps(currentOptions, options || {});
actions.open(__assign(__assign({}, currentOptions), { id: id }));
open = true;
}, cancel: function () {
actions.cancel({ id: id });
open = true;
}, setOptions: function (options) {
var newOptions = getMergedProps(currentOptions, options);
var shouldUpdate = JSON.stringify(newOptions) !== JSON.stringify(currentOptions) && open;
currentOptions = newOptions;
if (shouldUpdate) {
actions.update(__assign(__assign({}, currentOptions), { id: id }));
}
} });
return api;
},
};
export { pickerReducer as default, defaultPickerStore, feature, pickerActionCreatorsMap };