18.05 Adding Actions
Auth.js
src\containers\Auth\Auth.js
import React, { Component } from "react";
import { connect } from "react-redux";
import Button from "../../components/UI/Button/Button";
import Input from "../../components/UI/Input/Input";
import * as actions from "../../store/actions/index";
import classes from "./Auth.module.css";
class Auth extends Component {
state = {
controls: {
email: {
elementType: "input",
elementConfig: {
type: "email",
placeholder: "Mail Address",
},
value: "",
validation: {
required: true,
isEmail: true,
},
valid: false,
touched: false,
},
password: {
elementType: "input",
elementConfig: {
type: "password",
placeholder: "Password",
},
value: "",
validation: {
required: true,
minLength: 6,
},
valid: false,
touched: false,
},
},
};
checkValidity(value, rules) {
let isValid = true;
if (!rules) {
return true;
}
if (rules.required) {
isValid = value.trim() !== "" && isValid;
}
if (rules.minLength) {
isValid = value.length >= rules.minLength && isValid;
}
if (rules.maxLength) {
isValid = value.length <= rules.maxLength && isValid;
}
if (rules.isEmail) {
const pattern = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
isValid = pattern.test(value) && isValid;
}
if (rules.isNumeric) {
const pattern = /^\d+$/;
isValid = pattern.test(value) && isValid;
}
return isValid;
}
inputChangedHandler = (event, controlName) => {
const updatedControls = {
...this.state.controls,
[controlName]: {
...this.state.controls[controlName],
value: event.target.value,
valid: this.checkValidity(
event.target.value,
this.state.controls[controlName].validation
),
touched: true,
},
};
this.setState({ controls: updatedControls });
};
submitHandler = (event) => {
event.preventDefault();
this.props.onAuth(
this.state.controls.email.value,
this.state.controls.password.value
);
};
render() {
const formElementsArray = [];
for (let key in this.state.controls) {
formElementsArray.push({
id: key,
config: this.state.controls[key],
});
}
const form = formElementsArray.map((formElement) => (
<Input
key={formElement.id}
elementType={formElement.config.elementType}
elementConfig={formElement.config.elementConfig}
value={formElement.config.value}
invalid={!formElement.config.valid}
shouldValidate={formElement.config.validation}
touched={formElement.config.touched}
changed={(event) => this.inputChangedHandler(event, formElement.id)}
/>
));
return (
<div className={classes.Auth}>
<form onSubmit={this.submitHandler}>
{form}
<Button btnType="Success">Submit</Button>
</form>
</div>
);
}
}
const mapDispatchToProps = (dispatch) => {
return {
onAuth: (email, password) => dispatch(actions.auth(email, password)),
};
};
export default connect(null, mapDispatchToProps)(Auth);
actionTypes.js
src\store\actions\actionTypes.js
export const ADD_INGREDIENT = "ADD_INGREDIENT";
export const REMOVE_INGREDIENT = "REMOVE_INGREDIENT";
export const SET_INGREDIENTS = "SET_INGREDIENTS";
export const FETCH_INGREDIENTS_FAILED = "FETCH_INGREDIENTS_FAILED";
export const PURCHASE_BURGER_START = "PURCHASE_BURGER_START";
export const PURCHASE_BURGER_SUCCESS = "PURCHASE_BURGER_SUCCESS";
export const PURCHASE_BURGER_FAIL = "PURCHASE_BURGER_FAIL";
export const PURCHASE_INIT = "PURCHASE_INIT";
export const FETCH_ORDERS_START = "FETCH_ORDERS_START";
export const FETCH_ORDERS_SUCCESS = "FETCH_ORDERS_SUCCESS";
export const FETCH_ORDERS_FAIL = "FETCH_ORDERS_FAIL";
export const AUTH_START = "AUTH_START";
export const AUTH_SUCCESS = "AUTH_SUCCESS";
export const AUTH_FAIL = "AUTH_FAIL";
actions/index.js
src\store\actions\index.js
export { auth } from "./auth";
export {
addIngredient,
initIngredients,
removeIngredient,
} from "./burgerBuilder";
export { fetchOrders, purchaseBurger, purchaseInit } from "./order";
New auth.js
src\store\actions\auth.js
import * as actionTypes from "./actionTypes";
export const authStart = () => {
return {
type: actionTypes.AUTH_START,
};
};
export const authSuccess = (authData) => {
return {
type: actionTypes.AUTH_SUCCESS,
authData: authData,
};
};
export const authFail = (error) => {
return {
type: actionTypes.AUTH_FAIL,
error: error,
};
};
export const auth = (email, password) => {
return (dispatch) => {
dispatch(authStart());
};
};