GithubHelp home page GithubHelp logo

ydeshayes / redux-form-saga Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mhssmnn/redux-form-saga

0.0 2.0 0.0 47 KB

Connecting Redux Form and Redux Saga through a saga.

License: MIT License

JavaScript 100.00%

redux-form-saga's Introduction

redux-form-saga

Connecting Redux Form and Redux Saga through a saga.

Build Status npm version

npm install --save redux-form-saga

Why do I need this?

If you are using Redux Saga and have tried to get Redux Form to play along, then you likely know it doesn't quite work. This provides a solution using an action creator createFormAction('REQUEST') and a saga formActionSaga.

Installation

npm i --save redux-form-saga

Then, to enable Redux Form Saga, add formActionSaga in your sagaMiddleware.run().

Important! If the browser you are targeting doesn't support ES2015 promises, you must provide a valid polyfill, such as the one provided by babel.

Usage

Any form you create using Redux Form can receive an action creator (i.e. requestAction) as a parameter to handleSubmit.

import { createAction } from 'redux-actions';
import { createFormAction } from 'redux-form-saga';

const typePrefix = 'FORM';
const formAction = createFormAction(typePrefix);

// formAction.REQUEST == 'FORM_REQUEST';
// formAction.SUCCESS == 'FORM_SUCCESS';
// formAction.FAILURE == 'FORM_FAILURE';
// formAction.request(payload) == { type: 'FORM_REQUEST', payload };
// formAction.success(payload) == { type: 'FORM_SUCCESS', payload };
// formAction.failure(payload) == { type: 'FORM_FAILURE', payload };

<form onSubmit={handleSubmit(formAction)}>
// ...
</form>

or long form

import { createAction } from 'redux-actions';
import { createFormAction } from 'redux-form-saga';

const REQUEST = 'REQUEST';
const SUCCESS = 'SUCCESS';
const FAILURE = 'FAILURE';

const requestAction = createAction(REQUEST);
const successAction = createAction(SUCCESS);
const failureAction = createAction(FAILURE);

const formAction = createFormAction(requestAction, [SUCCESS, FAILURE]);

// ...

<form onSubmit={handleSubmit(formAction)}>
// ...
</form>

Now you can create a saga to handle the request, success and failure flow and this will be sent back to the form component.

For example, a login form saga:

import { push } from 'react-router-redux';
import { take, put, call } from 'redux-saga/effects';
import { auth } from '...';
import { LOGIN_REQUEST, loginSuccess, loginFailure } from '...';

/**
 * Authentication saga
 */
export function *loginFlow () {
  while (true) {
    let request = yield take(LOGIN_REQUEST);
    let user = ({ username, password } = request.payload);
    let token;

    try {
      token = yield call(auth.login, { user });
      yield put(loginSuccess(token));
    } catch (error) {
      yield put(loginFailure(error));
    }
  }
}

Scripts

$ npm run test

License

MIT

redux-form-saga's People

Contributors

afitiskin avatar gbirke avatar mhssmnn avatar sergeyzwezdin avatar ydeshayes avatar

Watchers

 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.