I'm trying to implement the following redux-thunk actions w/ redux-saga (where fetch is isomorphic-fetch):
export function addPostRequest(post) {
retu (dispatch) => {
fetch(`${baseURL}/api/addPost`, {
method: 'post',
body: JSON.stringify({
post: {
name: post.name,
title: post.title,
content: post.content,
},
}),
headers: new Headers({
'Content-Type': 'application/json',
}),
}).then(res => res.json()).then(res => dispatch(addPost(res.post))).catch(err => console.log(err));
};
}
export function fetchPosts() {
retu (dispatch) => {
retu fetch(`${baseURL}/api/getPosts`)
.then(response => response.json())
.then(response => dispatch(addPosts(response.posts)));
};
}
and I've tried variations on the following to no avail (I realize the fork calls shouldn't contain anon generator functions as I have them set up for sake of clarity but I was prototyping so I wasn't worried about clarity yet):
function callApi(url, requestObj = {}) {
retu fetch(url, requestObj)
.then(res => res.json())
.then(response => response)
.catch(err => console.log(err));
}
function *fetchPosts() {
while (true) {
yield take(ActionTypes.FETCH_POSTS);
const result = yield fork(function *() { //eslint-disable-line
yield call(callApi, `${baseURL}/api/getPosts`);
});
const posts = result.posts;
console.log(posts);
yield put(Actions.addPosts(posts));
}
}
function *addPostRequest() {
while (true) {
const post = yield take(ActionTypes.ADD_POST_REQUEST);
yield fork(function *(post) { //eslint-disable-line
yield call(callApi, `${baseURL}/api/addPost`, {
method: 'post',
body: JSON.stringify({
post: {
name: post.name,
title: post.title,
content: post.content,
},
}),
headers: new Headers({
'Content-Type': 'application/json',
}),
});
}, post);
yield put(Actions.addPost(post));
}
}
export default function *() {
yield [
fork(fetchPosts),
fork(addPostRequest),
];
}
and the default function is called on the server side after the store is initialized (where sagaMiddleware is exported by
function errorHandler(error, getState) {
console.error(error);
console.debug('current state', getState());
}
export const sagaMiddleware = createSagaMiddleware();
export function configureStore(initialState) {
const store = createStore(reducer, initialState, compose(
applyMiddleware(sagaMiddleware, reduxCatch(errorHandler)),
typeof window === 'object' && typeof window.devToolsExtension !== 'undefined' ? window.devToolsExtension() : f => f,
));
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers/post.reducer', () => {
const nextReducer = require('../reducers/post.reducer'); //eslint-disable-line
store.replaceReducer(nextReducer);
});
}
retu store;
}
):
const store = configureStore(initialState);
sagaMiddleware.run(routeSaga);
fetchComponentData(store.dispatch, renderProps.components, renderProps.params)
.then(() => {
const createElement = (Component, props) => (
<Component
{...props}
radiumConfig={{ userAgent: req.headers['user-agent'] }}
/>
);
const initialView = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} createElement={createElement} />
</Provider>
);
const finalState = store.getState().toJS();
res.status(200).end(renderFullPage(initialView, finalState, getFilename()));
})
.catch((err) => {
res.end(renderFullPage(`Error: ${err}`, {}, getFilename()));
});
and on the client side:
const store = configureStore(fromJS(window.__INITIAL_STATE__));
sagaMiddleware.run(routeSaga);
const history = syncHistoryWithStore(browserHistory, store, {
selectLocationState(state) {
retu state.get('route').toJS();
},
});
