0.2.22 • Published 7 years ago

redux-saga-form v0.2.22

Weekly downloads
1
License
MIT
Repository
-
Last release
7 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

7 years ago

0.2.21

7 years ago

0.2.20

7 years ago

0.2.19

7 years ago

0.2.18

7 years ago

0.2.17

7 years ago

0.2.16

7 years ago

0.2.15

7 years ago

0.2.14

7 years ago

0.2.13

7 years ago

0.2.12

7 years ago

0.2.11

7 years ago

0.2.10

7 years ago

0.2.9

7 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.6

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.11

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago