Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f17492b

Browse files
committed
Directly allow users to follow from the home page
1 parent 6f2ee61 commit f17492b

File tree

2 files changed

+74
-61
lines changed

2 files changed

+74
-61
lines changed

src/components/HomePageFeed.tsx

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Avatar } from '@/ui'
22
import {
33
ChatCenteredDots,
44
Gear,
5-
Plus,
65
RocketLaunch,
76
ShareNetwork,
87
ThumbsUp,
@@ -26,8 +25,8 @@ import {
2625
import { User } from 'src/pages/members'
2726
import AppNavBar from './AppNavBar'
2827
import AppFooter from './AppFooter'
29-
import { IconPlus } from 'tabler-icons'
3028
import { useRouter } from 'next/router'
29+
import useFollowUser from './profile/useFollowUser'
3130

3231
type LikeData = {
3332
count: number
@@ -336,6 +335,56 @@ function HomePageSideNavBar() {
336335
)
337336
}
338337

338+
function FollowButton({ user }: { user: User }) {
339+
const { toggleFollow } = useFollowUser(user.id)
340+
const [isFollowing, setIsFollowing] = useState(false)
341+
return (
342+
<li className="flex items-center py-4 space-x-3">
343+
<div className="flex-shrink-0">
344+
<img className="h-8 w-8 rounded-full" src={user.image} alt="" />
345+
</div>
346+
<div className="min-w-0 flex-1">
347+
<p className="text-sm font-medium text-gray-900">
348+
<A href={`/${user.username}`}>
349+
{user.account?.firstName ?? user.name}
350+
</A>
351+
</p>
352+
<p className="text-sm text-gray-500">
353+
<A href={`/${user.username}`}>@{user.username}</A>
354+
</p>
355+
</div>
356+
<div className="flex-shrink-0">
357+
<button
358+
type="button"
359+
className="inline-flex items-center px-3 py-0.5 rounded-full bg-brand-50 text-sm font-medium text-brand-700 hover:bg-brand-100"
360+
onClick={() => {
361+
setIsFollowing(true)
362+
toggleFollow()
363+
}}
364+
>
365+
{!isFollowing && (
366+
<svg
367+
className="-ml-1 mr-0.5 h-5 w-5 text-brand-400"
368+
xmlns="http://www.w3.org/2000/svg"
369+
viewBox="0 0 20 20"
370+
fill="currentColor"
371+
aria-hidden="true"
372+
>
373+
<path
374+
fillRule="evenodd"
375+
d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z"
376+
clipRule="evenodd"
377+
/>
378+
</svg>
379+
)}
380+
381+
<span>{isFollowing ? 'Following' : 'Follow'}</span>
382+
</button>
383+
</div>
384+
</li>
385+
)
386+
}
387+
339388
function HomePageAside({ updates }: { updates: HomePageFeedUpdateType[] }) {
340389
const router = useRouter()
341390
const [session, loading] = useSession()
@@ -430,50 +479,7 @@ function HomePageAside({ updates }: { updates: HomePageFeedUpdateType[] }) {
430479
{!isLoading &&
431480
!isError &&
432481
response.users.map((user: User) => (
433-
<li
434-
className="flex items-center py-4 space-x-3"
435-
key={user.id}
436-
>
437-
<div className="flex-shrink-0">
438-
<img
439-
className="h-8 w-8 rounded-full"
440-
src={user.image}
441-
alt=""
442-
/>
443-
</div>
444-
<div className="min-w-0 flex-1">
445-
<p className="text-sm font-medium text-gray-900">
446-
<A href={`/${user.username}`}>
447-
{user.account?.firstName ?? user.name}
448-
</A>
449-
</p>
450-
<p className="text-sm text-gray-500">
451-
<A href={`/${user.username}`}>@{user.username}</A>
452-
</p>
453-
</div>
454-
<div className="flex-shrink-0">
455-
<button
456-
type="button"
457-
className="inline-flex items-center px-3 py-0.5 rounded-full bg-brand-50 text-sm font-medium text-brand-700 hover:bg-brand-100"
458-
onClick={() => router.push(`/${user.username}`)}
459-
>
460-
<svg
461-
className="-ml-1 mr-0.5 h-5 w-5 text-brand-400"
462-
xmlns="http://www.w3.org/2000/svg"
463-
viewBox="0 0 20 20"
464-
fill="currentColor"
465-
aria-hidden="true"
466-
>
467-
<path
468-
fillRule="evenodd"
469-
d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z"
470-
clipRule="evenodd"
471-
/>
472-
</svg>
473-
<span>Follow</span>
474-
</button>
475-
</div>
476-
</li>
482+
<FollowButton user={user} key={user.id} />
477483
))}
478484
</ul>
479485
</div>

src/components/profile/useFollowUser.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { useSession } from 'next-auth/client'
22
import { useEffect, useState } from 'react'
3-
import { useMutation, useQuery } from 'react-query'
3+
import { useMutation, useQuery, useQueryClient } from 'react-query'
44
import { User } from 'src/pages/members'
55

66
export default function useFollowUser(userId: string) {
7+
const queryClient = useQueryClient()
78
const [session, loading] = useSession()
89
const [currentUser, setCurrentUser] = useState<User>({})
910
useEffect(() => {
@@ -31,21 +32,27 @@ export default function useFollowUser(userId: string) {
3132
}
3233
)
3334

34-
const { mutate: toggleFollow } = useMutation(() =>
35-
fetch(`/api/fauna/toggle-follow`, {
36-
method: 'POST',
37-
headers: {
38-
'Content-Type': 'application/json',
39-
},
40-
body: JSON.stringify({
41-
userId,
35+
const { mutate: toggleFollow } = useMutation(
36+
() =>
37+
fetch(`/api/fauna/toggle-follow`, {
38+
method: 'POST',
39+
headers: {
40+
'Content-Type': 'application/json',
41+
},
42+
body: JSON.stringify({
43+
userId,
44+
}),
45+
}).then((res) => {
46+
if (!res.ok) {
47+
throw new Error('Something went wrong!!')
48+
}
49+
return res.json()
4250
}),
43-
}).then((res) => {
44-
if (!res.ok) {
45-
throw new Error('Something went wrong!!')
46-
}
47-
return res.json()
48-
})
51+
{
52+
onSuccess: () => {
53+
queryClient.refetchQueries('api/fauna/who-to-follow')
54+
},
55+
}
4956
)
5057

5158
return {

0 commit comments

Comments
 (0)