Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jayphelps/redux-transaction
https://github.com/jayphelps/redux-transaction
Last synced: 21 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/jayphelps/redux-transaction
- Owner: jayphelps
- License: mit
- Created: 2016-05-31T04:11:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-06-20T21:05:04.000Z (over 8 years ago)
- Last Synced: 2024-10-06T20:46:33.385Z (about 1 month ago)
- Language: JavaScript
- Size: 14.6 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redux-transaction (alpha)
As of now, this is just an experiment. I highly recommend you don't use this yet, but feel free to keep an eye on it. It will make a lot more sense later combined with react-redux-transaction + redux-observable.
##### rootReducer.js
```js
const post = (state = { id: null, title: null }, action) => {
switch (action.type) {
case 'FETCH_POST':
case 'FETCH_POST_FULFILLED':
return {
...state,
...action.payload
};default:
return state;
}
};const postsById = (state = {}, action) => {
switch (action.type) {
case 'FETCH_POST':
case 'FETCH_POST_FULFILLED':
const { id } = action.payload;return {
...state,
[id]: post(state[id], action)
};default:
return state;
}
};export default combineReducers({
postsById
});```
##### index.js
```js
import transaction, { pend, fulfill, reject, cancel } from 'redux-transaction';const store = createStore(transaction(rootReducer));
const fetchPost = id => pend({ type: 'FETCH_POST', payload: { id } });
const action = store.dispatch(fetchPost());
store.getState();
/*
{
transactions: {
'1': {
isPending: true,
error: null
}
}
}
*/
setTimeout(() => {
store.dispatch(reject(action, { message: 'SERVER DOES NOT LIKE YOU' }));const state = store.getState();
/*
{
transactions: {
'1': {
isPending: false,
error: { message: 'SERVER DOES NOT LIKE YOU' }
}
}
}
*/selectTransaction(state, action);
/*
{
isPending: false,
error: { message: 'SERVER DOES NOT LIKE YOU' }
}
*/
}, 1000);
```