text: import { query,add,remove,edit,connect,command } from '../services/valve_sensors';
  
  export default {
    namespace: 'valve_sensors',
    state: {
      data:[],
      meta: {pagination: {total: 0, per_page: 0}},
      loading: true,
      name:''
  
    },
    effects: {
      *fetch({ payload,callback }, { call, put }) {
        yield put({
          type: 'changeLoading',
          payload: true,
        });
        const response = yield call(query, payload);
        console.log(response)
        if(response.status===200){
          if(response.data.meta){
            yield put({
              type: 'save',
              payload:  response.data
            });
            if(document.querySelector('.ant-table-body')){
              document.querySelector('.ant-table-body').scrollTop=0;
            }else{
              console.log('没找到表格')
            }
            yield put({
              type: 'changeLoading',
              payload: false,
            });
          }else{
            yield put({
              type: 'save',
              payload:  {
                data:response.data.data,
                meta: {pagination: {total: 0, per_page: 0}},
              }
            });
            if(document.querySelector('.ant-table-body')){
              document.querySelector('.ant-table-body').scrollTop=0;
            }else{
              console.log('没找到表格')
            }
            yield put({
              type: 'changeLoading',
              payload: false,
            });
          }
          if(callback) callback()
        }
  
      },
      *fetchAndPush({ payload,callback }, { call, put }) {
        const response = yield call(query, payload);
        console.log(response)
        if(response.status===200){
          yield put({
            type: 'saveAndPush',
            payload:  response.data
          });
          if (callback) callback();
        }
      },
      *add({ payload, callback }, { call, put }) {
        const response = yield call(add, payload);
        console.log(response)
        if(response.status===200){
          if (callback) callback();
        }
      },
      *connect({ payload, callback }, { call, put }) {
        const response = yield call(connect, payload);
        console.log(response)
        if(response.status===200){
          if (callback) callback();
        }
      },
      *command({ payload, callback }, { call, put }) {
        const response = yield call(command, payload);
        console.log(response)
        if(response.status===200){
          if (callback) callback();
        }
      },
      *edit({ payload, callback }, { call, put }) {
        const response = yield call(edit, payload);
        console.log(response)
        if(response.status===200){
          if (callback) callback();
        }
      },
      *remove({ payload, callback }, { call, put }) {
        const response = yield call(remove, payload);
        if(response.status===200){
          if (callback) callback();
        }
      },
    },
  
    reducers: {
      save(state, action) {
        return {
          ...state,
          data: action.payload.data,
          meta:action.payload.meta
        };
      },
      saveAndPush(state, action) {
        // console.log('[...state.data,...action.payload.data]',[...state.data,...action.payload.data]);
        const data=[...state.data,...action.payload.data];
        // data.map((item,index)=>{
        //   item.index=index
        // })
        return {
          ...state,
          data: data,
          meta:action.payload.meta
        };
      },
      changeLoading(state, action) {
        return {
          ...state,
          loading: action.payload,
        };
      },
      saveName(state,action){
        return {
          ...state,
          name: action.payload,
        };
      }
    },
  };
  