File tree 1 file changed +17
-21
lines changed
apps/resources/components
1 file changed +17
-21
lines changed Original file line number Diff line number Diff line change @@ -42,29 +42,25 @@ export const Card = ({
42
42
likes,
43
43
} : Props ) => {
44
44
const utils = trpc . useContext ( ) ;
45
- const mutation = trpc . resources . like . useMutation ( ) ;
45
+ const mutation = trpc . resources . like . useMutation ( {
46
+ onMutate : ( { id, type } ) => {
47
+ const oldData = utils . resources . get . getData ( ) ;
48
+ const newData = oldData ?. map ( ( data ) => {
49
+ if ( data . id === id && data . type === type ) {
50
+ return { ...data , likes : data . likes + 1 } ;
51
+ }
52
+ return data ;
53
+ } ) ;
54
+ utils . resources . get . setData ( undefined , newData ) ;
55
+ return { oldData } ;
56
+ } ,
57
+ onError : ( error , { id, type } , context ) => {
58
+ utils . resources . get . setData ( undefined , context ?. oldData ) ;
59
+ } ,
60
+ } ) ;
46
61
47
62
const likeResource = ( ) => {
48
- mutation . mutate (
49
- {
50
- id : resourceId ,
51
- type : resourceType ,
52
- } ,
53
- {
54
- onSuccess : ( newData ) => {
55
- utils . resources . get . setData ( undefined , ( oldData ) => {
56
- if ( ! oldData ) return oldData ;
57
-
58
- return oldData . map ( ( data ) => {
59
- if ( data . id === newData . id && data . type === newData . type ) {
60
- return { ...data , likes : newData . likes } ;
61
- }
62
- return data ;
63
- } ) ;
64
- } ) ;
65
- } ,
66
- }
67
- ) ;
63
+ mutation . mutate ( { id : resourceId , type : resourceType } ) ;
68
64
} ;
69
65
70
66
const getType = ( ) => {
You can’t perform that action at this time.
0 commit comments