This commit is contained in:
zuiidea
2016-10-10 22:25:56 +08:00
parent fda47b362c
commit 34ccb19f4c
11 changed files with 393 additions and 24 deletions
-24
View File
@@ -1,24 +0,0 @@
export default {
namespace: 'example',
state: {},
subscriptions: {
setup({ dispatch, history }) {
},
},
effects: {
*fetchRemote({ payload }, { call, put }) {
},
},
reducers: {
fetch(state, action) {
return { ...state, ...action.payload };
},
},
}
+144
View File
@@ -0,0 +1,144 @@
import {create, remove, update, query} from '../services/users'
import {parse} from 'qs'
export default {
namespace : 'users',
state : {
list: [],
loading: false,
total: null,
current: 1,
currentItem: {},
modalVisible: false,
modalType: 'create'
},
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,
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: 'deleteSuccess', payload})
}
},
*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: 'createSuccess', payload})
}
},
*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: 'updateSuccess', payload: newUser})
}
}
},
reducers : {
showLoading(state) {
return {
...state,
loading: true
}
},
createSuccess(state, action) {
const newUser = action.payload
return {
...state,
list: [
newUser, ...state.list
],
loading: false
}
},
deleteSuccess(state, action) {
const id = action.payload
const newList = state.list.filter(user => user.id !== id)
return {
...state,
list: newList,
loading: false
}
},
updateSuccess(state, action) {
const updateUser = action.payload
const newList = state.list.map(user => {
if (user.id === updateUser.id) {
return {
...user,
...updateUser
}
}
return user
})
return {
...state,
list: newList,
loading: false
}
},
querySuccess(state, action) {
return {
...state,
...action.payload,
loading: false
}
},
showModal(state, action) {
return {
...state,
...action.payload,
modalVisible: true
}
},
hideModal(state) {
return {
...state,
modalVisible: false
}
}
}
}