UNPKG

2.43 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5var __importStar = (this && this.__importStar) || function (mod) {
6 if (mod && mod.__esModule) return mod;
7 var result = {};
8 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
9 result["default"] = mod;
10 return result;
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13const lodash_kebabcase_1 = __importDefault(require("lodash.kebabcase"));
14const React = __importStar(require("react"));
15const js_1 = __importDefault(require("@bearer/js"));
16exports.BearerContext = React.createContext({
17 bearer: js_1.default.instance,
18 state: {},
19 handlePropUpdates: () => { }
20});
21class BearerProvider extends React.Component {
22 constructor(props) {
23 super(props);
24 this.handlePropUpdates = (e) => {
25 e.stopPropagation();
26 const payload = e.detail;
27 const kebabPayload = Object.keys(payload).reduce((acc, key) => {
28 acc[lodash_kebabcase_1.default(key)] = payload[key];
29 return acc;
30 }, {});
31 this.setState(state => (Object.assign({}, state, { integrationState: Object.assign({}, state.integrationState, kebabPayload) })));
32 };
33 this.state = {
34 bearer: this.initBearerFromProps(props),
35 integrationState: props.initialContext || {}
36 };
37 }
38 render() {
39 const value = {
40 bearer: this.state.bearer,
41 handlePropUpdates: this.handlePropUpdates,
42 state: this.state.integrationState
43 };
44 return React.createElement(exports.BearerContext.Provider, { value: value }, this.props.children);
45 }
46 componentWillReceiveProps(newProps, oldProps) {
47 if (newProps.clientId !== oldProps.clientId) {
48 this.setState(state => (Object.assign({}, state, { bearer: this.initBearerFromProps(newProps) })));
49 }
50 }
51 componentDidUpdate(_prevProps, prevState) {
52 if (this.props.onUpdate && this.state !== prevState) {
53 this.props.onUpdate(this.state);
54 }
55 }
56 initBearerFromProps(props) {
57 const { integrationHost, domObserver } = props;
58 return js_1.default(props.clientId, { integrationHost, domObserver });
59 }
60}
61exports.default = BearerProvider;