import { getData } from '../../utils/DataTemplate';
import { datasourceApi } from '../../services/datasource';

export default {
  namespace: 'HouseDetail',
  state: {
    singularData: {},
    enableEdit: false,
  },
  reducers: {
    setList(state, { payload }) {
      console.info({ ...state, singularData: payload.singularData });
      return { ...state, singularData: payload.singularData };
    },
    setEnable(state, { payload: enableEdit }) {
      return { ...state, enableEdit };
    },
  },
  effects: {
    *getHouseDetail({ payload }, { call, put }) {
      const coordinate = {
        containerType: 'module',
        containerName: 'query-DSRFWInfo',
        datasourceName: 'QueryfwDetails',
      };
      const result = yield call(getData, coordinate, { pst: 0, psz: 10, params: { pID: payload } });
      const singularData = result[0];
      yield put({ type: 'setList', payload: { singularData: { ...singularData } } });
    },
    *updateHouseDetail({ payload }, { call }) {
      const coordinate = {
        containerType: 'module',
        containerName: 'query-DSRFWInfo',
        datasourceName: 'QueryfwDetails',
      };
      const api = datasourceApi(coordinate);
      console.info({ key: payload.id, params: payload.singularData });
      const result = yield call(api.update, coordinate, { key: payload.id, params: payload.singularData });
      console.info(result);
    },
  },
};
