@livelike/react-native
Version:
LiveLike React Native package
112 lines • 3.66 kB
JavaScript
import { useCallback, useRef } from 'react';
import { hasDebugLogger } from '@livelike/javascript';
import { useWidgetActions } from './useWidgetActions';
import { useWidgetInteractionActions } from './useWidgetInteractionActions';
import { useWidgetInteractions } from './useWidgetInteractions';
import { useWidgetOptions } from './useWidgetOptions';
import { useWidgetInteractedAnalytics } from './useWidgetInteractedAnalytics';
export const useCheerMeterOnOptionPress = _ref => {
let {
widgetId,
optionIndex,
throttleTime
} = _ref;
const throttleRef = useRef({
prevDateTime: undefined,
timeout: undefined,
cacheVoteCount: 0
});
const widgetOptions = useWidgetOptions({
widgetId
});
const {
createWidgetInteractionAction,
updateWidgetInteractionAction
} = useWidgetInteractionActions({
widgetId
});
const {
updateWidgetOptionsAction
} = useWidgetActions({
widgetId
});
const widgetOption = widgetOptions === null || widgetOptions === void 0 ? void 0 : widgetOptions[optionIndex];
const widgetInteractions = useWidgetInteractions({
widgetId
});
const {
trackWidgetInteractedAction
} = useWidgetInteractedAnalytics({
widgetId
});
const hasInteractedOption = !!(widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.find(_ref2 => {
let {
option_id
} = _ref2;
return option_id === (widgetOption === null || widgetOption === void 0 ? void 0 : widgetOption.id);
}));
return useCallback(() => {
let {
timeout,
prevDateTime,
cacheVoteCount
} = throttleRef.current;
const now = Date.now();
clearTimeout(timeout);
throttleRef.current.cacheVoteCount = cacheVoteCount + 1;
if (!prevDateTime || now - prevDateTime >= throttleTime) {
updateCount();
prevDateTime = now;
} else {
timeout = setTimeout(() => updateCount(), throttleTime - (now - prevDateTime));
}
throttleRef.current = {
prevDateTime,
timeout,
cacheVoteCount: throttleRef.current.cacheVoteCount
};
function updateCount() {
const interactionItem = {
...widgetOptions[optionIndex],
vote_count: throttleRef.current.cacheVoteCount
};
trackWidgetInteractedAction({
interactionItem: widgetOptions[optionIndex]
});
throttleRef.current = {
...throttleRef.current,
// reset cache vote count
cacheVoteCount: 0
};
(hasInteractedOption ? updateWidgetInteractionAction({
interactionItem
}) : createWidgetInteractionAction({
interactionItem
})).then(res => {
if (!res) {
return;
}
const updatedOptions = widgetOptions.map(option => ({
...option
}));
updatedOptions[optionIndex] = {
...updatedOptions[optionIndex],
vote_count: updatedOptions[optionIndex].vote_count + interactionItem.vote_count
};
updateWidgetOptionsAction({
widgetId,
widgetOptions: updatedOptions
});
}).catch(e => {
hasDebugLogger() && console.error(`Error while ${hasInteractedOption ? 'updating' : 'creating'} interaction`, e);
// add earlier cache vote count back
throttleRef.current = {
...throttleRef.current,
// reset cache vote count
cacheVoteCount: interactionItem.vote_count
};
});
}
}, [throttleRef.current, optionIndex, hasInteractedOption, widgetOptions, updateWidgetInteractionAction, createWidgetInteractionAction]);
};
//# sourceMappingURL=useCheerMeterOnOptionPress.js.map