reducer.js 1.3 KB
Newer Older
dengxiaofeng's avatar
dengxiaofeng committed
1 2
import { fromJS } from 'immutable';

3 4 5 6 7
import {
  LOAD_REPOS,
  LOAD_REPOS_ERROR,
  LOAD_REPOS_SUCCESS,
  SET_AUTHORITY,
8
  SET_USER_MODE,
张烨's avatar
张烨 committed
9
  SET_LOGIN_NAME,
10
} from './constants';
dengxiaofeng's avatar
dengxiaofeng committed
11 12 13 14 15

export const initialState = fromJS({
  loading: false,
  error: false,
  currentUser: false,
16
  auth: [],
dengxiaofeng's avatar
dengxiaofeng committed
17 18 19
  userData: {
    repositories: false,
  },
20
  userMode: '',
张烨's avatar
张烨 committed
21
  loginName: '',
dengxiaofeng's avatar
dengxiaofeng committed
22 23 24 25 26 27 28 29 30 31
});

/* eslint-disable default-case, no-param-reassign */
const appReducer = (state = initialState, action) => {
  switch (action.type) {
    case LOAD_REPOS:
      return state.merge({
        loading: true,
        error: false,
        userData: {
张烨's avatar
张烨 committed
32 33 34
          repositories: false,
        },
      });
dengxiaofeng's avatar
dengxiaofeng committed
35 36 37 38 39
    case LOAD_REPOS_SUCCESS:
      return state.merge({
        loading: false,
        currentUser: action.username,
        userData: {
张烨's avatar
张烨 committed
40 41 42
          repositories: action.repos,
        },
      });
dengxiaofeng's avatar
dengxiaofeng committed
43 44 45 46

    case LOAD_REPOS_ERROR:
      return state.merge({
        error: action.error,
张烨's avatar
张烨 committed
47 48
        loading: false,
      });
49 50 51 52
    case SET_AUTHORITY:
      return state.merge({
        auth: action.auth,
      });
53 54 55 56
    case SET_USER_MODE:
      return state.merge({
        userMode: action.userMode,
      });
张烨's avatar
张烨 committed
57 58 59 60
    case SET_LOGIN_NAME:
      return state.merge({
        loginName: action.loginName,
      });
dengxiaofeng's avatar
dengxiaofeng committed
61 62 63
    default:
      return state;
  }
张烨's avatar
张烨 committed
64
};
dengxiaofeng's avatar
dengxiaofeng committed
65
export default appReducer;