@livelike/react-native
Version:
LiveLike React Native package
96 lines (95 loc) • 3.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.usePredictionWidgetEffect = void 0;
var _javascript = require("@livelike/javascript");
var _react = require("react");
var _store = require("../store");
var _types = require("../types");
var _useSelectedFieldStore = require("./useSelectedFieldStore");
var _useWidgetUIPhase = require("./useWidgetUIPhase");
/**
* @description Listens to prediction follow up updates, and updates widget state and return followup widget details
* @param UsePredictionWidgetEffectArg
* @returns object with "followUpWidget" prop
*/
const usePredictionWidgetEffect = _ref => {
let {
widgetId,
programId,
inlineFollowUp
} = _ref;
const [followUpWidget, setFollowUpWidget] = (0, _react.useState)(null);
const currentWidgetUIPhase = (0, _useWidgetUIPhase.useWidgetUIPhase)({
widgetId
});
const isFollowUpPublished = currentWidgetUIPhase === _types.WidgetUIPhase.FOLLOW_UP_PUBLISHED;
const followUpWidgetPayload = (0, _useSelectedFieldStore.useSelectedFieldStore)(_store.widgetStore, () => {
var _widgetStore$get$widg;
const widgetFollowUps = (_widgetStore$get$widg = _store.widgetStore.get()[widgetId]) === null || _widgetStore$get$widg === void 0 || (_widgetStore$get$widg = _widgetStore$get$widg.widgetPayload) === null || _widgetStore$get$widg === void 0 ? void 0 : _widgetStore$get$widg.follow_ups;
if (!(widgetFollowUps !== null && widgetFollowUps !== void 0 && widgetFollowUps.length)) {
return undefined;
}
return widgetFollowUps[widgetFollowUps.length - 1];
});
(0, _react.useEffect)(() => {
if (followUpWidgetPayload && inlineFollowUp && followUpWidgetPayload.status === 'published') {
setFollowUpWidget(followUpWidgetPayload);
}
}, [followUpWidgetPayload, inlineFollowUp]);
(0, _react.useEffect)(() => {
if (isFollowUpPublished) {
return;
}
function onProgramListener(arg) {
if (_javascript.FOLLOW_UP_WIDGET_UPDATED_EVENT.includes(arg.event)) {
var _widgetStore$get$widg2;
const predictionWidgetId = arg.message[_javascript.PREDICTION_WIDGET_ID_PROP[arg.message.kind]];
if (predictionWidgetId !== widgetId) {
return;
}
const widget = (_widgetStore$get$widg2 = _store.widgetStore.get()[widgetId]) === null || _widgetStore$get$widg2 === void 0 ? void 0 : _widgetStore$get$widg2.widgetPayload;
if (!widget) {
return;
}
const followUps = [...widget.follow_ups];
const followUpIndex = followUps.findIndex(_ref2 => {
let {
id
} = _ref2;
return id === arg.message.id;
});
if (followUpIndex < 0) {
followUps.push(arg.message);
} else {
followUps.splice(followUpIndex, 1, arg.message);
}
_store.widgetStoreActions.updateWidgetStateAction({
widgetId,
widgetState: {
widgetPayload: {
...widget,
follow_ups: followUps
},
widgetUIPhase: _types.WidgetUIPhase.FOLLOW_UP_PUBLISHED,
widgetResultState: _types.WidgetResultState.SHOWN
}
});
}
}
(0, _javascript.addProgramListener)({
programId
}, onProgramListener);
return () => {
(0, _javascript.removeProgramListener)({
programId
}, onProgramListener);
};
}, [isFollowUpPublished, programId, widgetId]);
return {
followUpWidget
};
};
exports.usePredictionWidgetEffect = usePredictionWidgetEffect;
//# sourceMappingURL=usePredictionWidgetEffect.js.map