anorudes / redux-easy-boilerplate

React redux easy boilerplate
MIT License
632 stars 122 forks source link

Add proxy with example #75

Closed j-clark closed 8 years ago

anorudes commented 8 years ago

Hello, @j-clark you can use api middleware for this task.

Example:

in redux/modules/posts:

export default createReducer({
  ['GET_POLICIES_SUCCESS']: (state, { payload }) =>
    state.set('items', payload.policies), // get posts from server
}, initialState);

export const apiGetPolicies = () => ({
  mode: 'GET',
  type: 'GET_POLICIES',
  url: 'policies',
  data: {
    overageAmount: 500000, term: 25,
  },
});

in api route:

app.use('/policies', (req, res) => {
  var urlBits = [
    'http://actuary-development.policygenius.com/policies?date_of_birth=1980-01-21&gender=male&health_profile%5Bcurrently_uses_tobacco%5D=false&health_profile%5Bheight_feet%5D=5&health_profile%5Bheight_inches%5D=8&health_profile%5Bhistory_of_tobacco_use%5D=false&health_profile%5Bweight%5D=180&policy_profile%5Bcoverage_amount%5D=',
    req.query.coverageAmount,
    '&policy_profile%5Bterm_in_years%5D=',
    req.query.term,
    '&state_code=TX'
  ];

   superagent.get(urlBits.join('')).then(({ body }) => {
     res.json({ policies: body });
   });
});

in component:

  componentDidMount() {
    const { apiGetPolicies } = this.props;

    apiGetPolicies();
  }
anorudes commented 8 years ago

I updated comments in source code, hope this helps )