I have a saga that posts to a webservice and returns a new sales order number to state. This is working great and looks like this.
//New SALES NUMBER SAGA
function getNewSalesNumber(salesRepId, typeId) {
const url = `${baseUrl}/api/SalesOrder/${salesRepId}?typeId=${typeId}`;
console.log(url);
return axios.post(url);
}
function* callGetNewSalesNumber({salesRepId, typeId, resolve, reject}) {
const result = yield call(getNewSalesNumber, salesRepId, typeId)
if (result.data) {
yield put({type: "NEWSALESNUMBER_FETCHED", result});
// yield call(resolve);
} else {
// yield call(reject);
}
}
function* getNewSalesNumberSaga() {
yield* takeEvery("FETCH_NEWSALESNUMBER", callGetNewSalesNumber);
}
The reducer looks like this.
function newSalesNumber(state = {}, action) {
switch(action.type) {
case 'NEWSALESNUMBER_FETCHED':
return state
.set("orderHeader.orderId", action.result);
default:
return state;
}
}
export default newSalesNumber;
I'm trying to figure out what my options are for returning that value to newSalesNumber (which is now in state) to a Field.
If I was doing this from within the form component I would use something like this...
this.props.dispatch(change('mainForm', 'orderNumber', "testNumber"));
Is there a way to do this from within the saga after the data is received> Can I call dispatch(change) from outside of the connected form (in Saga)?
case 'NEWSALESNUMBER_FETCHED': return state.set(<name of prop>, action.result);
After changing the state in your reducer, the component property mapped to the state throughmapStateToProps
will be automatically updated. – Linson