UNPKG

@livelike/react-native

Version:

LiveLike React Native package

43 lines 1.45 kB
import { claimPredictionWidgetRewards, hasDebugLogger } from '@livelike/javascript'; import { useEffect, useRef } from 'react'; import { widgetStoreActions } from '../store'; import { useWidgetInteractions } from './useWidgetInteractions'; /** * @description `usePredictionClaimRewardEffect` hook claims the prediction reward * whenever a prediction follow up based UI is rendered. * Internally it calls claimPredictionWidgetRewards JS API. */ export function usePredictionClaimRewardEffect(_ref) { let { widgetId, widgetKind } = _ref; const claimedRef = useRef(null); const widgetInteracions = useWidgetInteractions({ widgetId }); const widgetInteraction = widgetInteracions === null || widgetInteracions === void 0 ? void 0 : widgetInteracions[0]; useEffect(() => { if (claimedRef.current) { claimedRef.current = false; } }, [widgetId, widgetKind]); useEffect(() => { if (claimedRef.current || !widgetInteraction) { return; } claimPredictionWidgetRewards({ widgetId, widgetKind }).then(res => { claimedRef.current = true; widgetStoreActions.updateWidgetRewardsAction({ widgetId, widgetRewards: res.rewards }); }).catch(error => { hasDebugLogger() && console.error('Error while claiming reward', error); }); }, [widgetId, widgetKind, widgetInteraction]); } //# sourceMappingURL=usePredictionClaimRewardEffect.js.map