UNPKG

@livelike/react-native

Version:

LiveLike React Native package

89 lines 3.42 kB
import { addProgramListener, removeProgramListener, FOLLOW_UP_WIDGET_UPDATED_EVENT, PREDICTION_WIDGET_ID_PROP } from '@livelike/javascript'; import { useEffect, useState } from 'react'; import { widgetStore, widgetStoreActions } from '../store'; import { WidgetResultState, WidgetUIPhase } from '../types'; import { useSelectedFieldStore } from './useSelectedFieldStore'; import { useWidgetUIPhase } from './useWidgetUIPhase'; /** * @description Listens to prediction follow up updates, and updates widget state and return followup widget details * @param UsePredictionWidgetEffectArg * @returns object with "followUpWidget" prop */ export const usePredictionWidgetEffect = _ref => { let { widgetId, programId, inlineFollowUp } = _ref; const [followUpWidget, setFollowUpWidget] = useState(null); const currentWidgetUIPhase = useWidgetUIPhase({ widgetId }); const isFollowUpPublished = currentWidgetUIPhase === WidgetUIPhase.FOLLOW_UP_PUBLISHED; const followUpWidgetPayload = useSelectedFieldStore(widgetStore, () => { var _widgetStore$get$widg; const widgetFollowUps = (_widgetStore$get$widg = 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]; }); useEffect(() => { if (followUpWidgetPayload && inlineFollowUp && followUpWidgetPayload.status === 'published') { setFollowUpWidget(followUpWidgetPayload); } }, [followUpWidgetPayload, inlineFollowUp]); useEffect(() => { if (isFollowUpPublished) { return; } function onProgramListener(arg) { if (FOLLOW_UP_WIDGET_UPDATED_EVENT.includes(arg.event)) { var _widgetStore$get$widg2; const predictionWidgetId = arg.message[PREDICTION_WIDGET_ID_PROP[arg.message.kind]]; if (predictionWidgetId !== widgetId) { return; } const widget = (_widgetStore$get$widg2 = 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); } widgetStoreActions.updateWidgetStateAction({ widgetId, widgetState: { widgetPayload: { ...widget, follow_ups: followUps }, widgetUIPhase: WidgetUIPhase.FOLLOW_UP_PUBLISHED, widgetResultState: WidgetResultState.SHOWN } }); } } addProgramListener({ programId }, onProgramListener); return () => { removeProgramListener({ programId }, onProgramListener); }; }, [isFollowUpPublished, programId, widgetId]); return { followUpWidget }; }; //# sourceMappingURL=usePredictionWidgetEffect.js.map