Skip to content

Commit 9390bf3

Browse files
author
Vivek Chowdhury
committed
added redux store support [Work in progress]
1 parent 8708124 commit 9390bf3

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

src/app/Header/Header.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useEffect, useState } from "react";
22
import "./Header.css";
3-
import { NavLink, Link } from "react-router-dom";
3+
import { NavLink } from "react-router-dom";
44
import { connect } from "react-redux";
55

66
function Header(props) {
@@ -78,16 +78,17 @@ function Header(props) {
7878
Contact Us
7979
</NavLink>
8080
</li>
81-
<Link
82-
to=""
81+
<button
8382
className={
8483
doChangeBackground
8584
? "btn btn-outline-light"
8685
: "btn btn-outline-primary"
8786
}
87+
onClick={props.onSignIn}
8888
>
89-
Sign In
90-
</Link>
89+
{" "}
90+
{!props.user.isLoggedIn ? "Sign In" : "Sign Out"}
91+
</button>
9192
</ul>
9293
</div>
9394
</div>

src/app/Login/Login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function Login(props) {
2525
if (props.user.error !== "") {
2626
updateErrorMessage();
2727
}
28-
}, [props.history, props.user]);
28+
}, [props.history, props.user, props]);
2929

3030
const updateErrorMessage = () => {
3131
setError("Invalid User naem or Password, please try again !");

src/app/Login/loginActionTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export const VALIDATE_USER = "VALIDATE_USER";
22
export const USER_VALIDATED_SUCCESSFULLY = "USER_VALIDATED_SUCCESSFULLY";
33
export const USER_VALIDATION_FAILED = "USER_VALIDATION_FAILED";
44
export const REMEMBER_ME = "REMEMBER_ME";
5+
export const SIGN_OUT_LOGGEDIN_USER = "SIGN_OUT_LOGGEDIN_USER";

src/app/Login/loginActions.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export function onSuccessfulValidation(response) {
55
return { type: Actions.USER_VALIDATED_SUCCESSFULLY, response };
66
}
77

8+
export function signOutUser() {
9+
return { type: Actions.SIGN_OUT_LOGGEDIN_USER };
10+
}
811
export function validateUser(user) {
912
return function (dispatch) {
1013
return Services.validateUser(user.userId, user.password)

src/app/Login/loginReducer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const initialUser = {
44
id: "",
55
password: "",
66
error: "",
7+
isLoggedIn: false,
78
};
89

910
export default function loginReducer(state = initialUser, action) {
@@ -13,7 +14,10 @@ export default function loginReducer(state = initialUser, action) {
1314
return {
1415
...state,
1516
...action.response,
17+
isLoggedIn: true,
1618
};
19+
case Actions.SIGN_OUT_LOGGEDIN_USER:
20+
return initialUser;
1721
default:
1822
return state;
1923
}

src/app/TechMarketShell.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import Footer from "./Footer/Footer";
33
import Header from "./Header/Header";
4-
import { Switch, Route, Redirect } from "react-router-dom";
4+
import { Switch, Route, Redirect, withRouter } from "react-router-dom";
55
import "./TechMarketShell.css";
66

77
import Feeds from "./Feeds/Feeds";
@@ -10,7 +10,7 @@ import About from "./About/About";
1010
import ContactUs from "./ContactUs/ContactUs";
1111
import Login from "./Login/Login";
1212
import { connect } from "react-redux";
13-
// import { validateUser } from "./Login/loginActions";
13+
import { signOutUser } from "./Login/loginActions";
1414

1515
function TechMarketShell(props) {
1616
function enforceAuthentication(component, path, user) {
@@ -19,10 +19,20 @@ function TechMarketShell(props) {
1919
}
2020
return <Route exact path={path} component={component} />;
2121
}
22-
console.log(process.env.BASE_URL, "BASE_URL===>");
22+
23+
const hangleSignIn = (event) => {
24+
event.preventDefault();
25+
if (!props.user.isLoggedIn) {
26+
props.history.push("/");
27+
} else {
28+
props.signOutUser();
29+
props.history.push("/");
30+
}
31+
};
32+
2333
return (
2434
<div className="rootContainer">
25-
<Header />
35+
<Header onSignIn={hangleSignIn} />
2636
<div className="container-fluid contentContainer">
2737
<Switch>
2838
<Route path="/" exact component={Login} />
@@ -43,4 +53,11 @@ function mapStateToProps(state) {
4353
user: state.user,
4454
};
4555
}
46-
export default connect(mapStateToProps)(TechMarketShell);
56+
57+
const mapDispatchToProps = {
58+
signOutUser,
59+
};
60+
export default connect(
61+
mapStateToProps,
62+
mapDispatchToProps
63+
)(withRouter(TechMarketShell));

0 commit comments

Comments
 (0)