-
Notifications
You must be signed in to change notification settings - Fork 817
Expand file tree
/
Copy pathtrip-counter.tsx
More file actions
54 lines (46 loc) · 1.06 KB
/
trip-counter.tsx
File metadata and controls
54 lines (46 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React, { useEffect } from 'react';
import { useQuery, gql } from '@apollo/client';
const TRIPS_QUERY = gql`
query TripsQuery {
totalTripsBooked
}
`;
const TRIPS_SUBSCRIPTION = gql`
subscription TripsSubscription {
tripsBooked
}
`;
function TripCounterInner(props: any) {
useEffect(() =>
props.subscribeToMore({
document: TRIPS_SUBSCRIPTION,
updateQuery: (
prev: any,
{ subscriptionData }: { subscriptionData: any }
) => {
const totalTripsBooked =
prev.totalTripsBooked + subscriptionData.data.tripsBooked;
return {
...prev,
totalTripsBooked
};
}
})
)
return <p>Trips booked: {props.tripsBooked}</p>
}
export default function TripCounter() {
const { data, loading, error, subscribeToMore } = useQuery(TRIPS_QUERY);
if (loading) {
return <p>Loading...</p>
}
if (error) {
return <p>{error.message}</p>
}
return (
<TripCounterInner
tripsBooked={data.totalTripsBooked}
subscribeToMore={subscribeToMore}
/>
)
}