model.ts 1.52 KB
Newer Older
1
import { fakeSubmitForm } from './service';
陈帅's avatar
陈帅 committed
2 3 4
import { Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { AnyAction } from 'redux';
5

陈帅's avatar
陈帅 committed
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
export interface IStateType {
  current?: string;
  step?: {
    payAccount: string;
    receiverAccount: string;
    receiverName: string;
    amount: string;
  };
}

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: IStateType) => T) => T }
) => void;

export interface ModelType {
  namespace: string;
  state: IStateType;
  effects: {
    submitStepForm: Effect;
  };
  reducers: {
    saveStepFormData: Reducer<IStateType>;
    saveCurrentStep: Reducer<IStateType>;
  };
}

const Model: ModelType = {
34
  namespace: 'BLOCK_NAME_CAMEL_CASE',
35 36

  state: {
37
    current: 'info',
38
    step: {
WhatAKitty's avatar
WhatAKitty committed
39 40 41 42
      payAccount: 'ant-design@alipay.com',
      receiverAccount: 'test@example.com',
      receiverName: 'Alex',
      amount: '500',
43 44 45 46 47 48 49 50 51 52
    },
  },

  effects: {
    *submitStepForm({ payload }, { call, put }) {
      yield call(fakeSubmitForm, payload);
      yield put({
        type: 'saveStepFormData',
        payload,
      });
53 54 55 56
      yield put({
        type: 'saveCurrentStep',
        payload: 'result',
      });
57 58 59 60
    },
  },

  reducers: {
61 62 63 64 65 66 67
    saveCurrentStep(state, { payload }) {
      return {
        ...state,
        current: payload,
      };
    },

68 69 70 71
    saveStepFormData(state, { payload }) {
      return {
        ...state,
        step: {
陈帅's avatar
陈帅 committed
72
          ...state!.step,
73 74 75 76 77 78
          ...payload,
        },
      };
    },
  },
};
陈帅's avatar
陈帅 committed
79 80

export default Model;