Skip to content

Commit

Permalink
React-redux-form revisited
Browse files Browse the repository at this point in the history
  • Loading branch information
SohelRaja committed Apr 24, 2020
1 parent 1362323 commit 5cbf1c2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/components/ContactComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Component } from 'react';
import { Breadcrumb, BreadcrumbItem,
Button, Row, Col, Label } from 'reactstrap';
import {Link} from 'react-router-dom';
import { Control, LocalForm, Errors } from 'react-redux-form';
import { Control, Form, Errors, actions } from 'react-redux-form';

const required = (val) => val && val.length;
const maxLength = (len) => (val) => !(val) || (val.length <= len);
Expand All @@ -19,6 +19,7 @@ class Contact extends Component{
handleSubmit(values) {
console.log('Current State is: ' + JSON.stringify(values));
alert('Current State is: ' + JSON.stringify(values));
this.props.resetFeedbackForm();
}
render(){
return(
Expand Down Expand Up @@ -64,7 +65,7 @@ class Contact extends Component{
<h3>Send us your Feedback</h3>
</div>
<div className="col-12 col-md-9">
<LocalForm onSubmit={(values) => this.handleSubmit(values)}>
<Form model="feedback" onSubmit={(values) => this.handleSubmit(values)}>
<Row className="form-group">
<Label htmlFor="firstname" md={2}>First Name</Label>
<Col md={10}>
Expand Down Expand Up @@ -187,7 +188,7 @@ class Contact extends Component{
</Button>
</Col>
</Row>
</LocalForm>
</Form>
</div>
</div>
</div>
Expand Down
6 changes: 4 additions & 2 deletions src/components/MainComponent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react';
import {Switch, Route, Redirect, withRouter} from 'react-router-dom';
import { connect } from 'react-redux';
import { actions } from 'react-redux-form';

import Menu from './MenuComponent';
import './../App.css';
Expand All @@ -25,7 +26,8 @@ const mapStateToProps = state => {
const mapDispatchToProps = dispatch => ({

addComment: (dishId, rating, author, comment) => dispatch(addComment(dishId, rating, author, comment)),
fetchDishes: () => {dispatch(fetchDishes())}
fetchDishes: () => {dispatch(fetchDishes())},
resetFeedbackForm: () => { dispatch(actions.reset('feedback'))}

});

Expand Down Expand Up @@ -74,7 +76,7 @@ class Main extends Component{
<Route exact path='/aboutus' component={() => <About leaders={this.props.leaders} />} />} />
<Route exact path='/menu' component={() => <Menu dishes={this.props.dishes} />} />
<Route path='/menu/:dishId' component={DishWithId} />
<Route exact path='/contactus' component={Contact} />} />
<Route exact path='/contactus' component={() => <Contact resetFeedbackForm={this.props.resetFeedbackForm} />} />} />
<Redirect to="/home" />
</Switch>
</div>
Expand Down
7 changes: 6 additions & 1 deletion src/redux/configureStore.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import {createStore, combineReducers, applyMiddleware} from 'redux';
import { createForms } from 'react-redux-form';
import thunk from 'redux-thunk';
import logger from 'redux-logger';

import { Dishes } from './dishes';
import { Comments } from './comments';
import { Promotions } from './promotions';
import { Leaders } from './leaders';
import { InitialFeedback } from './forms';

export const ConfigureStore = () => {
const store = createStore(
combineReducers({
dishes: Dishes,
comments: Comments,
promotions: Promotions,
leaders: Leaders
leaders: Leaders,
...createForms({
feedback: InitialFeedback
})
}),
applyMiddleware(thunk, logger)
);
Expand Down
9 changes: 9 additions & 0 deletions src/redux/forms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const InitialFeedback = {
firstname: '',
lastname: '',
telnum: '',
email: '',
agree: false,
contactType: 'Tel.',
message: ''
};

0 comments on commit 5cbf1c2

Please sign in to comment.