Giter Site home page Giter Site logo

react-redux-transaction's Introduction

react-redux-transaction (alpha)

As of now, this is just an experiment. React helpers for redux-transaction.

Post.js
import { model } from 'react-redux-transaction';
import { fetchPost } from '../actions';

const Post = ({ id, title, transaction: { isPending, error } }) => (
  <div>
    <h1>
      {isPending
        ? 'Loading...'
        : error
          ? 'ERROR: ' + error.message
          : title
      }
    </h1>
  </div>
);

export default model({
  mapStateToActionProps: (state, props) => ({ id: props.params.id }),
  mapActionPropsToAction: ({ id }) => fetchPost(id),
  mapStateToProps: (state, props) => ({ ...state.postsById[props.params.id] })
})(Post);
actions.js
import { pend, fulfill, reject, cancel } from 'redux-transaction';

// Example using redux-thunk or similar

export const fetchPost = id =>
  dispatch => {
    // the action POJO is used as a handle to fulfill/reject/cancel that
    // transaction later
  	const pendingAction = dispatch(pend({ type: 'FETCH_POST', payload: { id } }));

    // fake AJAX
  	setTimeout(() => {
  	  // dispatch(fulfill(pendingAction, { id, title: 'Example post' }));
  	  dispatch(reject(pendingAction, { message: 'THE SERVER HATES YOU' }));
  	}, 2000);
	
	// return the pending action so that the @model() decorator
	// can automatically cancel() it on props change/unmount
  	return pendingAction;
  };
rootReducer.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
import thunk from 'redux-thunk';
import { reducerEnhancer } from 'redux-transaction';

const store = createStore(
  reducerEnhancer(rootReducer),
  applyMiddleware(thunk)
);

// etc...

react-redux-transaction's People

Contributors

jayphelps avatar

Stargazers

Yasin ATEŞ avatar Brandon Smith avatar Tim Branyen avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.