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

Skip to content

Commit 2691fea

Browse files
committed
Fetch the like status along with other update details
1 parent 1404d89 commit 2691fea

File tree

5 files changed

+47
-48
lines changed

5 files changed

+47
-48
lines changed

src/components/HomePageFeed.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,7 @@ export function HomePageFeedUpdate({
9494

9595
const { count: likesCount, hasLiked, toggleLike } = useLikes({
9696
initialCount: update.likes.data,
97-
query: {
98-
key: ['api/fauna/has-liked-update', update.id],
99-
endpoint: '/api/fauna/has-liked-update',
100-
body: {
101-
updateId: update.id,
102-
},
103-
},
97+
initialHasLiked: update.hasLiked,
10498
mutation: {
10599
endpoint: '/api/fauna/toggle-update-like',
106100
body: {
@@ -268,9 +262,6 @@ export function HomePageFeedUpdate({
268262
updateId={update.id}
269263
key={comment.id}
270264
comment={comment}
271-
isLastComment={
272-
index === update.comments.data.length - 1
273-
}
274265
>
275266
{comment.description}
276267
</UpdateComment>

src/components/goals/UpdateComment.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ export default function UpdateComment({
2525
updateId,
2626
comment,
2727
children,
28-
isLastComment = false,
2928
}: {
3029
updateId: string
3130
children: string
3231
comment: UpdateCommentType
33-
isLastComment?: boolean
3432
}) {
3533
const queryClient = useQueryClient()
3634
const [isInEditMode, setIsInEditMode] = useState(false)
@@ -40,13 +38,7 @@ export default function UpdateComment({
4038
const [isLikeModalOpen, setIsLikeModalOpen] = useState(false)
4139
const { count: likesCount, hasLiked, toggleLike } = useLikes({
4240
initialCount: comment.likes.data,
43-
query: {
44-
key: ['api/fauna/has-liked-comment', comment.id],
45-
endpoint: '/api/fauna/has-liked-comment',
46-
body: {
47-
commentId: comment.id,
48-
},
49-
},
41+
initialHasLiked: comment.hasLiked,
5042
mutation: {
5143
endpoint: '/api/fauna/toggle-comment-like',
5244
body: {

src/components/useLikes.ts

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { useEffect, useReducer } from 'react'
2-
import { useMutation, useQuery } from 'react-query'
1+
import { useReducer } from 'react'
2+
import { useMutation } from 'react-query'
33

44
type LikeData = {
55
count: number
@@ -24,30 +24,20 @@ function reducer(state: LikeData, action: { type: string; payload?: any }) {
2424

2525
export function useLikes({
2626
initialCount,
27-
query,
27+
initialHasLiked,
2828
mutation,
2929
}: {
3030
initialCount: number
31-
query: {
32-
key: string | string[]
33-
endpoint: string
34-
body: any
35-
}
31+
initialHasLiked: boolean
3632
mutation: {
3733
endpoint: string
3834
body: any
3935
}
4036
}) {
41-
const initialState: LikeData = { count: initialCount, hasLiked: false }
42-
const { isLoading, isError, data } = useQuery(query.key, () => {
43-
return fetch(query.endpoint, {
44-
method: 'POST',
45-
headers: {
46-
'Content-Type': 'application/json',
47-
},
48-
body: JSON.stringify(query.body),
49-
}).then((res) => res.json())
50-
})
37+
const initialState: LikeData = {
38+
count: initialCount,
39+
hasLiked: initialHasLiked,
40+
}
5141

5242
const { mutate } = useMutation(() => {
5343
return fetch(mutation.endpoint, {
@@ -66,15 +56,6 @@ export function useLikes({
6656

6757
const [{ count, hasLiked }, dispatch] = useReducer(reducer, initialState)
6858

69-
useEffect(() => {
70-
if (!isLoading && !isError) {
71-
dispatch({
72-
type: 'set',
73-
payload: { hasLiked: data.liked, count: initialCount },
74-
})
75-
}
76-
}, [data?.liked, initialCount, isError, isLoading])
77-
7859
const toggleLike = () => {
7960
dispatch({ type: 'toggle' })
8061
mutate()

src/pages/api/fauna/all-updates.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'
22

33
import faunadb from 'faunadb'
4+
import { getSession } from 'next-auth/client'
5+
import { User } from 'src/pages/members'
46
const q = faunadb.query
57
const isProduction = process.env.NODE_ENV === 'production'
68
const client = new faunadb.Client({
@@ -14,6 +16,7 @@ const FaunaCreateHandler: NextApiHandler = async (
1416
req: NextApiRequest,
1517
res: NextApiResponse
1618
) => {
19+
const session = await getSession({ req })
1720
try {
1821
const response: any = await client.query(
1922
q.Map(q.Paginate(q.Match(q.Index('all_recent_updates'))), (result) => {
@@ -26,8 +29,22 @@ const FaunaCreateHandler: NextApiHandler = async (
2629
const createdAt = q.ToMillis(
2730
q.Select(['data', 'timestamps', 'createdAt'], goalUpdateDoc)
2831
)
32+
const updateId = q.Select(['ref', 'id'], goalUpdateDoc)
33+
let hasLiked = false
34+
if (session) {
35+
const userId = (session.user as User).id
36+
const ref = q.Match(q.Index('unique_update_user_like'), [
37+
q.Ref(q.Collection('goal_updates'), updateId),
38+
q.Ref(q.Collection('users'), userId),
39+
])
40+
hasLiked = q.If(
41+
q.Exists(ref),
42+
q.Select(['data', 'liked'], q.Get(ref)),
43+
false
44+
) as boolean
45+
}
2946
return {
30-
id: q.Select(['ref', 'id'], goalUpdateDoc),
47+
id: updateId,
3148
goal: {
3249
id: q.Select(['ref', 'id'], goalDoc),
3350
title: q.Select(['data', 'title'], goalDoc),
@@ -41,12 +58,27 @@ const FaunaCreateHandler: NextApiHandler = async (
4158
const postedByDoc = q.Get(
4259
q.Select(['data', 'postedBy'], commentDoc)
4360
)
61+
let hasLiked = false
62+
const commentId = q.Select(['ref', 'id'], commentDoc)
63+
if (session) {
64+
const userId = (session.user as User).id
65+
const ref = q.Match(q.Index('unique_comment_user_like'), [
66+
q.Ref(q.Collection('update_comments'), commentId),
67+
q.Ref(q.Collection('users'), userId),
68+
])
69+
hasLiked = q.If(
70+
q.Exists(ref),
71+
q.Select(['data', 'liked'], q.Get(ref)),
72+
false
73+
) as boolean
74+
}
4475
return {
45-
id: q.Select(['ref', 'id'], commentDoc),
76+
id: commentId,
4677
description: q.Select(['data', 'description'], commentDoc),
4778
createdAt: q.ToMillis(
4879
q.Select(['data', 'timestamps', 'createdAt'], commentDoc)
4980
),
81+
hasLiked,
5082
likes: q.Count(
5183
q.Filter(
5284
q.Paginate(
@@ -73,6 +105,7 @@ const FaunaCreateHandler: NextApiHandler = async (
73105
}
74106
}
75107
),
108+
hasLiked,
76109
likes: q.Count(
77110
q.Filter(
78111
q.Paginate(

src/pages/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type UpdateCommentType = {
88
description: string
99
createdAt: number
1010
postedBy: User
11+
hasLiked: boolean
1112
likes: {
1213
data: number
1314
}
@@ -19,6 +20,7 @@ export type HomePageFeedUpdateType = {
1920
description: string
2021
createdAt: number
2122
postedBy: User
23+
hasLiked: boolean
2224
likes: {
2325
data: number
2426
}

0 commit comments

Comments
 (0)