-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracker.js
105 lines (93 loc) · 2.66 KB
/
Tracker.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, { Component } from 'react'
import { graphql } from 'react-apollo'
import gql from 'graphql-tag'
import Orders from './Orders'
import _ from 'lodash'
import { Link } from 'react-router-dom'
const createOrder = gql`
mutation createOrder($orderNumber: Int!) {
createOrder(orderNumber: $orderNumber) {
id
orderNumber
orderStatus
createdAt
}
}
`
const allOrders = gql`
query allOrders {
allOrders(orderBy: createdAt_DESC, first: 100) {
id
orderNumber
orderStatus
createdAt
},
_allOrdersMeta {
count
}
}
`
class Tracker extends Component {
state = {
order: '',
}
componentDidMount() {
this.createOrderSubscription = this.props.allOrdersQuery.subscribeToMore({
document: gql`
subscription {
Order(filter: {
mutation_in: [CREATED, UPDATED]
}) {
mutation
node {
id
orderNumber
orderStatus
createdAt
}
}
}
`,
updateQuery: (previousState, {subscriptionData}) => {
if (subscriptionData.data.Order.mutation === 'CREATED') {
const newOrder = subscriptionData.data.Order.node
const orders = [newOrder].concat(previousState.allOrders)
return {
allOrders: orders,
}
}
else if (subscriptionData.data.Order.mutation === 'UPDATED') {
const orders = previousState.allOrders.slice()
const updatedOrder = subscriptionData.data.Order.node
const oldOrderIndex = orders.findIndex(order => {
return updatedOrder.id === order.id
})
orders[oldOrderIndex] = updatedOrder
return {
allOrders: orders,
}
}
},
onError: (err) => console.error('subscription error',err)
})
}
render() {
return (
<main role="main" className="pa3">
<Orders orders={this.props.allOrdersQuery.allOrders || []}/>
<Link className="fixed right-1 bottom-1 link white grow pr1 pt1" to="/admin">▼</Link>
</main>
)
}
_onSend = () => {
// console.log('Send order: ', this.state.order, this.props.travellerId)
this.props.createOrderMutation({
variables: {
orderNumber: this.state.order
}
})
}
}
export default graphql(createOrder, {name : 'createOrderMutation'})(
graphql(allOrders, {name: 'allOrdersQuery'})(Tracker)
)