@livelike/react-native
Version:
LiveLike React Native package
63 lines (62 loc) • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useNumberPredictionWidgetOption = useNumberPredictionWidgetOption;
var _javascript = require("@livelike/javascript");
var _react = require("react");
var _useIsWidgetDisabled = require("./useIsWidgetDisabled");
var _useWidget = require("./useWidget");
var _useWidgetInteractions = require("./useWidgetInteractions");
var _useWidgetOptions = require("./useWidgetOptions");
const NUMBER_PREDICTION_FOLLOW_UP_WIDGET_KIND = [_javascript.WidgetKind.IMAGE_NUMBER_PREDICTION_FOLLOW_UP, _javascript.WidgetKind.TEXT_NUMBER_PREDICTION_FOLLOW_UP];
function useNumberPredictionWidgetOption(_ref) {
let {
widgetId,
optionIndex,
numberOption
} = _ref;
const widgetOptions = (0, _useWidgetOptions.useWidgetOptions)({
widgetId
});
const isWidgetOptionDisabled = (0, _useIsWidgetDisabled.useIsWidgetDisabled)({
widgetId
});
const widgetInteractions = (0, _useWidgetInteractions.useWidgetInteractions)({
widgetId
});
const widget = (0, _useWidget.useWidget)({
widgetId
});
const widgetOption = widgetOptions === null || widgetOptions === void 0 ? void 0 : widgetOptions[optionIndex];
const widgetOptionId = widgetOption === null || widgetOption === void 0 ? void 0 : widgetOption.id;
const interactedNumberOption = (0, _react.useMemo)(() => {
var _widgetInteractions$;
if (!(widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length) || !((_widgetInteractions$ = widgetInteractions[0]) !== null && _widgetInteractions$ !== void 0 && (_widgetInteractions$ = _widgetInteractions$.votes) !== null && _widgetInteractions$ !== void 0 && _widgetInteractions$.length)) {
return numberOption;
}
const widgetInteraction = widgetInteractions[0];
const currentVote = widgetInteraction.votes.find(option => option.option_id === widgetOptionId);
if (!currentVote) {
return numberOption;
}
return {
optionId: currentVote.id,
number: currentVote.number
};
}, [widgetInteractions, optionIndex, numberOption, widgetOptionId]);
const widgetKind = widget.kind;
const showWidgetResult = NUMBER_PREDICTION_FOLLOW_UP_WIDGET_KIND.includes(widgetKind);
if (!widgetOptions || !widgetOption) {
return undefined;
}
const isCorrect = widgetOption.correct_number === (interactedNumberOption === null || interactedNumberOption === void 0 ? void 0 : interactedNumberOption.number);
return {
isCorrect,
widgetOption,
showWidgetResult,
interactedNumberOption,
isWidgetOptionDisabled
};
}
//# sourceMappingURL=useNumberPredictionWidgetOption.js.map