import { calcModuleDatasource } from '../services/datasource';

export default {
  namespace: 'HouseDetail',
  state: {
    sourceData: [],
  },
  reducers: {
    setList(state, { payload: { arrayData } }) {
      return { ...state, sourceData: [...state.sourceData, ...arrayData] };
    },
    // 下拉刷新用到的，清空列表的数据
    clearList(state) {
      return { ...state, sourceData: [] };
    },
  },
  effects: {
    *getHouseList({ payload }, { take, put, call, select }) {
      if (payload === 'clear') {
        yield put({ type: 'clearList', payload: {} });
      }
      const { sourceData } = yield select(state => state.ListHouse);
      console.info(sourceData);
      const size = sourceData.length;
      const result = yield call(calcModuleDatasource, 'query-DSRFWInfo', 'QueryfwTable', { pst: size, psz: 10 });
      for (const row of result) {
        yield put({ type: 'addHouse', payload: row });
        yield take('addHouse/@@end');
      }
    },
    *addHouse({ payload }, { put }) {
      yield put({ type: 'setList', payload: { arrayData: [payload] } });
    },
  },
};
