import * as userService from '../services/userresult';
const methods = {
user: userService.fetch,
orgs: userService.fetchorgs,
repos: userService.fetchrepos,
};
export default {
namespace: 'userresult',
state: {
user: {},
orgs: [],
repos: [],
},
reducers: {
saveUser(state, { payload: { data: user } }) {
return { ...state, user };
},
saveOrgs(state, { payload: { data: orgs } }) {
return { ...state, orgs };
},
saveRepos(state, { payload: { data: repos } }) {
return { ...state, repos };
},
},
effects: {
// 查询用户资料
// 机构
// repos
*fetch({ payload: { username, datakey = 'user' } }, { call, put }) {
const { data } = yield call(methods[datakey], { username });
yield put({
type: 'save',
payload: {
data,
},
});
},
},
在路由监听时候做触发的时机
subscriptions: {
// 监听路由,决定model的数据请求时机
setup({ dispatch, history }) {
return history.listen(({ pathname, query }) => {
if (pathname === '/gitsearch/user/result') {
const username = query.query;
dispatch({ type: 'fetch', payload: { username } });
dispatch({ type: 'fetch', payload: { username, datakey: 'orgs' } });
dispatch({ type: 'fetch', payload: { username, datakey: 'repos' } });
}
});
},
},
};
网友评论