/*
 * @Author: your name
 * @Date: 2020-11-19 16:54:53
 * @LastEditTime: 2020-12-14 18:28:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tostumi\src\models\init.ts
 */
import * as service from '../services/Init';
import { message } from 'antd';

import { routerRedux } from 'dva/router';

export default {
  namespace: 'Init',
  state: {
    CommunityList: null,
  },

  reducers: {
    returnCommunityList(state, { CommunityList }) {
      return { ...state, CommunityList };
    },
  },

  effects: {
    //获取
    *tosCommunityget({ playload }, { call, put }) {
      const resp = yield call(service.tosCommunityget, playload);
      let CommunityList = resp.data.communityList;
      localStorage.setItem('CommunityList', JSON.stringify(CommunityList));
      yield put({ type: 'returnCommunityList', CommunityList });
    },

    // 新增，
    *addCommunityget({ playload }, { call, put, select }) {
      const newData = playload ? playload.Result : []; // 如果是null 就是空数组
      const village = yield select((state) => state.Init.CommunityList);
      const CommunityList = [...village, ...newData];
      localStorage.setItem('CommunityList', JSON.stringify(CommunityList));
      yield put({ type: 'returnCommunityList', CommunityList });
    },
  },
};
