Не знаю почему, одного из работников саги не вызывают.
В sagas / login.js - это login
работник, где я вызываю другое действие getProfile
в actions / profile.js .
yield put({ type: ActionTypes.LOGIN_SUCCEEDED, address: account.address });
вызывается и getProfile
действие также вызывается, но getProfile
в sagas / profile.js не вызывается.
Home.jsx
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import login from 'actions/login';
class Home extends Component {
static propTypes = {
login: PropTypes.func,
};
static defaultProps = {
login: () => null,
};
submit = (e) => {
e.preventDefault();
this.props.login(this.key.value);
};
render() {
return (
<div>
<form onSubmit={this.submit}>
<div className="input-group">
<input
type="password"
className="form-control"
ref={el => (this.key = el)}
/>
<button type="submit" className="btn btn-primary">
Login
</button>
</div>
</form>
</div>
);
}
}
const mapDispatchToProps = dispatch => ({
login: key => dispatch(login(key)),
});
export default connect(
null,
mapDispatchToProps,
)(Home);
actions / login.js
import * as ActionTypes from '../constants/actionTypes';
const login = key => ({
type: ActionTypes.LOGIN_REQUESTED,
key,
});
export default login;
actions / profile.js
import * as ActionTypes from '../constants/actionTypes';
const getProfile = id => ({
type: ActionTypes.PROFILE_REQUESTED,
id,
});
export default getProfile;
sagas / index.js
import { all, fork } from 'redux-saga/effects';
import watchLogin from './login';
import watchProfile from './balance';
export default function* rootSaga() {
yield all([
fork(watchLogin),
fork(watchProfile),
]);
}
sagas / login.js
import { fork, put, takeLatest } from 'redux-saga/effects';
import { wallet } from '@cityofzion/neon-js';
import getProfile from 'actions/profile';
import * as ActionTypes from '../constants/actionTypes';
function* login(action) {
const { key } = action;
try {
const account = new wallet.Account(key);
yield call(getProfile, account.id);
yield put({ type: ActionTypes.LOGIN_SUCCEEDED, address: account.address });
} catch (error) {
yield put({ type: ActionTypes.LOGIN_FAILED, message: error.message });
}
}
export default function* watchLogin() {
yield takeLatest(ActionTypes.LOGIN_REQUESTED, login);
}
sagas/profile.js
import { call, put, takeLatest } from 'redux-saga/effects';
import { api, wallet } from '@cityofzion/neon-js';
import * as ActionTypes from '../constants/actionTypes';
function* getProfile(action) {
const { id } = action;
try {
const profile = yield call(
get,
id,
);
yield put({ type: ActionTypes.PROFILE_SUCCEEDED, profile });
} catch (error) {
yield put({ type: ActionTypes.PROFILE_FAILED, message: error.message });
}
}
export default function* watchProfile() {
yield takeLatest(ActionTypes.PROFILE_REQUESTED, getProfile);
}
index.jsx
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import createSagaMiddleware from 'redux-saga';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';
import App from 'components/App';
import reducers from 'state/index';
import sagas from 'sagas/index';
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
combineReducers({
...reducers,
}),
composeWithDevTools(applyMiddleware(
sagaMiddleware,
)),
);
sagaMiddleware.run(sagas);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('app'),
);
package.json
"dependencies": {
"@cityofzion/neon-js": "^3.8.1",
"axios": "^0.18.0",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-slidedown": "^1.3.0",
"redux": "^4.0.0",
"redux-saga": "^0.16.0"
},