diff options
| author | Dayana31 <[email protected]> | 2022-04-21 17:27:08 -0500 |
|---|---|---|
| committer | Dayana31 <[email protected]> | 2022-04-21 17:27:08 -0500 |
| commit | 67c50667678dd0ce4709b29a854f6a47093a1ac5 (patch) | |
| tree | b6f9f39092ad54bf6b815984d32b37d7c7ca67ab /front/odiparpack/app/redux/modules/chat.js | |
| parent | 91140b24f0d49a9f89a080ee063e9eb023a4b73a (diff) | |
| parent | e13e630cd6e4fc0b1ff92098a28a770794c7bb9a (diff) | |
| download | DP1_project-67c50667678dd0ce4709b29a854f6a47093a1ac5.tar.gz DP1_project-67c50667678dd0ce4709b29a854f6a47093a1ac5.tar.bz2 DP1_project-67c50667678dd0ce4709b29a854f6a47093a1ac5.zip | |
Merge branch 'gabshr' into dayana
Diffstat (limited to 'front/odiparpack/app/redux/modules/chat.js')
| -rw-r--r-- | front/odiparpack/app/redux/modules/chat.js | 73 |
1 files changed, 73 insertions, 0 deletions
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; + } +} |
