UNPKG

@livelike/react-native

Version:

LiveLike React Native package

94 lines (93 loc) 4.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LLWidgetVoteOption = LLWidgetVoteOption; 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 LLWidgetVoteOption(_ref) { let { widgetId, optionIndex, selectedOptionIndex: selectedOptionIndexProp, onOptionChange, correctable, OptionComponent = _LLWidgetOption.LLWidgetOption, OptionComponentStyles } = _ref; const { updateWidgetInteractionAction, createWidgetInteractionAction } = (0, _hooks.useWidgetInteractionActions)({ widgetId }); const widgetResultState = (0, _hooks.useWidgetResultState)({ widgetId }); const widgetOptions = (0, _hooks.useWidgetOptions)({ 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 widgetOption = widgetOptions === null || widgetOptions === void 0 ? void 0 : widgetOptions[optionIndex]; const widgetOptionId = widgetOption === null || widgetOption === void 0 ? void 0 : widgetOption.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 && widgetOptionId && interactedOptionId === widgetOptionId && selectedOptionIndexProp < 0) { return optionIndex; } return selectedOptionIndexProp; }, [selectedOptionIndexProp, widgetOptionId, interactedOptionId, optionIndex]); const votePercentage = (0, _react.useMemo)(() => { if (!(widgetOptions !== null && widgetOptions !== void 0 && widgetOptions.length)) { return 0; } const totalVotes = widgetOptions.reduce((total, option) => option.vote_count + total, 0); if (!totalVotes) { return 0; } const percentage = Math.round(widgetOptions[optionIndex].vote_count / totalVotes * 100); return percentage < 100 ? percentage : 100; }, [widgetOptions, optionIndex]); const onOptionPressHandler = (0, _react.useCallback)(() => { onOptionChange(optionIndex); if (interactedOption) { updateWidgetInteractionAction({ interactionItem: widgetOption }); } else { createWidgetInteractionAction({ interactionItem: widgetOption }); } }, [interactedOption, widgetId, widgetOption, optionIndex, selectedOptionIndex, updateWidgetInteractionAction, createWidgetInteractionAction]); const showWidgetResult = widgetResultState === _types.WidgetResultState.SHOWN; if (!widgetOptions || !widgetOption) { return undefined; } return /*#__PURE__*/_react.default.createElement(OptionComponent, { optionIndex: optionIndex, percentage: votePercentage, optionImage: widgetOption === null || widgetOption === void 0 ? void 0 : widgetOption.image_url, optionText: widgetOption === null || widgetOption === void 0 ? void 0 : widgetOption.description, optionDisabled: optionDisabled, showWidgetResult: showWidgetResult, selectedOptionIndex: selectedOptionIndex, onOptionChange: onOptionPressHandler, styles: OptionComponentStyles, correctOption: correctable && interactedOption ? widgetOption === null || widgetOption === void 0 ? void 0 : widgetOption.is_correct : undefined }); } //# sourceMappingURL=LLWidgetVoteOption.js.map