@livelike/react-native
Version:
LiveLike React Native package
420 lines (419 loc) • 13.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getSelectedOptionIndex = getSelectedOptionIndex;
exports.getWidgeUIPhase = getWidgeUIPhase;
exports.getWidgetResultState = getWidgetResultState;
exports.getWidgetRewardsFromRewardTransactions = getWidgetRewardsFromRewardTransactions;
exports.getWidgetUIPhaseOnInteractionUpdate = getWidgetUIPhaseOnInteractionUpdate;
exports.isTimeExpired = isTimeExpired;
exports.widgetStoreActions = exports.widgetStore = void 0;
var _javascript = require("@livelike/javascript");
var _types = require("../types");
var _store = require("./store");
const initialWidgetStoreValue = {};
const widgetStore = exports.widgetStore = (0, _store.createStore)(initialWidgetStoreValue);
const widgetStoreActions = exports.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 = _javascript.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
};
});
}
function getWidgetResultState(widgetState) {
const {
widgetPayload,
widgetInteractions
} = widgetState;
switch (widgetPayload.kind) {
case _javascript.WidgetKind.TEXT_PREDICTION:
case _javascript.WidgetKind.IMAGE_PREDICTION:
case _javascript.WidgetKind.TEXT_NUMBER_PREDICTION:
case _javascript.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' ? _types.WidgetResultState.SHOWN : _types.WidgetResultState.HIDDEN;
}
case _javascript.WidgetKind.TEXT_PREDICTION_FOLLOW_UP:
case _javascript.WidgetKind.IMAGE_PREDICTION_FOLLOW_UP:
case _javascript.WidgetKind.IMAGE_NUMBER_PREDICTION_FOLLOW_UP:
case _javascript.WidgetKind.TEXT_NUMBER_PREDICTION_FOLLOW_UP:
{
return _types.WidgetResultState.SHOWN;
}
default:
{
return widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length ? _types.WidgetResultState.SHOWN : _types.WidgetResultState.HIDDEN;
}
}
}
function getWidgetUIPhaseOnInteractionUpdate(widgetState) {
const {
widgetPayload
} = widgetState;
if (_javascript.SINGLE_INTERACTION_WIDGET_KINDS.includes(widgetPayload.kind)) {
return _types.WidgetUIPhase.SUBMITTED;
} else if (_javascript.MULTI_INTERACTION_WIDGET_KINDS.includes(widgetPayload.kind)) {
return _types.WidgetUIPhase.INTERACTIVE;
}
return undefined;
}
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 _types.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 _javascript.WidgetKind.TEXT_PREDICTION:
case _javascript.WidgetKind.IMAGE_PREDICTION:
{
return followUp && followUp.status === 'published' ? _types.WidgetUIPhase.FOLLOW_UP_PUBLISHED : _types.WidgetUIPhase.INTERACTIVE;
}
case _javascript.WidgetKind.TEXT_NUMBER_PREDICTION:
case _javascript.WidgetKind.IMAGE_NUMBER_PREDICTION:
{
if (followUp && followUp.status === 'published') {
return _types.WidgetUIPhase.FOLLOW_UP_PUBLISHED;
}
return widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length ? _types.WidgetUIPhase.SUBMITTED : _types.WidgetUIPhase.INTERACTIVE;
}
case _javascript.WidgetKind.TEXT_PREDICTION_FOLLOW_UP:
case _javascript.WidgetKind.IMAGE_PREDICTION_FOLLOW_UP:
case _javascript.WidgetKind.IMAGE_NUMBER_PREDICTION_FOLLOW_UP:
case _javascript.WidgetKind.TEXT_NUMBER_PREDICTION_FOLLOW_UP:
{
return _types.WidgetUIPhase.FOLLOW_UP_PUBLISHED;
}
default:
{
return widgetInteractions !== null && widgetInteractions !== void 0 && widgetInteractions.length ? _types.WidgetUIPhase.SUBMITTED : _types.WidgetUIPhase.INTERACTIVE;
}
}
}
function getSelectedOptionIndex(_ref14) {
let {
widgetPayload,
widgetInteractions
} = _ref14;
const {
kind,
choices,
options
} = widgetPayload;
if (_javascript.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 (_javascript.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;
}
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
};
});
}
function isTimeExpired(expiryTimestamp) {
const expiredTime = new Date(expiryTimestamp);
const timeout = expiredTime.getTime() - Date.now();
return timeout <= 0;
}
//# sourceMappingURL=widget.js.map