Skip to content

Commit 0cc1556

Browse files
committed
Added following example
1 parent d3d6ece commit 0cc1556

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

Fullstack/twitter-clone/api/src/controllers/users.controller.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ const jwtMiddleware = require("../helpers/jwtMiddleware");
99

1010
const router = AsyncRouter();
1111

12-
const followValidators = [
13-
check("user").exists(),
14-
]
15-
1612
// Follow a user
1713
router.post(
1814
"/follow/:_id",
19-
[...followValidators, jwtMiddleware, handleValidationErrors],
15+
[jwtMiddleware],
2016
async (req, res) => {
2117
const followUser = await User.findById(req.params._id);
2218

@@ -34,6 +30,18 @@ router.post(
3430
}
3531
);
3632

33+
router.get(
34+
"/following/:_id",
35+
[jwtMiddleware],
36+
async (req, res) => {
37+
console.log(req.user.following[0], req.params._id)
38+
const isFollowing = req.user.following.find((_id) => _id.equals(req.params._id));
39+
40+
if(isFollowing) return res.send(200);
41+
else return res.send(404);
42+
}
43+
)
44+
3745
// Get chirps for a user
3846
router.get("/profile/:userId", async (req, res) => {
3947
const profile = await User.findById(req.params.userId).populate({
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React, { useState, useEffect, useGlobal } from "reactn";
2+
import client from '../api/client';
3+
4+
const Follow = ({ userId }) => {
5+
const { 0: token } = useGlobal("token")
6+
const [following, setFollowing] = useState(false);
7+
8+
const followUser = async () => {
9+
const { data } = await client.post(`/users/follow/${userId}`, {}, {
10+
headers: { Authorization: `Bearer ${token}` }
11+
})
12+
setFollowing(true);
13+
}
14+
15+
const isFollowing = async () => {
16+
try {
17+
await client.get(`/users/following/${userId}`, {
18+
headers: { Authorization: `Bearer ${token}` }
19+
});
20+
setFollowing(true);
21+
} catch(error) {
22+
setFollowing(false);
23+
}
24+
}
25+
26+
useEffect(() => {
27+
isFollowing()
28+
}, []);
29+
30+
return (
31+
<button onClick={followUser}>{(following) ? "Following" : "Follow"}</button>
32+
)
33+
}
34+
35+
export default Follow;

Fullstack/twitter-clone/ui/src/pages/Profile.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useState, useEffect, useGlobal } from "reactn";
22
import client from "../api/client";
3+
import Follow from "../components/Follow";
34

45
const Profile = () => {
56
const [profile, setProfile] = useState(null);
@@ -23,7 +24,11 @@ const Profile = () => {
2324
<div>
2425
<h1>Profile:</h1>
2526
{profile && (
26-
<em>{profile.email}</em>
27+
<div>
28+
<div>
29+
<em>{profile.email}</em>
30+
</div>
31+
</div>
2732
)}
2833
</div>
2934
)

Fullstack/twitter-clone/ui/src/pages/User.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useState, useEffect } from "reactn";
22
import client from '../api/client';
33
import Chirp from "../components/Chirp";
44
import { useParams } from "react-router-dom";
5+
import Follow from '../components/Follow';
56

67
const User = () => {
78
const [profile, setProfile] = useState(null);
@@ -22,6 +23,9 @@ const User = () => {
2223
<h2>
2324
{profile.email}
2425
</h2>
26+
<div>
27+
<Follow userId={profile._id} />
28+
</div>
2529
<div>
2630
{profile.chirps.map((chirp) => (
2731
<Chirp key={chirp._id} chirp={chirp} />

0 commit comments

Comments
 (0)