From e13e630cd6e4fc0b1ff92098a28a770794c7bb9a Mon Sep 17 00:00:00 2001 From: gabrhr <73925454+gabrhr@users.noreply.github.com> Date: Wed, 20 Apr 2022 10:19:29 -0500 Subject: =?UTF-8?q?A=C3=B1adir=20plantilla?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Base para front --- front/odiparpack/app/redux/modules/chat.js | 73 ++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 front/odiparpack/app/redux/modules/chat.js (limited to 'front/odiparpack/app/redux/modules/chat.js') diff --git a/front/odiparpack/app/redux/modules/chat.js b/front/odiparpack/app/redux/modules/chat.js new file mode 100644 index 0000000..e30c97a --- /dev/null +++ b/front/odiparpack/app/redux/modules/chat.js @@ -0,0 +1,73 @@ +import { fromJS, List, Map } from 'immutable'; +import { + FETCH_CHAT_DATA, + SHOW_CHAT, + HIDE_CHAT, + SEND_CHAT, + DELETE_CONVERSATION +} from 'ba-actions/actionTypes'; +import { getDate, getTime } from '../helpers/dateTimeHelper'; + +const initialState = { + chatList: List([]), + activeChat: List([]), + chatSelected: 0, + showMobileDetail: false +}; + +const buildMessage = (message, curData) => { + const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36); + const newData = Map({ + id, + from: 'me', + date: getDate(), + time: getTime(), + message, + }); + return curData.push(newData); +}; + +const initialImmutableState = fromJS(initialState); +export default function reducer(state = initialImmutableState, action = {}) { + switch (action.type) { + case FETCH_CHAT_DATA: + return state.withMutations((mutableState) => { + const items = fromJS(action.items); + mutableState + .set('chatList', items) + .set('activeChat', items.getIn([state.get('chatSelected'), 'chat'])); + }); + case SHOW_CHAT: + return state.withMutations((mutableState) => { + const chatItem = state.get('chatList') + .find(obj => ( + obj.get('with') === action.person.get('id') + )); + const index = state.get('chatList').indexOf(chatItem); + const chatValue = chatItem.get('chat') !== [] ? chatItem.get('chat') : List([]); + mutableState + .set('chatSelected', index) + .set('activeChat', chatValue) + .set('showMobileDetail', true); + }); + case HIDE_CHAT: + return state.withMutations((mutableState) => { + mutableState.set('showMobileDetail', false); + }); + case SEND_CHAT: + return state.withMutations((mutableState) => { + const newMessage = buildMessage(action.message, state.getIn(['chatList', state.get('chatSelected'), 'chat'])); + mutableState + .update('chatList', chatList => chatList.setIn([state.get('chatSelected'), 'chat'], newMessage)) + .set('activeChat', newMessage); + }); + case DELETE_CONVERSATION: + return state.withMutations((mutableState) => { + mutableState + .update('chatList', chatList => chatList.setIn([state.get('chatSelected'), 'chat'], List([]))) + .set('activeChat', List([])); + }); + default: + return state; + } +} -- cgit v1.2.3