This repository was archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathApp.js
252 lines (243 loc) · 7.32 KB
/
App.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import React from 'react';
import { StyleSheet, Text, View, Button, Platform } from 'react-native';
import AWSAppSyncClient from "aws-appsync";
import { Rehydrated } from 'aws-appsync-react';
import { AUTH_TYPE } from "aws-appsync/lib/link/auth-link";
import { graphql, ApolloProvider, compose } from 'react-apollo';
import * as AWS from 'aws-sdk';
import appSyncConfig from './aws-exports';
import { StackNavigator } from 'react-navigation';
import AllEvents from './Components/AllEvents'
import AddEvent from "./Components/AddEvent";
import EventComments from './Components/EventComments'
import ListEvents from './queries/ListEvents';
import CreateEvent from './queries/CreateEvent';
import DeleteEvent from './queries/DeleteEvent';
import GetEvent from './queries/GetEvent'
import SubscribeToEventComments from './queries/SubscribeToEventComments'
import CommentOnEvent from './queries/CommentOnEvent'
import uuidV4 from 'uuid/v4'
const client = new AWSAppSyncClient({
url: appSyncConfig.aws_appsync_graphqlEndpoint,
region: appSyncConfig.aws_appsync_region,
auth: {
type: appSyncConfig.aws_appsync_authenticationType,
apiKey: appSyncConfig.aws_appsync_apiKey,
}
});
_button = function (navigation) {
if (Platform.OS === 'ios') {
return <Button title='Create' color='#ffffff' onPress={() => navigation.navigate('AddEvent')} />
} else {
return <Button title='Create' onPress={() => navigation.navigate('AddEvent')} />
}
}
const App = StackNavigator({
AllEvents: {
screen: (props) => <AllEventWithData {...props} />,
navigationOptions: ({ navigation }) => ({
title: 'Upcoming Events',
headerRight: this._button(navigation),
headerStyle: {
backgroundColor: '#42a1f4',
},
headerTitleStyle: {
color: '#ffffff'
},
headerTintColor: '#ffffff'
})
},
AddEvent: {
screen: (props) => <AddEventData {...props} />,
navigationOptions: ({ navigation, screenProps }) => {
return {
title: 'Create Event',
headerStyle: {
backgroundColor: '#42a1f4'
},
headerTitleStyle: {
color: '#ffffff'
},
headerTintColor: '#ffffff'
};
}
},
EventComments: {
screen: (props) => (
<EventCommentsWithSubscription
{...props}
eventId={props.navigation.state.params.eventId}
/>
),
navigationOptions: ({ navigation, screenProps }) => {
return {
title: 'Comments',
headerStyle: {
backgroundColor: '#42a1f4'
},
headerTitleStyle: {
color: '#ffffff'
},
headerTintColor: '#ffffff'
};
}
}
});
const WithProvider = () => (
<ApolloProvider client={client}>
<Rehydrated>
<App />
</Rehydrated>
</ApolloProvider>
);
export default WithProvider;
const AllEventWithData = compose(
graphql(ListEvents, {
options: {
fetchPolicy: 'cache-and-network'
},
props: (props) => ({
events: props.data.listEvents ? props.data.listEvents.items : [],
})
}),
graphql(DeleteEvent, {
options: {
fetchPolicy: 'cache-and-network'
},
props: (props) => ({
onDelete: (event) => {
props.mutate({
variables: { id: event.id },
optimisticResponse: () => ({ deleteEvent: { ...event, __typename: 'Event', comments: { __typename: "CommentConnection", items: [], nextToken: null } } }),
})
}
}),
options: {
refetchQueries: [{ query: ListEvents }],
update: (dataProxy, { data: { deleteEvent: { id } } }) => {
const query = ListEvents;
const data = dataProxy.readQuery({ query });
data.listEvents.items = data.listEvents.items.filter(event => event.id !== id);
dataProxy.writeQuery({ query, data });
}
}
}),
)(AllEvents);
const AddEventData = compose(
graphql(CreateEvent, {
options: {
refetchQueries: [{ query: ListEvents }],
update: (dataProxy, { data: { createEvent } }) => {
const query = ListEvents;
const data = dataProxy.readQuery({ query });
data.listEvents = {
...data.listEvents,
items: [
...data.listEvents.items,
createEvent
]
}
dataProxy.writeQuery({ query, data });
}
},
props: (props) => ({
onAdd: event => {
return props.mutate({
variables: event,
optimisticResponse: () => {
return {
createEvent: { ...event, __typename: 'Event', comments: { __typename: "CommentConnection", items: [], nextToken: null } }
}
},
});
}
})
})
)(AddEvent);
const EventCommentsWithSubscription = compose(
graphql(CommentOnEvent, {
options: props => ({
fetchPolicy: 'cache-and-network',
update: (proxy, { data: { commentOnEvent } }) => {
const query = GetEvent;
const variables = { eventId: props.eventId };
const data = proxy.readQuery({ query, variables });
data.getEvent = {
...data.getEvent,
comments: {
...data.getEvent.comments,
items: [
...data.getEvent.comments.items.filter(c => {
return c.content !== commentOnEvent.content &&
c.createdAt !== commentOnEvent.createdAt &&
c.commentId !== commentOnEvent.commentId
}),
commentOnEvent,
]
}
};
proxy.writeQuery({ query, data });
},
}),
props: props => ({
createComment: (comment) =>
props.mutate({
variables: comment,
optimisticResponse: { commentOnEvent: { ...comment, __typename: 'Comment', commentId: uuidV4() } },
})
})
}),
graphql(GetEvent, {
options: ({ eventId }) => ({
fetchPolicy: 'cache-and-network',
variables: {
eventId,
}
}),
props: props => {
return {
comments: props.data.getEvent ? props.data.getEvent.comments : [],
subscribeToNewComments: () => {
props.data.subscribeToMore({
document: SubscribeToEventComments,
variables: {
eventId: props.ownProps.navigation.state.params.eventId,
},
updateQuery: (prev, { subscriptionData: { data: { subscribeToEventComments } } }) => {
const res = {
...prev,
getEvent: {
...prev.getEvent,
comments: {
nextToken: null,
__typename: 'CommentConnections',
items: [
...prev.getEvent.comments.items.filter(c => {
return (
c.content !== subscribeToEventComments.content &&
c.createdAt !== subscribeToEventComments.createdAt &&
c.commentId !== subscribeToEventComments.commentId
)
}),
subscribeToEventComments,
]
}
},
};
return res;
}
})
}
}
}
})
)(EventComments)
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
backgroundColor: '#fff',
alignItems: 'stretch',
justifyContent: 'center',
},
});