Files
antd-admin/src/models/users.js
T
2016-10-20 21:33:30 +08:00

123 lines
2.9 KiB
JavaScript

import { create, remove, update, query } from '../services/users'
import { parse } from 'qs'
export default {
namespace: 'users',
state: {
list: [],
loading: false,
currentItem: {},
modalVisible: false,
modalType: 'create',
pagination:{
showSizeChanger: true,
showQuickJumper: true,
showTotal: total => `共 ${total} 条`,
current:1,
total:null,
}
},
subscriptions: {
setup({ dispatch, history }) {
history.listen(location => {
if (location.pathname === '/users') {
dispatch({
type: 'query',
payload: location.query,
})
}
})
},
},
effects: {
*query({ payload }, { call, put }) {
yield put({ type: 'showLoading' })
const data = yield call(query, parse(payload))
if (data) {
yield put({
type: 'querySuccess',
payload: {
list: data.data,
pagination:{
total: data.page.total,
current: data.page.current,
}
},
})
}
},
*'delete'({ payload }, { call, put }) {
yield put({ type: 'showLoading' })
const data = yield call(remove, { id: payload })
if (data && data.success) {
yield put({
type: 'querySuccess',
payload: {
list: data.data,
pagination:{
total: data.page.total,
current: data.page.current,
}
},
})
}
},
*create({ payload }, { call, put }) {
yield put({ type: 'hideModal' })
yield put({ type: 'showLoading' })
const data = yield call(create, payload)
if (data && data.success) {
yield put({
type: 'querySuccess',
payload: {
list: data.data,
pagination:{
total: data.page.total,
current: data.page.current,
}
},
})
}
},
*update({ payload }, { select, call, put }) {
yield put({ type: 'hideModal' })
yield put({ type: 'showLoading' })
const id = yield select(({ users }) => users.currentItem.id)
const newUser = { ...payload, id }
const data = yield call(update, newUser)
if (data && data.success) {
yield put({
type: 'querySuccess',
payload: {
list: data.data,
pagination:{
total: data.page.total,
current: data.page.current,
}
},
})
}
},
},
reducers: {
showLoading(state) {
return { ...state, loading: true }
},
querySuccess(state, action) {
return { ...state, ...action.payload, loading: false }
},
showModal(state, action) {
return { ...state, ...action.payload, modalVisible: true }
},
hideModal(state) {
return { ...state, modalVisible: false }
},
},
}