summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/redux/modules/email.js
blob: e103316f724dfd39cdca73c496ccd20636df5597 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { fromJS, List, Map } from 'immutable';
import notif from 'ba-api/notifMessage';
import dummyData from 'ba-api/dummyContents';
import {
  FETCH_EMAIL_DATA,
  OPEN_MAIL,
  FILTER_MAIL,
  COMPOSE_MAIL,
  SEND_MAIL,
  DISCARD_MESSAGE,
  SEARCH_MAIL,
  DELETE_MAIL,
  MOVE_TO,
  TOGGLE_STARED,
  CLOSE_NOTIF
} from 'ba-actions/actionTypes';
import { getDate, getTime } from '../helpers/dateTimeHelper';

const initialState = {
  inbox: List([]),
  selectedMail: 0,
  selectedMailId: '',
  keywordValue: '',
  currentPage: 'inbox',
  openFrm: false,
  notifMsg: '',
};

const buildMessage = (to, subject, content, files) => {
  const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
  const newData = Map({
    id,
    date: getDate(),
    time: getTime(),
    avatar: dummyData.user.avatar,
    name: to,
    subject,
    content,
    attachment: files,
    category: 'sent',
    stared: false,
  });
  return newData;
};

const initialImmutableState = fromJS(initialState);
export default function reducer(state = initialImmutableState, action = {}) {
  switch (action.type) {
    case FETCH_EMAIL_DATA:
      return state.withMutations((mutableState) => {
        const items = fromJS(action.items);
        mutableState.set('inbox', items);
      });
    case OPEN_MAIL:
      return state.withMutations((mutableState) => {
        const index = state.get('inbox').indexOf(action.mail);
        mutableState.set('selectedMail', index);
      });
    case FILTER_MAIL:
      return state.withMutations((mutableState) => {
        mutableState.set('currentPage', action.filter);
      });
    case COMPOSE_MAIL:
      return state.withMutations((mutableState) => {
        mutableState.set('openFrm', true);
      });
    case SEND_MAIL:
      return state.withMutations((mutableState) => {
        const newMail = buildMessage(action.to, action.subject, action.content, action.attachment);
        mutableState
          .update('inbox', inbox => inbox.unshift(newMail))
          .set('selectedMailId', '')
          .set('openFrm', false)
          .set('notifMsg', notif.sent);
      });
    case DISCARD_MESSAGE:
      return state.withMutations((mutableState) => {
        mutableState
          .set('openFrm', false)
          .set('selectedMailId', '')
          .set('notifMsg', notif.discard);
      });
    case SEARCH_MAIL:
      return state.withMutations((mutableState) => {
        action.keyword.persist();
        const keyword = action.keyword.target.value.toLowerCase();
        mutableState.set('keywordValue', keyword);
      });
    case DELETE_MAIL:
      return state.withMutations((mutableState) => {
        const index = state.get('inbox').indexOf(action.mail);
        mutableState
          .update('inbox', inbox => inbox.splice(index, 1))
          .set('notifMsg', notif.removed);
      });
    case TOGGLE_STARED:
      return state.withMutations((mutableState) => {
        const index = state.get('inbox').indexOf(action.mail);
        mutableState.update('inbox', inbox => inbox
          .setIn([index, 'stared'], !state.getIn(['inbox', index, 'stared']))
        );
      });
    case MOVE_TO:
      return state.withMutations((mutableState) => {
        const index = state.get('inbox').indexOf(action.mail);
        mutableState
          .update('inbox', inbox => inbox
            .setIn([index, 'category'], action.category)
          )
          .set('notifMsg', notif.labeled);
      });
    case CLOSE_NOTIF:
      return state.withMutations((mutableState) => {
        mutableState.set('notifMsg', '');
      });
    default:
      return state;
  }
}