import { Model } from 'dva';
import { kyc1 } from './service';
import { message } from 'antd';

export interface IdentityModelState {
  kyc1status: string;
  nameInput: string;
  kyc1Submitting: boolean;
  formData: any;
}

const model: Model = {
  namespace: 'identity',

  state: {
    kyc1status: 'none',
    nameInput: '',
    kyc1Submitting: false,
    formData: ''
  } as IdentityModelState,

  effects: {
    *kyc1submit(_, { put, all, call, select }) {
      const { realname, idNo } = yield select((state:any) => state.identity)
      if (!idNo || !realname) return
      yield put({ type: 'onUpdateState', kyc1Submitting: true })
      try {
        yield call(kyc1, {idNo:idNo, realname:realname})

/*        yield all([
          put({ type: 'onUpdateState', nameInput: '', nameEditable: false }),
          put({ type: 'user/updateUser', payload: { nickname: nameInput } }),
        ]);
*/

      } catch (e) {
        message.error(e.message)
      } finally {
        yield put({ type: 'onUpdateState', kyc1Submitting: false})
      }
    }
  },

  reducers: {
    onUpdateState(state, payload) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default model;
