@@ -15,7 +15,7 @@ import { HomePageFeedUpdateType } from 'src/pages'
15
15
import { DateTime } from 'luxon'
16
16
import { useMutation , useQuery , useQueryClient } from 'react-query'
17
17
import classNames from 'classnames'
18
- import { useEffect , useReducer , useState , useRef } from 'react'
18
+ import { useState , useRef } from 'react'
19
19
import { signIn , useSession } from 'next-auth/client'
20
20
import { User } from 'src/pages/members'
21
21
import useFollowUser from './profile/useFollowUser'
@@ -40,28 +40,7 @@ import { scrollToContentWithId } from 'src/utils'
40
40
import { IconBrandDiscord } from 'tabler-icons'
41
41
import EditUpdate from './goals/EditUpdate'
42
42
import toast , { Toaster } from 'react-hot-toast'
43
-
44
- type LikeData = {
45
- count : number
46
- hasLiked : boolean
47
- }
48
- const initialState : LikeData = { count : 0 , hasLiked : false }
49
-
50
- function reducer ( state : LikeData , action : { type : string ; payload ?: any } ) {
51
- switch ( action . type ) {
52
- case 'toggle' :
53
- return {
54
- hasLiked : ! state . hasLiked ,
55
- count : state . hasLiked
56
- ? Number ( state . count ) - 1
57
- : Number ( state . count ) + 1 ,
58
- }
59
- case 'set' :
60
- return { count : action . payload . count , hasLiked : action . payload . hasLiked }
61
- default :
62
- throw new Error ( )
63
- }
64
- }
43
+ import { useLikes } from './useLikes'
65
44
66
45
export function HomePageFeedUpdate ( {
67
46
update,
@@ -78,36 +57,6 @@ export function HomePageFeedUpdate({
78
57
const { postedBy, createdAt : createdAtInMillis , goal, description } = update
79
58
const createdAt = DateTime . fromMillis ( createdAtInMillis )
80
59
const toastId = useRef ( '' )
81
- const { isLoading, isError, data } = useQuery (
82
- [ 'api/fauna/has-liked' , update . id ] ,
83
- ( ) => {
84
- return fetch ( `/api/fauna/has-liked` , {
85
- method : 'POST' ,
86
- headers : {
87
- 'Content-Type' : 'application/json' ,
88
- } ,
89
- body : JSON . stringify ( {
90
- updateId : update . id ,
91
- } ) ,
92
- } ) . then ( ( res ) => res . json ( ) )
93
- }
94
- )
95
- const { mutate } = useMutation ( ( ) => {
96
- return fetch ( `/api/fauna/toggle-like` , {
97
- method : 'POST' ,
98
- headers : {
99
- 'Content-Type' : 'application/json' ,
100
- } ,
101
- body : JSON . stringify ( {
102
- updateId : update . id ,
103
- } ) ,
104
- } ) . then ( ( res ) => {
105
- if ( ! res . ok ) {
106
- throw new Error ( 'something went wrong!!!' )
107
- }
108
- return res . json ( )
109
- } )
110
- } )
111
60
112
61
const { mutate : deleteUpdate } = useMutation (
113
62
( ) => {
@@ -143,19 +92,22 @@ export function HomePageFeedUpdate({
143
92
}
144
93
)
145
94
146
- const [ { count : likesCount , hasLiked } , dispatch ] = useReducer (
147
- reducer ,
148
- initialState
149
- )
150
-
151
- useEffect ( ( ) => {
152
- if ( ! isLoading && ! isError ) {
153
- dispatch ( {
154
- type : 'set' ,
155
- payload : { hasLiked : data . liked , count : update . likes . data } ,
156
- } )
157
- }
158
- } , [ data ?. liked , isError , isLoading , update . likes . data ] )
95
+ const { count : likesCount , hasLiked, toggleLike } = useLikes ( {
96
+ 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
+ } ,
104
+ mutation : {
105
+ endpoint : '/api/fauna/toggle-update-like' ,
106
+ body : {
107
+ updateId : update . id ,
108
+ } ,
109
+ } ,
110
+ } )
159
111
160
112
return (
161
113
< >
@@ -270,8 +222,7 @@ export function HomePageFeedUpdate({
270
222
setIsLikeModalOpen ( true )
271
223
return
272
224
}
273
- dispatch ( { type : 'toggle' } )
274
- mutate ( )
225
+ toggleLike ( )
275
226
} }
276
227
>
277
228
< ThumbsUp
0 commit comments