UNPKG

@livelike/react-native

Version:

LiveLike React Native package

77 lines (76 loc) 3.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LLWidgetChoiceOption = LLWidgetChoiceOption; var _react = _interopRequireWildcard(require("react")); var _hooks = require("../../hooks"); var _types = require("../../types"); var _LLWidgetOption = require("./LLWidgetOption"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function LLWidgetChoiceOption(_ref) { let { widgetId, optionIndex, selectedOptionIndex: selectedOptionIndexProp, onOptionChange, OptionComponent = _LLWidgetOption.LLWidgetOption, OptionComponentStyles } = _ref; const widgetResultState = (0, _hooks.useWidgetResultState)({ widgetId }); const widgetChoices = (0, _hooks.useWidgetChoices)({ widgetId }); const interactedOption = (0, _hooks.useInteractedWidgetOption)({ widgetId }); const optionDisabled = (0, _hooks.useIsWidgetOptionDisabled)({ widgetId, optionIndex }); const interactedOptionId = interactedOption === null || interactedOption === void 0 ? void 0 : interactedOption.id; const widgetChoice = widgetChoices === null || widgetChoices === void 0 ? void 0 : widgetChoices[optionIndex]; const widgetChoiceId = widgetChoice === null || widgetChoice === void 0 ? void 0 : widgetChoice.id; const selectedOptionIndex = (0, _react.useMemo)(() => { // show interactedOption as selected when user has not interacted with any of the option // useful for inital mount of widget where selectedIndex is -1 but user has already interacted // with the widget before. if (interactedOptionId && widgetChoiceId && interactedOptionId === widgetChoiceId && selectedOptionIndexProp < 0) { return optionIndex; } return selectedOptionIndexProp; }, [selectedOptionIndexProp, widgetChoiceId, interactedOptionId, optionIndex]); const answerPercentage = (0, _react.useMemo)(() => { if (!widgetChoices || !widgetChoices.length) { return 0; } const totalVotes = widgetChoices.reduce((total, option) => option.answer_count + total, 0); if (!totalVotes) { return 0; } return Math.round(widgetChoices[optionIndex].answer_count / totalVotes * 100); }, [widgetChoices, optionIndex]); const onOptionPressHandler = (0, _react.useCallback)(() => { onOptionChange(optionIndex); }, [optionIndex]); const showWidgetResult = widgetResultState === _types.WidgetResultState.SHOWN; if (!widgetChoices || !widgetChoice) { return undefined; } return /*#__PURE__*/_react.default.createElement(OptionComponent, { optionIndex: optionIndex, percentage: answerPercentage, optionImage: widgetChoice === null || widgetChoice === void 0 ? void 0 : widgetChoice.image_url, optionText: widgetChoice === null || widgetChoice === void 0 ? void 0 : widgetChoice.description, optionDisabled: optionDisabled, showWidgetResult: showWidgetResult, selectedOptionIndex: selectedOptionIndex, onOptionChange: onOptionPressHandler, styles: OptionComponentStyles, correctOption: interactedOption ? !!(widgetChoice !== null && widgetChoice !== void 0 && widgetChoice.is_correct) : undefined }); } //# sourceMappingURL=LLWidgetChoiceOption.js.map