UNPKG

@livelike/react-native

Version:

LiveLike React Native package

98 lines 3.53 kB
import { getRewardTransactions, getTargetedWidgetIdAndKind } from '@livelike/javascript'; import { useEffect, useRef } from 'react'; import { getWidgetRewardsFromRewardTransactions, widgetStoreActions } from '../store'; import { useWidgetInteractions } from './useWidgetInteractions'; import { useWidgetKind } from './useWidgetKind'; export function useLoadWidgetRewardsEffect(_ref) { let { widgetId } = _ref; const widgetInteractions = useWidgetInteractions({ widgetId }); const widgetKind = useWidgetKind({ widgetId }); const interactionsRef = useRef(null); const rewardRef = useRef(null); const mountRef = useRef(null); useEffect(() => { // avoid getting reward transactions on initial mount since we are already loading // widget details including reward transaction as part of useLoadWidgetEffect if (!mountRef.current && Array.isArray(widgetInteractions)) { mountRef.current = true; return; } // avoid loading widget rewards if there are no interactions or // widget interaction are same as earlier interaction if (!(widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length)) { return; } if (areWidgetInteractionsSame(widgetInteractions, interactionsRef.current ?? [])) { return; } interactionsRef.current = widgetInteractions; const timeout = setTimeout(() => { getTargetedWidgetIdAndKind({ widgetId, widgetKind }).then(_ref2 => { let { widgetId: targettedWidgetId } = _ref2; return getRewardTransactions({ widgetIds: [targettedWidgetId] }); }).then(res => { const widgetRewards = getWidgetRewardsFromRewardTransactions(res.results); if (rewardRef.current && areWidgetRewardsSame(widgetRewards, rewardRef.current)) { return; } rewardRef.current = widgetRewards; // Observed a behaviour where old reward animation overlap with new reward animation // until old reward animation gets resetted and dereferenced // To avoid overlapping, we clear the existing rewards from state so that // there's no residue of existing reward animation Promise.resolve(widgetStoreActions.updateWidgetRewardsAction({ widgetId, widgetRewards: [] })).then(() => widgetStoreActions.updateWidgetRewardsAction({ widgetId, widgetRewards })); }); }, 1000); return () => { clearTimeout(timeout); }; }, [widgetId, widgetKind, widgetInteractions]); } function areWidgetRewardsSame(rewardsA, rewardsB) { if (rewardsA.length !== rewardsB.length) { return false; } return rewardsA.every(rewardA => { return !!rewardsB.find(_ref3 => { let { reward_item_id, reward_item_amount } = _ref3; return rewardA.reward_item_id === reward_item_id && rewardA.reward_item_amount === reward_item_amount; }); }); } function areWidgetInteractionsSame(interactionsA, interactionsB) { if (interactionsA.length !== interactionsB.length) { return false; } return interactionsA.every(interactionA => { return !!interactionsB.find(_ref4 => { let { option_id, choice_id } = _ref4; return interactionA.option_id && interactionA.option_id === option_id || interactionA.choice_id && interactionA.choice_id === choice_id; }); }); } //# sourceMappingURL=useLoadWidgetRewardsEffect.js.map