@livelike/react-native
Version:
LiveLike React Native package
119 lines (118 loc) • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useCheerMeterOnOptionPress = void 0;
var _react = require("react");
var _javascript = require("@livelike/javascript");
var _useWidgetActions = require("./useWidgetActions");
var _useWidgetInteractionActions = require("./useWidgetInteractionActions");
var _useWidgetInteractions = require("./useWidgetInteractions");
var _useWidgetOptions = require("./useWidgetOptions");
var _useWidgetInteractedAnalytics = require("./useWidgetInteractedAnalytics");
const useCheerMeterOnOptionPress = _ref => {
let {
widgetId,
optionIndex,
throttleTime
} = _ref;
const throttleRef = (0, _react.useRef)({
prevDateTime: undefined,
timeout: undefined,
cacheVoteCount: 0
});
const widgetOptions = (0, _useWidgetOptions.useWidgetOptions)({
widgetId
});
const {
createWidgetInteractionAction,
updateWidgetInteractionAction
} = (0, _useWidgetInteractionActions.useWidgetInteractionActions)({
widgetId
});
const {
updateWidgetOptionsAction
} = (0, _useWidgetActions.useWidgetActions)({
widgetId
});
const widgetOption = widgetOptions === null || widgetOptions === void 0 ? void 0 : widgetOptions[optionIndex];
const widgetInteractions = (0, _useWidgetInteractions.useWidgetInteractions)({
widgetId
});
const {
trackWidgetInteractedAction
} = (0, _useWidgetInteractedAnalytics.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 (0, _react.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 => {
(0, _javascript.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]);
};
exports.useCheerMeterOnOptionPress = useCheerMeterOnOptionPress;
//# sourceMappingURL=useCheerMeterOnOptionPress.js.map