0.2.22 • Published 9 years ago

redux-saga-form v0.2.22

Weekly downloads
1
License
MIT
Repository
-
Last release
9 years ago

redux-saga-form

npm version

IMPORTANT: redux-saga-form is no longer maintained.

redux-saga-form is an extension of React Redux Form that integrates with redux-saga and enables a strict separation between business and presentation logic.

Why this library?

TODO

Installation

# dependencies
yarn add react redux react-redux redux-thunk redux-saga react-redux-form

yarn add redux-saga-form

(Note: redux-thunk is required by React Redux Form).)

Usage Example

Run the saga included with the library. This only has to be done once:

store.js

import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import createSagaMiddleware from 'redux-saga';
import { formSaga } from 'redux-saga-form';

import reducer from './reducers';
import mySaga from './sagas';


const sagaMiddleware = createSagaMiddleware();
const store = createStore(reducer, applyMiddleware(thunkMiddleware, sagaMiddleware));

sagaMiddleware.run(formSaga);
sagaMiddleware.run(mySaga);

export default store;

Create the form definition:

forms.jsx

import { call } from 'redux-saga/effects';
import { Form, Field, FieldTypes } from 'redux-saga-form';
import validator from 'validator';


export const registerForm = new Form('forms.register', "Create Account")
    .schema({
        first_name: new Field(FieldTypes.text, "First name", {required: true}),
        
        last_name: new Field(FieldTypes.text, "Last name", {required: true}),
        
        email: new Field(FieldTypes.email, "Email", {required: true})
            .validators({isEmail: (val) => !!val && validator.isEmail(val)})
            .messages({isEmail: "Please enter a valid email address."}),
        
        password: new Field(FieldTypes.password, "Password", {required: true})
            .validators({length: (value) => !!value && value.length >= 8})
            .messages({length: "Password must have at least 8 characters."}),
        
        password2: new Field(FieldTypes.password, "Confirm Password", {required: true}),
    })
    .validators({passwordsMatch: (data) => data.password === data.password2})
    .messages({passwordsMatch: "Passwords do not match."})
    .submit(function* (data, props, success, failure) {
        const res = yield call(fetch, '/api/register/', {
            method: 'POST',
            body: data,
        });
        
        if (res.ok) {
            yield success();
        } else {
            yield failure({errors: "An error occured."});
        }
    });

Create the form reducer:

reducers.js

import { combineReducers } from 'redux';
import { combineForms } from 'react-redux-form';

import * as forms from './forms';


export default combineReducers({
    // ...
    forms: combineForms({
        register: forms.registerForm.initialData(),
    }, 'forms'),
});

Create the form saga:

sagas.js

import * as forms from './forms';


export default function* () {
    yield [
        // ...
        forms.registerForm.saga(),
    ];
};

Display the form:

app.jsx

import React from 'react';
import { FormView } from 'redux-saga-form';

import * as forms from './forms';


export default class App extends React.Component {
    render() {
        return <div>
            <FormView form={forms.registerForm} />
        </div>;
    }
}
0.2.22

9 years ago

0.2.21

9 years ago

0.2.20

9 years ago

0.2.19

9 years ago

0.2.18

9 years ago

0.2.17

9 years ago

0.2.16

9 years ago

0.2.15

9 years ago

0.2.14

9 years ago

0.2.13

9 years ago

0.2.12

9 years ago

0.2.11

9 years ago

0.2.10

9 years ago

0.2.9

9 years ago

0.2.8

9 years ago

0.2.7

9 years ago

0.2.6

10 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.16

10 years ago

0.0.15

10 years ago

0.0.14

10 years ago

0.0.13

10 years ago

0.0.12

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago