login.js 1.09 KB
Newer Older
1
import { routerRedux } from 'dva/router';
afc163's avatar
afc163 committed
2
import { fakeAccountLogin } from '../services/api';
3 4 5 6 7 8 9 10 11

export default {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
afc163's avatar
afc163 committed
12
    *login({ payload }, { call, put }) {
13 14 15 16
      yield put({
        type: 'changeSubmitting',
        payload: true,
      });
ddcat1115's avatar
ddcat1115 committed
17
      const response = yield call(fakeAccountLogin, payload);
18
      yield put({
19
        type: 'changeLoginStatus',
20 21
        payload: response,
      });
afc163's avatar
afc163 committed
22 23 24 25
      // Login successfully
      if (response.status === 'ok') {
        yield put(routerRedux.push('/'));
      }
26
    },
27
    *logout(_, { put }) {
ddcat1115's avatar
fix #52  
ddcat1115 committed
28
      yield put({
29 30 31 32
        type: 'changeLoginStatus',
        payload: {
          status: false,
        },
ddcat1115's avatar
fix #52  
ddcat1115 committed
33
      });
34
      yield put(routerRedux.push('/user/login'));
ddcat1115's avatar
fix #52  
ddcat1115 committed
35
    },
36 37 38
  },

  reducers: {
39
    changeLoginStatus(state, { payload }) {
40 41 42 43
      return {
        ...state,
        status: payload.status,
        type: payload.type,
afc163's avatar
afc163 committed
44
        submitting: false,
45 46 47 48 49 50 51 52 53 54
      };
    },
    changeSubmitting(state, { payload }) {
      return {
        ...state,
        submitting: payload,
      };
    },
  },
};