From 4bd30c87136bffcd15e1c31fbd8fa110dfa5ea78 Mon Sep 17 00:00:00 2001 From: estrada9166 Date: Tue, 19 Sep 2017 14:24:28 -0500 Subject: [PATCH] Example: Form handler with Redux and React-bootstrap (#2949) * Example: Form handler with Redux and React-bootstrap * inputChange refactoring * Destructuring inputChange --- examples/form-handler/README.md | 30 +++++++++++++++ .../actions/formActionsCreators.js | 5 +++ examples/form-handler/actions/index.js | 1 + .../form-handler/components/DisplayForm.js | 26 +++++++++++++ examples/form-handler/components/Header.js | 17 +++++++++ examples/form-handler/components/Social.js | 26 +++++++++++++ examples/form-handler/components/UserForm.js | 26 +++++++++++++ examples/form-handler/components/index.js | 34 +++++++++++++++++ examples/form-handler/constants/index.js | 1 + examples/form-handler/handlers/Input.js | 38 +++++++++++++++++++ examples/form-handler/package.json | 26 +++++++++++++ examples/form-handler/pages/index.js | 14 +++++++ examples/form-handler/reducers/formReducer.js | 15 ++++++++ examples/form-handler/reducers/index.js | 6 +++ examples/form-handler/server.js | 28 ++++++++++++++ examples/form-handler/store.js | 7 ++++ 16 files changed, 300 insertions(+) create mode 100644 examples/form-handler/README.md create mode 100644 examples/form-handler/actions/formActionsCreators.js create mode 100644 examples/form-handler/actions/index.js create mode 100644 examples/form-handler/components/DisplayForm.js create mode 100644 examples/form-handler/components/Header.js create mode 100644 examples/form-handler/components/Social.js create mode 100644 examples/form-handler/components/UserForm.js create mode 100644 examples/form-handler/components/index.js create mode 100644 examples/form-handler/constants/index.js create mode 100644 examples/form-handler/handlers/Input.js create mode 100644 examples/form-handler/package.json create mode 100644 examples/form-handler/pages/index.js create mode 100644 examples/form-handler/reducers/formReducer.js create mode 100644 examples/form-handler/reducers/index.js create mode 100644 examples/form-handler/server.js create mode 100644 examples/form-handler/store.js diff --git a/examples/form-handler/README.md b/examples/form-handler/README.md new file mode 100644 index 00000000..bd9ea3c2 --- /dev/null +++ b/examples/form-handler/README.md @@ -0,0 +1,30 @@ +[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/form-handler) + +# Form Handler + +## How to use + +Download the example [or clone the repo](https://github.com/zeit/next.js): + +```bash +curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/form-handler +cd form-handler +``` + +Install it and run: + +```bash +npm install +npm run dev +``` + +Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) + +```bash +now +``` + +## The idea behind the example + +Sometimes handle multiple forms can be tricky, the idea is to have a global reducer +with the name of each form and the inputs of it; making accessible everywhere. diff --git a/examples/form-handler/actions/formActionsCreators.js b/examples/form-handler/actions/formActionsCreators.js new file mode 100644 index 00000000..bdc12364 --- /dev/null +++ b/examples/form-handler/actions/formActionsCreators.js @@ -0,0 +1,5 @@ +import { INPUT_VALUE } from '../constants' + +export const inputChange = (title, name, val) => dispatch => { + return dispatch({type: INPUT_VALUE, title, name, val}) +} diff --git a/examples/form-handler/actions/index.js b/examples/form-handler/actions/index.js new file mode 100644 index 00000000..3811c44a --- /dev/null +++ b/examples/form-handler/actions/index.js @@ -0,0 +1 @@ +export * from './formActionsCreators' diff --git a/examples/form-handler/components/DisplayForm.js b/examples/form-handler/components/DisplayForm.js new file mode 100644 index 00000000..1cb6643c --- /dev/null +++ b/examples/form-handler/components/DisplayForm.js @@ -0,0 +1,26 @@ +import React, {Component} from 'react' +import { Col, Row } from 'react-bootstrap' +import { connect } from 'react-redux' + +class DisplayForm extends Component { + render () { + const { state } = this.props + return ( +
+ + +
{JSON.stringify(state, null, 2) }
+ +
+
+ ) + } +} + +const mapStateToProps = state => { + return { + state + } +} + +export default connect(mapStateToProps)(DisplayForm) diff --git a/examples/form-handler/components/Header.js b/examples/form-handler/components/Header.js new file mode 100644 index 00000000..b71b74e8 --- /dev/null +++ b/examples/form-handler/components/Header.js @@ -0,0 +1,17 @@ +import { Col, Row, Jumbotron } from 'react-bootstrap' + +const Header = () => { + return ( +
+ + + +

Form Handler

+
+ +
+
+ ) +} + +export default Header diff --git a/examples/form-handler/components/Social.js b/examples/form-handler/components/Social.js new file mode 100644 index 00000000..6f4ea2d7 --- /dev/null +++ b/examples/form-handler/components/Social.js @@ -0,0 +1,26 @@ +import { Col, Row } from 'react-bootstrap' + +import Input from '../handlers/Input' + +const Social = () => { + return ( +
+ + + + + + + + + + + + + + +
+ ) +} + +export default Social diff --git a/examples/form-handler/components/UserForm.js b/examples/form-handler/components/UserForm.js new file mode 100644 index 00000000..aefb43d2 --- /dev/null +++ b/examples/form-handler/components/UserForm.js @@ -0,0 +1,26 @@ +import { Col, Row } from 'react-bootstrap' + +import Input from '../handlers/Input' + +const UserForm = () => { + return ( +
+ + + + + + + + + + + + + + +
+ ) +} + +export default UserForm diff --git a/examples/form-handler/components/index.js b/examples/form-handler/components/index.js new file mode 100644 index 00000000..25366e16 --- /dev/null +++ b/examples/form-handler/components/index.js @@ -0,0 +1,34 @@ +import React, {Component} from 'react' +import Head from 'next/head' +import { Col, Row } from 'react-bootstrap' + +import Header from './Header' +import DisplayForm from './DisplayForm' + +import UserForm from './UserForm' +import Social from './Social' + +class Main extends Component { + render () { + return ( +
+ + Form Handler + + +
+ + + + + + + + + +
+ ) + } +} + +export default Main diff --git a/examples/form-handler/constants/index.js b/examples/form-handler/constants/index.js new file mode 100644 index 00000000..056ed31b --- /dev/null +++ b/examples/form-handler/constants/index.js @@ -0,0 +1 @@ +export const INPUT_VALUE = 'INPUT_VALUE' diff --git a/examples/form-handler/handlers/Input.js b/examples/form-handler/handlers/Input.js new file mode 100644 index 00000000..3d39d39f --- /dev/null +++ b/examples/form-handler/handlers/Input.js @@ -0,0 +1,38 @@ +import React, {Component} from 'react' +import { FormGroup, ControlLabel, FormControl } from 'react-bootstrap' +import { connect } from 'react-redux' +import { bindActionCreators } from 'redux' + +import { inputChange } from '../actions' + +class Input extends Component { + inputChange = (e) => { + const { inputChange, title, name } = this.props + inputChange(title, name, e.target.value) + } + + render () { + return ( +
+ + {this.props.controlLabel} + + +
+ ) + } +} + +const mapDispatchToProps = dispatch => { + return { + inputChange: bindActionCreators(inputChange, dispatch) + } +} + +export default connect(null, mapDispatchToProps)(Input) diff --git a/examples/form-handler/package.json b/examples/form-handler/package.json new file mode 100644 index 00000000..42f7c604 --- /dev/null +++ b/examples/form-handler/package.json @@ -0,0 +1,26 @@ +{ + "name": "redux-form", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "next start -p 8000", + "dev": "node server.js", + "build": "next build" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "express": "^4.15.4", + "next": "^3.2.2", + "next-redux-wrapper": "^1.3.2", + "react": "^15.6.1", + "react-bootstrap": "^0.31.3", + "react-dom": "^15.6.1", + "react-redux": "^5.0.6", + "redux": "^3.7.2", + "redux-thunk": "^2.2.0" + } +} diff --git a/examples/form-handler/pages/index.js b/examples/form-handler/pages/index.js new file mode 100644 index 00000000..e2f6fa6e --- /dev/null +++ b/examples/form-handler/pages/index.js @@ -0,0 +1,14 @@ +import React, { Component } from 'react' +import withRedux from 'next-redux-wrapper' + +import Main from '../components' + +import { initStore } from '../store' + +class Index extends Component { + render () { + return
+ } +} + +export default withRedux(initStore, null)(Index) diff --git a/examples/form-handler/reducers/formReducer.js b/examples/form-handler/reducers/formReducer.js new file mode 100644 index 00000000..2eaa9090 --- /dev/null +++ b/examples/form-handler/reducers/formReducer.js @@ -0,0 +1,15 @@ +import { INPUT_VALUE } from '../constants' + +export default (state = {}, action) => { + switch (action.type) { + case INPUT_VALUE: + return { ...state, + [action.title]: + { ...state[action.title], + [action.name]: action.val + } + } + default: + return state + } +} diff --git a/examples/form-handler/reducers/index.js b/examples/form-handler/reducers/index.js new file mode 100644 index 00000000..35ee6547 --- /dev/null +++ b/examples/form-handler/reducers/index.js @@ -0,0 +1,6 @@ +import { combineReducers } from 'redux' +import formReducer from './formReducer' + +export default combineReducers({ + formReducer +}) diff --git a/examples/form-handler/server.js b/examples/form-handler/server.js new file mode 100644 index 00000000..87c18710 --- /dev/null +++ b/examples/form-handler/server.js @@ -0,0 +1,28 @@ +const express = require('express') +const next = require('next') + +const dev = process.env.NODE_ENV !== 'production' +const app = next({ dev }) +const handle = app.getRequestHandler() + +app.prepare() +.then(() => { + const server = express() + + server.get('/', (req, res) => { + return handle(req, res) + }) + + server.get('*', (req, res) => { + return handle(req, res) + }) + + server.listen(3000, (err) => { + if (err) throw err + console.log('> Ready on http://localhost:3000') + }) +}) +.catch((ex) => { + console.error(ex.stack) + process.exit(1) +}) diff --git a/examples/form-handler/store.js b/examples/form-handler/store.js new file mode 100644 index 00000000..bad02c08 --- /dev/null +++ b/examples/form-handler/store.js @@ -0,0 +1,7 @@ +import { createStore, applyMiddleware } from 'redux' +import thunkMiddleware from 'redux-thunk' +import reducer from './reducers' + +export const initStore = (initialState = {}) => { + return createStore(reducer, initialState, applyMiddleware(thunkMiddleware)) +}