Skip to content

Commit 5dc9526

Browse files
committed
Updating the branch
1 parent 0c37589 commit 5dc9526

File tree

7 files changed

+83
-58
lines changed

7 files changed

+83
-58
lines changed

src/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import "bootstrap/dist/css/bootstrap.min.css";
66
import Registration from "./components/registration";
77
import Login from "./components/login";
88
import Facebook from "./components/facebook";
9-
import User from "./components/user";
9+
import User from "./components/user/index";
1010
import CreateArticle from "./components/create-article";
1111
import ArticleView from "./components/article/article-view";
1212
import PrivateRoute from "./components/privateRoutes";

src/components/article/Bookmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Bookmark = () => {
1010
(state) => state.userReducer.bookmarks,
1111
shallowEqual
1212
);
13-
const { _id } = useSelector(
13+
const { _id, user } = useSelector(
1414
(state) => state.articlesReducer.article,
1515
shallowEqual
1616
);

src/components/article/article-view.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ class ArticleView extends Component {
143143
this.props.dispatch(deleteArticle(this.props.match.params.id));
144144
window.location = "/";
145145
};
146+
showBookmark = () => {
147+
if (
148+
this.props.loggedIn &&
149+
this.props.user.id !== this.state.articleDetails.user_id
150+
) {
151+
return <Bookmark />;
152+
}
153+
};
146154

147155
render() {
148156
return (
@@ -158,8 +166,7 @@ class ArticleView extends Component {
158166
</h6>
159167

160168
<Like />
161-
{this.props.loggedIn ? <Bookmark /> : ""}
162-
169+
{this.showBookmark()}
163170
{this.editAndDeleArticle()}
164171

165172
<hr />
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import React, { useState, useEffect } from "react";
2+
import { shallowEqual, useSelector, useDispatch } from "react-redux";
3+
import {
4+
followUser,
5+
unFollowUser,
6+
} from "../../redux/actions/get-user";
7+
8+
const FollowButton = () => {
9+
const dispatch = useDispatch();
10+
const following = useSelector(
11+
(state) => state.userReducer.following,
12+
shallowEqual
13+
);
14+
// Logged In user id
15+
const { id } = useSelector((state) => state.authReducer.user);
16+
// Follow User Id
17+
const { _id } = useSelector((state) => state.userReducer.user);
18+
const loggedIn = useSelector((state) => state.authReducer.loggedIn);
19+
const [isFollowed, setFollowed] = useState(false);
20+
21+
useEffect(() => {
22+
const result = following.find((user) => user._id === _id);
23+
if (result) {
24+
setFollowed(true);
25+
} else {
26+
setFollowed(false);
27+
}
28+
29+
}, [following, _id]);
30+
31+
const handleFollow = (e) => {
32+
if (!isFollowed) {
33+
dispatch(followUser(id, _id));
34+
} else {
35+
dispatch(unFollowUser(id, _id));
36+
setFollowed(false);
37+
}
38+
};
39+
40+
return (
41+
<>
42+
<button
43+
type="button"
44+
style={
45+
isFollowed
46+
? { backgroundColor: "#007bff" }
47+
: { backgroundColor: "#fff", color: "#007bff" }
48+
}
49+
className="btn btn-success btn-block"
50+
onClick={handleFollow}
51+
disabled={!loggedIn}
52+
>
53+
Follow
54+
</button>
55+
</>
56+
);
57+
};
58+
export default FollowButton;

src/components/user.js renamed to src/components/user/index.js

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ import React, { Component } from "react";
22
import { Link } from "react-router-dom";
33
import { Avatar, Card } from "antd";
44
import { UserOutlined } from "@ant-design/icons";
5-
import "../styles/styles.css";
5+
import "../../styles/styles.css";
66

77
import { connect } from "react-redux";
88
import {
99
getUser,
10-
followUser,
11-
unFollowUser,
1210
getBookmarks,
1311
getFollowers,
1412
getFollowing,
1513
getUserWrittenArticles,
1614
getArticlesLiked,
17-
} from "../redux/actions/get-user";
18-
import ArticlesList from "./article/articles-list";
15+
} from "../../redux/actions/get-user";
16+
import ArticlesList from "../article/articles-list";
17+
import FollowButton from "./FollowButton";
1918

2019
class ErrorBoundary extends Component {
2120
state = {
@@ -67,6 +66,9 @@ class User extends Component {
6766
componentDidMount() {
6867
this.props.dispatch(getUser(this.props.match.params.id));
6968
this.props.dispatch(getBookmarks(this.props.match.params.id));
69+
if (this.props.loggedIn) {
70+
this.props.dispatch(getFollowing(this.props.user.id));
71+
}
7072
}
7173
componentDidUpdate(prevProps) {
7274
if (this.props.userDetails !== prevProps.userDetails) {
@@ -92,35 +94,6 @@ class User extends Component {
9294
// return false;
9395
// }
9496

95-
handleFollow = (e) => {
96-
if (this.props.loggedIn && this.props.user.id !== this.state.user._id) {
97-
this.props.dispatch(followUser(this.props.user.id, this.state.user._id));
98-
} else if (
99-
this.props.loggedIn &&
100-
this.props.user.id === this.state.user._id
101-
) {
102-
alert("You cannot follow yourself.");
103-
} else {
104-
e.preventDefault();
105-
alert("Log In or Register to follow.");
106-
}
107-
};
108-
109-
handleUnfollow = (e) => {
110-
if (this.props.loggedIn && this.props.user.id !== this.state.user._id) {
111-
this.props.dispatch(
112-
unFollowUser(this.props.user.id, this.state.user._id)
113-
);
114-
} else if (
115-
this.props.loggedIn &&
116-
this.props.user.id === this.state.user._id
117-
) {
118-
alert("You cannot un follow yourself.");
119-
} else {
120-
e.preventDefault();
121-
alert("Log In or Register to Unfollow.");
122-
}
123-
};
12497
getFollowers = (e) => {
12598
e.preventDefault();
12699
if (this.state.user.followers.length === 0) {
@@ -186,19 +159,6 @@ class User extends Component {
186159
});
187160
};
188161

189-
followButton = () => {
190-
return (
191-
<div>
192-
<button className="btn btn-primary" onClick={this.handleFollow}>
193-
Follow
194-
</button>
195-
<button className="btn btn-primary" onClick={this.handleUnfollow}>
196-
Un Follow
197-
</button>
198-
</div>
199-
);
200-
};
201-
202162
render() {
203163
return (
204164
// <div>
@@ -333,9 +293,9 @@ class User extends Component {
333293
</a>
334294
</small>
335295
</p>
336-
<button className="btn btn-success btn-block">
337-
<span className="fa fa-plus-circle"></span> Follow{" "}
338-
</button>
296+
297+
<FollowButton />
298+
339299
<button
340300
type="button"
341301
className="btn btn-success btn-block"

src/redux/reducers/articlesReducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const handleEditComment = (state, action) => {
114114
if (action.result !== undefined) {
115115
newState = Object.assign({}, state, {
116116
comments: newState.comments.map((comment) => {
117-
if (comment.id !== action.result._id) {
117+
if (comment._id !== action.result._id) {
118118
return comment;
119119
}
120120
return Object.assign({}, comment, {

src/redux/store/sagas.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ function* followUser(action) {
454454
});
455455
} else {
456456
yield put({
457-
type: Types.FOLLOW_USER_ERROR_RESPONSE,
457+
type: Types.FOLLOW_USER_SUCCESS_RESPONSE,
458458
result,
459459
});
460460
}
@@ -467,7 +467,7 @@ function* unFollowUser(action) {
467467
try {
468468
let formBody = {};
469469
formBody.user = action.unfollow_id;
470-
const postUrl = baseUrl + +"/user/" + action.user_id + "/follow";
470+
const postUrl = baseUrl + "/user/" + action.user_id + "/follow";
471471
const response = yield call(GetDataFromServer, postUrl, "POST", formBody);
472472
const result = yield response.json();
473473
if (result.error) {
@@ -477,7 +477,7 @@ function* unFollowUser(action) {
477477
});
478478
} else {
479479
yield put({
480-
type: Types.UN_FOLLOW_USER_ERROR_RESPONSE,
480+
type: Types.UN_FOLLOW_USER_SUCCESS_RESPONSE,
481481
result,
482482
});
483483
}

0 commit comments

Comments
 (0)