Skip to content

Commit e682861

Browse files
committed
update to use redux
1 parent 79e2c17 commit e682861

File tree

25 files changed

+893
-162
lines changed

25 files changed

+893
-162
lines changed

package-lock.json

Lines changed: 34 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@
3737
"react": "^16.11.0",
3838
"react-dev-utils": "^5.0.1",
3939
"react-dom": "^16.11.0",
40+
"react-redux": "^7.2.0",
4041
"react-router-dom": "^5.2.0",
42+
"redux": "^4.0.5",
43+
"redux-thunk": "^2.3.0",
4144
"resolve": "1.6.0",
4245
"style-loader": "0.19.0",
4346
"sw-precache-webpack-plugin": "0.11.4",

src/App.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ import BurgerBuilder from './Containers/BurgerBuilder/BurgerBuilder';
44
import CheckOut from './Containers/CheckOut/CheckOut'
55
import {Route, Switch} from 'react-router-dom';
66
import Orders from './Containers/Orders/Orders';
7+
import Auth from './Containers/Auth/Auth';
8+
import LogOut from './Containers/Auth/LogOut/LogOut';
9+
import { connect } from 'react-redux';
10+
import * as actions from './store/actions/index';
711

812

913
class App extends Component {
14+
componentDidMount(){
15+
this.props.onTryAutoSignup();
16+
}
1017
render() {
1118
return (
1219
<div>
@@ -15,6 +22,8 @@ class App extends Component {
1522
<Route path="/" exact component={BurgerBuilder}></Route>
1623
<Route path="/checkout" component={CheckOut}></Route>
1724
<Route path="/orders" component={Orders}/>
25+
<Route path="/auth" component={Auth}/>
26+
<Route path="/logOut" component={LogOut}/>
1827
<Route component={BurgerBuilder}></Route>
1928
</Switch>
2029
</Layout>
@@ -23,4 +32,9 @@ class App extends Component {
2332
}
2433
}
2534

26-
export default App;
35+
const mapDispatchToProps = (dispatch) =>{
36+
return{
37+
onTryAutoSignup : ()=> dispatch(actions.authCheckState())
38+
}
39+
}
40+
export default connect(null, mapDispatchToProps)(App);

src/Components/Burger/BuildControls/BuildControls.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ const buildControls = (props) => (
1717
removed ={() => props.ingredientRemoved(control.type)}
1818
disabled ={props.disabledInfo[control.type]}/>
1919
)}
20-
<button className={classes.OrderButton} disabled={props.disabledButton} onClick={props.purchased}>Order Now</button>
20+
<button
21+
className={classes.OrderButton}
22+
disabled={props.disabledButton} onClick={props.purchased}>
23+
{props.isAuthenticated? 'Order Now' : 'SignIn and Order'}
24+
</button>
2125
</div>
2226
)
2327
export default buildControls;

src/Components/Navigation/NavigationItems/NavigationItems.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import NavigationItem from '../NavigationItems/NavigationItem/NavigationItem';
55
const navigationItems = (props)=>(
66
<ul className={classes.NavigationItems}>
77
<NavigationItem link= "/" exact>Burger Builder</NavigationItem>
8-
<NavigationItem link= "/orders" >Orders</NavigationItem>
8+
{props.isAuthenticated? <NavigationItem link= "/orders" >Orders</NavigationItem> : null}
9+
{props.isAuthenticated? <NavigationItem link= "/logOut" >LogOut</NavigationItem>
10+
: <NavigationItem link= "/auth" >Authentication</NavigationItem>}
11+
912
</ul>
1013

1114
);

src/Components/Navigation/SideDrawer/SideDrawer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const sideDrawer = (props)=>{
1919
<Logo />
2020
</div>
2121
<nav>
22-
<NavigationItems/>
22+
<NavigationItems isAuthenticated={props.isAuthenticated}/>
2323
</nav>
2424
</div>
2525
</Aux>

src/Components/Navigation/Toolbar/Toolbar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const toolBar= (props)=>(
1212
<Logo/>
1313
</div>
1414
<nav className={classes.DesktopOnly}>
15-
<NavigationItems/>
15+
<NavigationItems isAuthenticated={props.isAuthenticated}/>
1616
</nav>
1717
</header>
1818

src/Containers/Auth/Auth.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.Auth{
2+
margin: 20px auto;
3+
width: 80%;
4+
text-align: center;
5+
box-shadow: 0 2px 3px #ccc;
6+
border: 1px solid #eee;
7+
padding:10px;
8+
box-sizing: border-box;
9+
}
10+
11+
@media(min-width:600px){
12+
.Auth{
13+
width: 500px;
14+
}
15+
}

src/Containers/Auth/Auth.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import React, { Component } from "react";
2+
import Input from "../../Components/UI/Input/Input";
3+
import Button from "../../Components/UI/Button/Button";
4+
import classes from './Auth.css';
5+
import * as actions from '../../store/actions/index';
6+
import {connect} from 'react-redux';
7+
import Spinner from '../../Components/UI/Spinner/Spinner';
8+
import { Redirect } from 'react-router-dom';
9+
10+
class Auth extends Component {
11+
state = {
12+
controls: {
13+
email: {
14+
elementType: "input",
15+
elementConfig: {
16+
type: "email",
17+
placeholder: "Enter Emailaddress",
18+
},
19+
value: "",
20+
validation: {
21+
required: true,
22+
isEmail: true,
23+
},
24+
valid: false,
25+
touched: false,
26+
},
27+
password: {
28+
elementType: "input",
29+
elementConfig: {
30+
type: "password",
31+
placeholder: "Enter password",
32+
},
33+
value: "",
34+
validation: {
35+
required: true,
36+
minLength: 6,
37+
},
38+
valid: false,
39+
touched: false,
40+
},
41+
},
42+
isSignUp: true
43+
};
44+
45+
componentDidMount(){
46+
47+
if(!this.props.buildingBurger && this.props.authRedirect!=='/'){
48+
this.props.onSetAuthRedirectPath();
49+
}
50+
}
51+
52+
inputChangedHandler = (event, inputIdentifier) => {
53+
const updatedControls = {
54+
...this.state.controls,
55+
[inputIdentifier]:{ ...this.state.controls[inputIdentifier],
56+
value: event.target.value,
57+
valid: this.checkValidity(event.target.value,this.state.controls[inputIdentifier].validation),
58+
touched: true
59+
},
60+
};
61+
this.setState({controls: updatedControls});
62+
}
63+
64+
checkValidity(value, rules) {
65+
let isValid = true;
66+
67+
if (rules.required) {
68+
isValid = value !== "" && isValid;
69+
}
70+
if (rules.minLength) {
71+
isValid = value.length >= rules.minLength && isValid;
72+
}
73+
if (rules.maxLength) {
74+
isValid = value.length <= rules.maxLength && isValid;
75+
}
76+
if(rules.isEmail){
77+
const pattern = /[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
78+
isValid = pattern.test(value) && isValid;
79+
}
80+
return isValid;
81+
}
82+
83+
submitHandler =(event) =>{
84+
event.preventDefault();
85+
this.props.onAuth(this.state.controls.email.value, this.state.controls.password.value, this.state.isSignUp);
86+
}
87+
88+
switchAuthModeHandler = () =>{
89+
this.setState(prevState => {
90+
return {
91+
isSignUp : !prevState.isSignUp
92+
}
93+
})
94+
}
95+
render() {
96+
const formElementsArray = [];
97+
for (let key in this.state.controls) {
98+
formElementsArray.push({ id: key, config: this.state.controls[key] });
99+
}
100+
let form = formElementsArray.map((formElement) => (
101+
<Input
102+
key={formElement.id}
103+
changed={(event) => this.inputChangedHandler(event, formElement.id)}
104+
elementType={formElement.config.elementType}
105+
elementConfig={formElement.config.elementConfig}
106+
validation={formElement.config.validation}
107+
touched={formElement.config.touched}
108+
valid={formElement.config.valid}
109+
value={formElement.config.value}
110+
/>
111+
));
112+
113+
if(this.props.loading){
114+
form =<Spinner/>;
115+
}
116+
let errorMessage = null;
117+
if(this.props.error){
118+
errorMessage =(<p>{this.props.error.message}</p>)
119+
}
120+
let redirect = null;
121+
if(this.props.isAuthenticated){
122+
redirect =<Redirect to={this.props.authRedirectPath}/>
123+
}
124+
return (
125+
<div className= {classes.Auth}>
126+
{redirect}
127+
{errorMessage}
128+
<form onSubmit={this.submitHandler}>
129+
{form}
130+
<Button btnType="Success">{this.state.isSignUp? 'SignUp' :'SignIn'}</Button>
131+
</form>
132+
<Button clicked = {this.switchAuthModeHandler}
133+
btnType="Danger">Swith to {this.state.isSignUp? 'SignIn' :'SignUp'}</Button>
134+
</div>
135+
);
136+
}
137+
}
138+
const mapStateToProps =(state) =>{
139+
return{
140+
loading: state.auth.loading,
141+
error: state.auth.error,
142+
token : state.auth.token,
143+
isAuthenticated : state.auth.token!==null,
144+
buildingBurger: state.burgerBuilder.building,
145+
authRedirectPath: state.auth.authRedirect
146+
}
147+
}
148+
const mapDispatchToProps =(dispatch) =>{
149+
return{
150+
onAuth : (email,passWord,isSignUp) => dispatch(actions.auth(email,passWord,isSignUp)),
151+
onSetAuthRedirectPath: () => dispatch(actions.setAuthRedirectPath('/'))
152+
}
153+
}
154+
export default connect(mapStateToProps,mapDispatchToProps)(Auth);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, {Component } from 'react';
2+
import * as actions from '../../../store/actions/index';
3+
import {connect} from 'react-redux';
4+
import {Redirect} from 'react-router-dom';
5+
6+
class LogOut extends Component{
7+
8+
componentDidMount(){
9+
this.props.onLogOut();
10+
}
11+
12+
render(){
13+
return <Redirect to="/"/>;
14+
}
15+
16+
}
17+
18+
const mapDispatchToProps =(dispatch)=>{
19+
return {
20+
onLogOut : () => dispatch(actions.logOut())
21+
}
22+
}
23+
export default connect(null,mapDispatchToProps)(LogOut);

0 commit comments

Comments
 (0)