calamarcopollo
Version:
Save the chicken foundation
53 lines (47 loc) • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.default = function (state, action) {
const { type, payload } = action;
const { text, chat, date } = payload || {};
const chatId = chat && chat.id ? chat.id : null;
const expressionIndex = state.expressions.findIndex(t => t === text);
const outcomeIndex = state.outcomes.findIndex(item => item && item.text === text);
const chatIndex = state.chats.findIndex(item => item.id === chatId);
const oldChat = chatIndex >= 0 ? state.chats[chatIndex] : null;
const lastDate = oldChat ? oldChat.date : Date.now();
const dateInput = date || Date.now();
const newDate = dateInput.toString().length < 13 ? dateInput * 1000 : dateInput;
switch (type) {
case _actionTypes.UPDATE_EXPRESSION:
return _extends({}, state, {
expressions: updateArrayItem(state.expressions, expressionIndex, text)
});
case _actionTypes.UPDATE_OUTCOME:
return _extends({}, state, {
outcomes: updateArrayItem(state.outcomes, outcomeIndex, payload)
});
case _actionTypes.UPDATE_CHAT_SESSION:
if (!chat.session) {
chat.session = oldChat && oldChat.session ? oldChat.session : {};
}
if (newDate - lastDate > expireSessionTime) {
console.warn('session expired, reset session');
chat.session = {};
}
chat.date = newDate;
return _extends({}, state, {
chats: updateArrayItem(state.chats, chatIndex, chat)
});
default:
return state;
}
};
var _actionTypes = require('./actionTypes');
const expireSessionTime = 3 * 60 * 1000; // 3 minutes
const updateArrayItem = (arr, i, newItem) => {
const shouldReplaceItem = i > -1 && newItem !== null;
return shouldReplaceItem ? [...arr.slice(0, i), newItem, ...arr.slice(i + 1)] : [newItem, ...arr];
};