-
Notifications
You must be signed in to change notification settings - Fork 26
/
App.js
87 lines (78 loc) · 2.48 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
import React, { Component } from 'react';
import { StackNavigator } from 'react-navigation';
import { View, StyleSheet } from 'react-native';
import { HomeScreen, DetailScreen, UserScreen } from './screens';
import { initializeFirebase, subscribeToTrack, listenFirebaseChanges } from './utils/firebaseService';
import { handleUserLogin } from './utils/authenticationService';
import getShiftData from './utils/shiftService';
const Navigator = StackNavigator({
Home: { screen: HomeScreen },
Detail: { screen: DetailScreen },
User: { screen: UserScreen },
});
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default class App extends Component {
constructor(props) {
super(props);
this.firebaseRefs = {};
this.state = {
shiftData: [],
userInfo: {},
usersPerSchedule: {},
};
}
componentWillMount() {
initializeFirebase();
getShiftData()
.then((response) => {
this.setState({ shiftData: response.data });
return response.data;
})
.then((shiftData) => {
shiftData.forEach((shiftSchedule) => {
const firebaseRef = listenFirebaseChanges(shiftSchedule.name);
firebaseRef.on('value', snapshot => this.onChangeUsers(snapshot, shiftSchedule.name));
this.firebaseRefs[shiftSchedule.name] = firebaseRef;
});
});
}
componentWillUnmount() {
Object.keys(this.firebaseRefs).forEach(trackId => this.firebaseRefs[trackId].off('value', this.onChangeUsers));
}
onChangeUsers = (snapshot, trackId) => {
const visitors = snapshot.val() && snapshot.val().userIds;
this.setState({
usersPerSchedule: { ...this.state.usersPerSchedule, [trackId]: visitors },
});
};
handleUserLogin = async () => {
const userInfo = await handleUserLogin();
this.setState({ userInfo });
};
render() {
const { userInfo } = this.state;
return (
<View style={styles.container}>
<Navigator
screenProps={{
shiftData: this.state.shiftData,
userInfo,
login: () => this.handleUserLogin(),
onChangeSubscription: trackId =>
subscribeToTrack({
trackId,
currentUserId: this.state.userInfo.id,
subscribedUsers: this.state.usersPerSchedule[trackId] || [],
}),
userId: this.state.userInfo.id,
usersPerSchedule: this.state.usersPerSchedule,
}}
/>
</View>
);
}
}