11import React from 'react' ;
2- import { FEED_QUERY } from './LinkList ' ;
3- import { AUTH_TOKEN } from '../constants' ;
2+ import { gql , useMutation } from '@apollo/client ' ;
3+ import { AUTH_TOKEN , LINKS_PER_PAGE } from '../constants' ;
44import { timeDifferenceForDate } from '../utils' ;
5-
6- import { useMutation , gql } from '@apollo/client' ;
5+ import { FEED_QUERY } from './LinkList' ;
76
87const VOTE_MUTATION = gql `
98 mutation VoteMutation($linkId: ID!) {
@@ -27,38 +26,50 @@ const VOTE_MUTATION = gql`
2726const Link = ( props ) => {
2827 const { link } = props ;
2928 const authToken = localStorage . getItem ( AUTH_TOKEN ) ;
30- const [ vote , { loading, error, data } ] = useMutation (
31- VOTE_MUTATION ,
32- {
33- variables : {
34- linkId : link . id
35- } ,
36- update ( cache , { data : { vote } } ) {
37- const { feed } = cache . readQuery ( {
38- query : FEED_QUERY
39- } ) ;
4029
41- const updatedLinks = feed . links . map ( ( feedLink ) => {
42- if ( feedLink . id === link . id ) {
43- return {
44- ...feedLink ,
45- votes : [ ...feedLink . votes , vote ]
46- } ;
47- }
48- return feedLink ;
49- } ) ;
30+ const take = LINKS_PER_PAGE ;
31+ const skip = 0 ;
32+ const orderBy = 'createdAt_DESC' ;
33+
34+ const [ vote ] = useMutation ( VOTE_MUTATION , {
35+ variables : {
36+ linkId : link . id
37+ } ,
38+ update ( cache , { data : { vote } } ) {
39+ const { feed } = cache . readQuery ( {
40+ query : FEED_QUERY ,
41+ variables : {
42+ take,
43+ skip,
44+ orderBy
45+ }
46+ } ) ;
5047
51- cache . writeQuery ( {
52- query : FEED_QUERY ,
53- data : {
54- feed : {
55- links : updatedLinks
56- }
48+ const updatedLinks = feed . links . map ( ( feedLink ) => {
49+ if ( feedLink . id === link . id ) {
50+ return {
51+ ...feedLink ,
52+ votes : [ ...feedLink . votes , vote ]
53+ } ;
54+ }
55+ return feedLink ;
56+ } ) ;
57+
58+ cache . writeQuery ( {
59+ query : FEED_QUERY ,
60+ data : {
61+ feed : {
62+ links : updatedLinks
5763 }
58- } ) ;
59- }
64+ } ,
65+ variables : {
66+ take,
67+ skip,
68+ orderBy
69+ }
70+ } ) ;
6071 }
61- ) ;
72+ } ) ;
6273 return (
6374 < div className = "flex mt2 items-start" >
6475 < div className = "flex items-center" >
0 commit comments