UNPKG

@livelike/react-native

Version:

LiveLike React Native package

408 lines 13.1 kB
import { CHOICE_WIDGET_KIND, MULTI_INTERACTION_WIDGET_KINDS, OPTION_WIDGET_KIND, SINGLE_INTERACTION_WIDGET_KINDS, WidgetKind } from '@livelike/javascript'; import { WidgetResultState, WidgetUIPhase } from '../types'; import { createStore } from './store'; const initialWidgetStoreValue = {}; export const widgetStore = createStore(initialWidgetStoreValue); export const widgetStoreActions = { updateWidgetStateAction(_ref) { let { widgetId, widgetState } = _ref; const prevWidgetState = widgetStore.get()[widgetId]; widgetStore.set({ ...widgetStore.get(), [widgetId]: { ...prevWidgetState, ...widgetState } }); }, updateWidgetChoicesAction(_ref2) { let { widgetId, widgetChoices } = _ref2; const widgetState = widgetStore.get()[widgetId]; if (!widgetState) { throw new Error(`Error while updating widget choices, widget not found for widgetId=${widgetId}`); } // check if there's any choice change const changedChoices = widgetState.widgetPayload.choices.filter((choice, index) => { return Object.entries(widgetChoices[index]).some(_ref3 => { let [key, value] = _ref3; return choice[key] !== widgetChoices[key]; }); }); if (!changedChoices.length) { return; } widgetStore.set({ ...widgetStore.get(), [widgetId]: { ...widgetState, widgetPayload: { ...widgetState.widgetPayload, choices: widgetChoices.map((choice, index) => { var _widgetState$widgetPa; return { ...((_widgetState$widgetPa = widgetState.widgetPayload) === null || _widgetState$widgetPa === void 0 || (_widgetState$widgetPa = _widgetState$widgetPa.choices) === null || _widgetState$widgetPa === void 0 ? void 0 : _widgetState$widgetPa[index]), ...choice }; }) } } }); }, updateWidgetOptionsAction(_ref4) { let { widgetId, widgetOptions } = _ref4; const widgetState = widgetStore.get()[widgetId]; if (!widgetState) { throw new Error(`Error while updating widget options, widget not found for widgetId=${widgetId}`); } // check if there's any option vote change const changedOptions = widgetState.widgetPayload.options.filter((option, index) => { return Object.entries(widgetOptions[index]).some(_ref5 => { let [key, value] = _ref5; return option[key] !== widgetOptions[index][key]; }); }); if (!changedOptions.length) { return; } widgetStore.set({ ...widgetStore.get(), [widgetId]: { ...widgetState, widgetPayload: { ...widgetState.widgetPayload, options: widgetOptions.map((option, index) => { var _widgetState$widgetPa2; return { ...((_widgetState$widgetPa2 = widgetState.widgetPayload) === null || _widgetState$widgetPa2 === void 0 || (_widgetState$widgetPa2 = _widgetState$widgetPa2.options) === null || _widgetState$widgetPa2 === void 0 ? void 0 : _widgetState$widgetPa2[index]), ...option }; }) } } }); }, updateWidgetUIPhaseAction(_ref6) { let { widgetId, widgetUIPhase } = _ref6; const widgetState = widgetStore.get()[widgetId]; if (!widgetState) { throw new Error('Error while updating widget phase, Widget Id not found'); } widgetStore.set({ ...widgetStore.get(), [widgetId]: { ...widgetState, widgetUIPhase } }); }, updateWidgetInteractionAction(_ref7) { let { widgetId, widgetInteractions } = _ref7; const widgetState = widgetStore.get()[widgetId]; if (!widgetState) { throw new Error(`Error while updating widget interaction, widget state not found for widgetId=${widgetId}`); } widgetStore.set({ ...widgetStore.get(), [widgetId]: { ...widgetState, widgetInteractions, widgetResultState: getWidgetResultState({ ...widgetState, widgetInteractions }), widgetUIPhase: getWidgetUIPhaseOnInteractionUpdate(widgetState) } }); }, updateSelectedOptionIndexAction(_ref8) { let { widgetId, selectedOptionIndex } = _ref8; const widgetState = widgetStore.get()[widgetId]; if (!widgetState) { throw new Error(`Error while updating widget selected option, widget state not found for widgetId=${widgetId}`); } const { widgetPayload, selectedOptionIndex: prevSelectedOptionIndex } = widgetState; const { options, choices, kind } = widgetPayload; const isChoiceWidgetKind = CHOICE_WIDGET_KIND.includes(kind); const updatedChoicesOrOptions = isChoiceWidgetKind ? getUpdatedChoices({ choices, selectedOptionIndex, prevSelectedOptionIndex }) : getUpdatedOptions({ options, selectedOptionIndex, prevSelectedOptionIndex }); widgetStore.set({ ...widgetStore.get(), [widgetId]: { ...widgetState, widgetPayload: { ...widgetState.widgetPayload, [isChoiceWidgetKind ? 'choices' : 'options']: updatedChoicesOrOptions }, selectedOptionIndex } }); }, updateWidgetResultStateAction(_ref9) { let { widgetId, widgetResultState } = _ref9; const widgetState = widgetStore.get()[widgetId]; if (!widgetState) { throw new Error(`Error while updating widget selected option, widget state not found for widgetId=${widgetId}`); } widgetStore.set({ ...widgetStore.get(), [widgetId]: { ...widgetState, widgetResultState } }); }, updateWidgetRewardsAction(_ref10) { let { widgetId, widgetRewards } = _ref10; const widgetState = widgetStore.get()[widgetId]; if (!widgetState) { throw new Error(`Error while updating widget selected option, widget state not found for widgetId=${widgetId}`); } widgetStore.set({ ...widgetStore.get(), [widgetId]: { ...widgetState, widgetRewards } }); }, updateWidgetAverageMagnitude(_ref11) { let { widgetId, averageMagnitude } = _ref11; const widgetState = widgetStore.get()[widgetId]; if (!widgetState) { throw new Error(`Error while updating widget selected option, widget state not found for widgetId=${widgetId}`); } widgetStore.set({ ...widgetStore.get(), [widgetId]: { ...widgetState, widgetPayload: { ...widgetState.widgetPayload, average_magnitude: averageMagnitude } } }); } }; function getUpdatedOptions(_ref12) { let { options, prevSelectedOptionIndex, selectedOptionIndex } = _ref12; let validOptionUpdate = true; const updatedOptions = options.map((option, index) => { let { vote_count } = option; if (index === selectedOptionIndex) { vote_count += 1; } else if (index === prevSelectedOptionIndex) { vote_count -= 1; } if (vote_count < 0) { validOptionUpdate = false; } return { ...option, vote_count }; }); return validOptionUpdate ? updatedOptions : options; } function getUpdatedChoices(_ref13) { let { choices, prevSelectedOptionIndex, selectedOptionIndex } = _ref13; return choices.map((choice, index) => { let { answer_count } = choice; if (index === selectedOptionIndex) { answer_count += 1; } else if (index === prevSelectedOptionIndex) { answer_count -= 1; } return { ...choice, answer_count }; }); } export function getWidgetResultState(widgetState) { const { widgetPayload, widgetInteractions } = widgetState; switch (widgetPayload.kind) { case WidgetKind.TEXT_PREDICTION: case WidgetKind.IMAGE_PREDICTION: case WidgetKind.TEXT_NUMBER_PREDICTION: case WidgetKind.IMAGE_NUMBER_PREDICTION: { var _widgetPayload$follow, _widgetPayload$follow2; const followUp = widgetPayload === null || widgetPayload === void 0 || (_widgetPayload$follow = widgetPayload.follow_ups) === null || _widgetPayload$follow === void 0 ? void 0 : _widgetPayload$follow[(widgetPayload === null || widgetPayload === void 0 || (_widgetPayload$follow2 = widgetPayload.follow_ups) === null || _widgetPayload$follow2 === void 0 ? void 0 : _widgetPayload$follow2.length) - 1]; return widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length && followUp && followUp.status === 'published' ? WidgetResultState.SHOWN : WidgetResultState.HIDDEN; } case WidgetKind.TEXT_PREDICTION_FOLLOW_UP: case WidgetKind.IMAGE_PREDICTION_FOLLOW_UP: case WidgetKind.IMAGE_NUMBER_PREDICTION_FOLLOW_UP: case WidgetKind.TEXT_NUMBER_PREDICTION_FOLLOW_UP: { return WidgetResultState.SHOWN; } default: { return widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length ? WidgetResultState.SHOWN : WidgetResultState.HIDDEN; } } } export function getWidgetUIPhaseOnInteractionUpdate(widgetState) { const { widgetPayload } = widgetState; if (SINGLE_INTERACTION_WIDGET_KINDS.includes(widgetPayload.kind)) { return WidgetUIPhase.SUBMITTED; } else if (MULTI_INTERACTION_WIDGET_KINDS.includes(widgetPayload.kind)) { return WidgetUIPhase.INTERACTIVE; } return undefined; } export function getWidgeUIPhase(widgetState) { var _widgetPayload$follow3, _widgetPayload$follow4; const { widgetPayload, widgetInteractions } = widgetState; if (widgetPayload !== null && widgetPayload !== void 0 && widgetPayload.interactive_until && isTimeExpired(widgetPayload === null || widgetPayload === void 0 ? void 0 : widgetPayload.interactive_until)) { return WidgetUIPhase.EXPIRED; } const followUp = widgetPayload === null || widgetPayload === void 0 || (_widgetPayload$follow3 = widgetPayload.follow_ups) === null || _widgetPayload$follow3 === void 0 ? void 0 : _widgetPayload$follow3[(widgetPayload === null || widgetPayload === void 0 || (_widgetPayload$follow4 = widgetPayload.follow_ups) === null || _widgetPayload$follow4 === void 0 ? void 0 : _widgetPayload$follow4.length) - 1]; switch (widgetPayload.kind) { case WidgetKind.TEXT_PREDICTION: case WidgetKind.IMAGE_PREDICTION: { return followUp && followUp.status === 'published' ? WidgetUIPhase.FOLLOW_UP_PUBLISHED : WidgetUIPhase.INTERACTIVE; } case WidgetKind.TEXT_NUMBER_PREDICTION: case WidgetKind.IMAGE_NUMBER_PREDICTION: { if (followUp && followUp.status === 'published') { return WidgetUIPhase.FOLLOW_UP_PUBLISHED; } return widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length ? WidgetUIPhase.SUBMITTED : WidgetUIPhase.INTERACTIVE; } case WidgetKind.TEXT_PREDICTION_FOLLOW_UP: case WidgetKind.IMAGE_PREDICTION_FOLLOW_UP: case WidgetKind.IMAGE_NUMBER_PREDICTION_FOLLOW_UP: case WidgetKind.TEXT_NUMBER_PREDICTION_FOLLOW_UP: { return WidgetUIPhase.FOLLOW_UP_PUBLISHED; } default: { return widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length ? WidgetUIPhase.SUBMITTED : WidgetUIPhase.INTERACTIVE; } } } export function getSelectedOptionIndex(_ref14) { let { widgetPayload, widgetInteractions } = _ref14; const { kind, choices, options } = widgetPayload; if (CHOICE_WIDGET_KIND.includes(kind)) { return choices.findIndex(_ref15 => { let { id } = _ref15; return widgetInteractions === null || widgetInteractions === void 0 ? void 0 : widgetInteractions.find(_ref16 => { let { choice_id } = _ref16; return choice_id === id; }); }); } else if (OPTION_WIDGET_KIND.includes(kind)) { return options.findIndex(_ref17 => { let { id } = _ref17; return widgetInteractions === null || widgetInteractions === void 0 ? void 0 : widgetInteractions.find(_ref18 => { let { option_id } = _ref18; return option_id === id; }); }); } return -1; } export function getWidgetRewardsFromRewardTransactions(rewardTransactions) { return rewardTransactions.map(_ref19 => { let { reward_item_amount, reward_item_name, reward_item_id, reward_action_key } = _ref19; return { reward_item_amount, reward_item_name, reward_item_id, reward_action_key }; }); } export function isTimeExpired(expiryTimestamp) { const expiredTime = new Date(expiryTimestamp); const timeout = expiredTime.getTime() - Date.now(); return timeout <= 0; } //# sourceMappingURL=widget.js.map