Skip to content

Commit 2123c19

Browse files
author
Vivek Chowdhury
committed
Modified flow and added features like registratioin and profile update
1 parent 9390bf3 commit 2123c19

26 files changed

+867
-254
lines changed

src/api/LoginServices.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1+
import { handleError, handleResponse } from "./serviceUtils";
12
const baseUrl = process.env.REACT_APP_API_URL + "/Login";
23

34
export function validateUser(userid, password) {
45
const url = baseUrl + `?userid=${userid}&password=${password}`;
56
return fetch(url).then(handleResponse).catch(handleError);
67
}
7-
8-
async function handleResponse(response) {
9-
if (response.ok || response.status === 401) return response.json();
10-
if (response.status === 400) {
11-
// So, a server-side validation error occurred.
12-
// Server side validation returns a string error message, so parse as text instead of json.
13-
const error = await response.text();
14-
throw new Error(error);
15-
}
16-
throw new Error("Network response was not ok.");
17-
}
18-
19-
function handleError(error) {
20-
console.error("API call failed. " + error);
21-
throw error;
22-
}

src/api/UserService.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { handleError, handleResponse } from "./serviceUtils";
2+
3+
const baseUrl = process.env.REACT_APP_API_URL + "/users/";
4+
5+
export function updateProfile(source) {
6+
const user = clearRequestBody(source);
7+
const userId = user.id || "";
8+
const url = baseUrl + `${userId}`;
9+
10+
return fetch(url, {
11+
method: "PUT",
12+
headers: { "Content-Type": "application/json" },
13+
body: JSON.stringify(user),
14+
})
15+
.then(handleResponse)
16+
.catch(handleError);
17+
}
18+
19+
export function registerUser(source) {
20+
const user = clearRequestBody(source);
21+
return fetch(baseUrl, {
22+
method: "POST",
23+
headers: { "Content-Type": "application/json" },
24+
body: JSON.stringify(user),
25+
})
26+
.then(handleResponse)
27+
.catch(handleError);
28+
}
29+
30+
function clearRequestBody(source) {
31+
const user = { ...source };
32+
delete user.isLoggedIn;
33+
delete user.iAgree;
34+
delete user.rememberMe;
35+
delete user.error;
36+
return user;
37+
}

src/api/serviceUtils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export async function handleResponse(response) {
2+
if (response.ok || response.status === 401) return response.json();
3+
if (response.status === 400) {
4+
// So, a server-side validation error occurred.
5+
// Server side validation returns a string error message, so parse as text instead of json.
6+
const error = await response.text();
7+
throw new Error(error);
8+
}
9+
throw new Error("Network response was not ok.");
10+
}
11+
12+
export function handleError(error) {
13+
console.error("API call failed. " + error);
14+
throw error;
15+
}

src/app/Header/Header.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ import { NavLink } from "react-router-dom";
44
import { connect } from "react-redux";
55

66
function Header(props) {
7+
// Contains true if scroll event is added.
78
const [isListening, setIsListening] = useState(false);
9+
10+
// contains true if window is scrolled by user
811
const [doChangeBackground, setDoChangeBackground] = useState(false);
912

13+
/**
14+
* @description This function is responsible for adding scroll event listener and
15+
* removing it when Header is removed from display list.
16+
*/
1017
useEffect(() => {
1118
if (!isListening) {
1219
setIsListening(true);
@@ -19,6 +26,12 @@ function Header(props) {
1926
};
2027
}, [isListening]);
2128

29+
/**
30+
* @description This method is invoked when user scroll window, it is responsible
31+
* for setting member variable which will further change the background color of header
32+
* component.
33+
* @param {*} event
34+
*/
2235
const handleScroll = (event) => {
2336
event.preventDefault();
2437
setDoChangeBackground(window.scrollY > 40);
@@ -33,17 +46,19 @@ function Header(props) {
3346
<div className="container">
3447
<div className="collapse navbar-collapse full-width">
3548
<ul className="navbar-nav mr-auto">
36-
<li className="nav-item">
37-
<NavLink
38-
className={
39-
doChangeBackground ? "nav_link_light" : "nav_link_dark"
40-
}
41-
to="/feeds"
42-
>
43-
Feeds
44-
</NavLink>
45-
</li>
46-
{props.user.id && (
49+
{props.user.isLoggedIn && (
50+
<li className="nav-item">
51+
<NavLink
52+
className={
53+
doChangeBackground ? "nav_link_light" : "nav_link_dark"
54+
}
55+
to="/feeds"
56+
>
57+
Feeds
58+
</NavLink>
59+
</li>
60+
)}
61+
{props.user.isLoggedIn && (
4762
<li className="nav-item">
4863
<NavLink
4964
className={
@@ -86,7 +101,6 @@ function Header(props) {
86101
}
87102
onClick={props.onSignIn}
88103
>
89-
{" "}
90104
{!props.user.isLoggedIn ? "Sign In" : "Sign Out"}
91105
</button>
92106
</ul>
@@ -96,6 +110,10 @@ function Header(props) {
96110
);
97111
}
98112

113+
/**
114+
* @description This method retrieve slice of state from store
115+
* @param {*} state
116+
*/
99117
function mapToStateProps(state) {
100118
return {
101119
user: state.user,

src/app/Login/Login.js

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/app/Login/loginActionTypes.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/app/Login/loginActions.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/app/Login/loginReducer.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/app/Profile/Profile.js

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/app/Shared/DisplayPicture/DisplayPicture.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ function DisplayPicture(props) {
1111
className="displayPicture"
1212
alt="Logo"
1313
></img>
14-
{props.name && <div className="nameHolder">{props.name}</div>}
14+
{props.fname && props.lname && (
15+
<div className="nameHolder">{props.fname + " " + props.lname}</div>
16+
)}
1517
</div>
1618
);
1719
}

0 commit comments

Comments
 (0)