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

Skip to content

Commit 7365def

Browse files
committed
Do not refetch the entire feed when a new comment is added
1 parent c7d9a8b commit 7365def

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

src/components/goals/NewComment.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useMutation, useQueryClient } from 'react-query'
66
import toast, { Toaster } from 'react-hot-toast'
77
import { useRef, useState } from 'react'
88
import { Markdown, A } from '@/components'
9+
import { HomePageFeedUpdateType, UpdateCommentType } from 'src/pages'
910

1011
type Inputs = {
1112
description: string
@@ -35,11 +36,23 @@ export default function NewComment({ updateId }: { updateId: string }) {
3536
return res.json()
3637
}),
3738
{
38-
onSuccess: () => {
39+
onSuccess: (data: UpdateCommentType) => {
3940
toast.success('You have successfully added your comment.', {
4041
id: toastId.current,
4142
})
42-
queryClient.refetchQueries('/api/fauna/all-updates')
43+
if (queryClient.getQueryState('/api/fauna/all-updates')) {
44+
queryClient.setQueryData<{ updates: HomePageFeedUpdateType[] }>(
45+
'/api/fauna/all-updates',
46+
(oldData) => ({
47+
updates: oldData.updates.map((_update) => {
48+
if (_update.id === data.updateId) {
49+
_update.comments.data = [..._update.comments.data, data]
50+
}
51+
return _update
52+
}),
53+
})
54+
)
55+
}
4356
reset()
4457
},
4558
onError: () => {

src/pages/api/fauna/add-comment.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getSession } from 'next-auth/client'
33

44
import faunadb from 'faunadb'
55
import { User } from 'src/pages/members'
6+
import { getCommentFromCommentRef, getUserFromUserRef } from 'src/utils/fauna'
67
const q = faunadb.query
78
const isProduction = process.env.NODE_ENV === 'production'
89
const client = new faunadb.Client({
@@ -26,25 +27,34 @@ const FaunaCreateHandler: NextApiHandler = async (
2627

2728
try {
2829
const response: any = await client.query(
29-
q.Create(q.Collection('update_comments'), {
30-
data: {
31-
postedBy: q.Ref(q.Collection('users'), userId),
32-
update: q.Ref(q.Collection('goal_updates'), updateId),
33-
description,
34-
timestamps: {
35-
createdAt: q.Now(),
36-
updatedAt: q.Now(),
37-
},
30+
q.Let(
31+
{
32+
commentDoc: q.Create(q.Collection('update_comments'), {
33+
data: {
34+
postedBy: q.Ref(q.Collection('users'), userId),
35+
update: q.Ref(q.Collection('goal_updates'), updateId),
36+
description,
37+
timestamps: {
38+
createdAt: q.Now(),
39+
updatedAt: q.Now(),
40+
},
41+
},
42+
}),
3843
},
39-
})
44+
getCommentFromCommentRef({
45+
ref: q.Select(['ref'], q.Var('commentDoc')),
46+
session,
47+
})
48+
)
4049
)
50+
4151
await client.query(
4252
q.Let(
4353
{
4454
activityDoc: q.Create(q.Collection('activities'), {
4555
data: {
4656
user: q.Ref(q.Collection('users'), userId),
47-
resource: q.Ref(q.Collection('update_comments'), response.ref.id),
57+
resource: q.Ref(q.Collection('update_comments'), response.id),
4858
type: 'COMMENTED_ON_UPDATE',
4959
timestamps: {
5060
createdAt: q.Now(),
@@ -73,7 +83,7 @@ const FaunaCreateHandler: NextApiHandler = async (
7383
{}
7484
)
7585
)
76-
res.status(201).json({ response })
86+
res.status(201).json(response)
7787
} catch (error) {
7888
console.error(error)
7989
console.error({ msg: error.message })

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

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

33
import faunadb from 'faunadb'
44
import { getSession } from 'next-auth/client'
5-
import { User } from 'src/pages/members'
6-
import { getUpdateFromUpdateRef, getUserFromUserRef } from 'src/utils/fauna'
5+
import { getUpdateFromUpdateRef } from 'src/utils/fauna'
76
const q = faunadb.query
87
const isProduction = process.env.NODE_ENV === 'production'
98
const client = new faunadb.Client({

0 commit comments

Comments
 (0)