forked from kuy/redux-saga-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createAction } from 'redux-actions'; | ||
|
||
export const REQUEST_SUBMIT = 'REQUEST_SUBMIT'; | ||
export const SUCCESS_SUBMIT = 'SUCCESS_SUBMIT'; | ||
export const FAILURE_SUBMIT = 'FAILURE_SUBMIT'; | ||
export const requestSubmit = createAction(REQUEST_SUBMIT); | ||
export const successSubmit = createAction(SUCCESS_SUBMIT); | ||
export const failureSubmit = createAction(FAILURE_SUBMIT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import Form from './form'; | ||
import { requestSubmit } from './actions'; | ||
|
||
class App extends Component { | ||
handleSubmit(values) { | ||
return new Promise((resolve, reject) => { | ||
this.props.dispatch(requestSubmit({ values, resolve, reject })); | ||
}); | ||
} | ||
|
||
render() { | ||
return <div> | ||
<h1> | ||
<a href="https://github.com/kuy/redux-saga-examples/tree/master/form">Form</a> from <a href="https://github.com/kuy/redux-saga-examples">redux-saga-examples</a> by <a href="https://twitter.com/kuy">@kuy</a> | ||
</h1> | ||
<Form onSubmit={this.handleSubmit.bind(this)} /> | ||
</div>; | ||
} | ||
} | ||
|
||
function select({ app }) { | ||
return { app }; | ||
} | ||
|
||
export default connect(select)(App); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { Component, PropTypes } from 'react' | ||
import { reduxForm } from 'redux-form' | ||
|
||
const fields = ['first', 'last']; | ||
|
||
class Form extends Component { | ||
static get propTypes() { | ||
return { | ||
fields: PropTypes.object.isRequired, | ||
handleSubmit: PropTypes.func.isRequired, | ||
submitting: PropTypes.bool.isRequired | ||
}; | ||
} | ||
|
||
render() { | ||
const { | ||
fields: { first, last }, | ||
handleSubmit, | ||
submitting | ||
} = this.props; | ||
return (<form onSubmit={handleSubmit}> | ||
<div> | ||
<label>First Name</label> | ||
<div> | ||
<input type="text" placeholder="First Name" {...first}/> | ||
</div> | ||
</div> | ||
<div> | ||
<label>Last Name</label> | ||
<div> | ||
<input type="text" placeholder="Last Name" {...last}/> | ||
</div> | ||
</div> | ||
<div> | ||
<button type="submit" disabled={submitting}> | ||
{submitting ? <i/> : <i/>} Submit | ||
</button> | ||
</div> | ||
</form> | ||
) | ||
} | ||
} | ||
|
||
export default reduxForm({ | ||
form: 'redux', | ||
fields | ||
})(Form); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import 'babel-polyfill'; | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { Provider } from 'react-redux'; | ||
import App from './app'; | ||
import configureStore from './store'; | ||
|
||
ReactDOM.render( | ||
<Provider store={configureStore()}> | ||
<App /> | ||
</Provider>, | ||
document.getElementById('container')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { combineReducers } from 'redux'; | ||
import { reducer as form } from 'redux-form'; | ||
|
||
const initial = { | ||
app: {} | ||
}; | ||
|
||
function app(state = initial.app, { type, payload }) { | ||
switch (type) { | ||
} | ||
return state; | ||
} | ||
|
||
export default combineReducers( | ||
{ app, form } | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { call, fork, take, put } from 'redux-saga/effects'; | ||
import { REQUEST_SUBMIT, successSubmit, failureSubmit } from './actions'; | ||
|
||
function submit(data) { | ||
// Dummy API calling | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve({ data: 'done' }); | ||
}, 2000); | ||
}) | ||
.then(data => ({ data })) | ||
.catch(error => ({ error })); | ||
} | ||
|
||
function* handleSubmit() { | ||
const { payload: { values, resolve, reject } } = yield take(REQUEST_SUBMIT); | ||
const { data, error } = yield call(submit, values); | ||
if (data && !error) { | ||
resolve(); | ||
yield put(successSubmit({ data })); | ||
} else { | ||
reject(); | ||
yield put(failureSubmit({ error })); | ||
} | ||
} | ||
|
||
export default function* rootSaga() { | ||
yield fork(handleSubmit); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createStore, applyMiddleware } from 'redux'; | ||
import createSagaMiddleware from 'redux-saga'; | ||
import logger from 'redux-logger'; | ||
import reducer from './reducers'; | ||
import rootSaga from './sagas'; | ||
|
||
export default function configureStore(initialState) { | ||
const sagaMiddleware = createSagaMiddleware(); | ||
const store = createStore( | ||
reducer, | ||
initialState, | ||
applyMiddleware( | ||
sagaMiddleware, logger() | ||
) | ||
); | ||
sagaMiddleware.run(rootSaga); | ||
return store; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> | ||
<title>Form</title> | ||
</head> | ||
<body> | ||
<div id="container"></div> | ||
<script src="/in-memory/form.bundle.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters