summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/redux/modules/chat.js
blob: e30c97a0fd2230a0b060821b2a0a044a48882bb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
  }
}