-
Notifications
You must be signed in to change notification settings - Fork 110
/
navigation.js
239 lines (215 loc) · 6.94 KB
/
navigation.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
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { NavigationActions, addNavigationHelpers, StackNavigator, TabNavigator } from 'react-navigation';
import {
createReduxBoundAddListener,
createReactNavigationReduxMiddleware,
} from 'react-navigation-redux-helpers';
import { Text, View, StyleSheet } from 'react-native';
import { connect } from 'react-redux';
import { graphql, compose } from 'react-apollo';
import update from 'immutability-helper';
import { map } from 'lodash';
import { Buffer } from 'buffer';
import { REHYDRATE } from 'redux-persist';
import Groups from './screens/groups.screen';
import Messages from './screens/messages.screen';
import FinalizeGroup from './screens/finalize-group.screen';
import GroupDetails from './screens/group-details.screen';
import NewGroup from './screens/new-group.screen';
import Signin from './screens/signin.screen';
import Settings from './screens/settings.screen';
import { USER_QUERY } from './graphql/user.query';
import MESSAGE_ADDED_SUBSCRIPTION from './graphql/message-added.subscription';
import GROUP_ADDED_SUBSCRIPTION from './graphql/group-added.subscription';
import { wsClient } from './app';
import { LOGOUT } from './constants/constants';
// tabs in main screen
const MainScreenNavigator = TabNavigator({
Chats: { screen: Groups },
Settings: { screen: Settings },
}, {
initialRouteName: 'Chats',
});
const AppNavigator = StackNavigator({
Main: { screen: MainScreenNavigator },
Signin: { screen: Signin },
Messages: { screen: Messages },
GroupDetails: { screen: GroupDetails },
NewGroup: { screen: NewGroup },
FinalizeGroup: { screen: FinalizeGroup },
}, {
mode: 'modal',
});
// reducer initialization code
const initialState=AppNavigator.router.getStateForAction(NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({
routeName: 'Main',
}),
],
}));
// reducer code
export const navigationReducer = (state = initialState, action) => {
let nextState = AppNavigator.router.getStateForAction(action, state);
switch (action.type) {
case REHYDRATE:
// convert persisted data to Immutable and confirm rehydration
if (!action.payload || !action.payload.auth || !action.payload.auth.jwt) {
const { routes, index } = state;
if (routes[index].routeName !== 'Signin') {
nextState = AppNavigator.router.getStateForAction(
NavigationActions.navigate({ routeName: 'Signin' }),
state,
);
}
}
break;
case LOGOUT:
const { routes, index } = state;
if (routes[index].routeName !== 'Signin') {
nextState = AppNavigator.router.getStateForAction(
NavigationActions.navigate({ routeName: 'Signin' }),
state,
);
}
break;
default:
nextState = AppNavigator.router.getStateForAction(action, state);
break;
}
// Simply return the original `state` if `nextState` is null or undefined.
return nextState || state;
};
// Note: createReactNavigationReduxMiddleware must be run before createReduxBoundAddListener
export const navigationMiddleware = createReactNavigationReduxMiddleware(
"root",
state => state.nav,
);
const addListener = createReduxBoundAddListener("root");
class AppWithNavigationState extends Component {
componentWillReceiveProps(nextProps) {
if (!nextProps.user) {
if (this.groupSubscription) {
this.groupSubscription();
}
if (this.messagesSubscription) {
this.messagesSubscription();
}
// clear the event subscription
if (this.reconnected) {
this.reconnected();
}
} else if (!this.reconnected) {
this.reconnected = wsClient.onReconnected(() => {
this.props.refetch(); // check for any data lost during disconnect
}, this);
}
if (nextProps.user && nextProps.user.id === nextProps.auth.id &&
(!this.props.user || nextProps.user.groups.length !== this.props.user.groups.length)) {
// unsubscribe from old
if (typeof this.messagesSubscription === 'function') {
this.messagesSubscription();
}
// subscribe to new
if (nextProps.user.groups.length) {
this.messagesSubscription = nextProps.subscribeToMessages();
}
}
if (!this.groupSubscription && nextProps.user) {
this.groupSubscription = nextProps.subscribeToGroups();
}
}
render() {
return (
<AppNavigator navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state: this.props.nav,
addListener,
})} />
);
}
}
AppWithNavigationState.propTypes = {
auth: PropTypes.shape({
id: PropTypes.number,
jwt: PropTypes.string,
}),
dispatch: PropTypes.func.isRequired,
nav: PropTypes.object.isRequired,
refetch: PropTypes.func,
subscribeToGroups: PropTypes.func,
subscribeToMessages: PropTypes.func,
user: PropTypes.shape({
id: PropTypes.number.isRequired,
email: PropTypes.string.isRequired,
groups: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
}),
),
}),
};
const mapStateToProps = ({ auth, nav }) => ({
auth,
nav,
});
const userQuery = graphql(USER_QUERY, {
skip: ownProps => !ownProps.auth || !ownProps.auth.jwt,
options: ownProps => ({ variables: { id: ownProps.auth.id } }),
props: ({ data: { loading, user, refetch, subscribeToMore } }) => ({
loading,
user,
refetch,
subscribeToMessages() {
return subscribeToMore({
document: MESSAGE_ADDED_SUBSCRIPTION,
variables: {
groupIds: map(user.groups, 'id'),
},
updateQuery: (previousResult, { subscriptionData }) => {
const previousGroups = previousResult.user.groups;
const newMessage = subscriptionData.data.messageAdded;
const groupIndex = map(previousGroups, 'id').indexOf(newMessage.to.id);
return update(previousResult, {
user: {
groups: {
[groupIndex]: {
messages: {
edges: {
$set: [{
__typename: 'MessageEdge',
node: newMessage,
cursor: Buffer.from(newMessage.id.toString()).toString('base64'),
}],
},
},
},
},
},
});
},
});
},
subscribeToGroups() {
return subscribeToMore({
document: GROUP_ADDED_SUBSCRIPTION,
variables: { userId: user.id },
updateQuery: (previousResult, { subscriptionData }) => {
const newGroup = subscriptionData.data.groupAdded;
return update(previousResult, {
user: {
groups: { $push: [newGroup] },
},
});
},
});
},
}),
});
export default compose(
connect(mapStateToProps),
userQuery,
)(AppWithNavigationState);