-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHome.js
80 lines (68 loc) · 2.31 KB
/
Home.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
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import NetInfo from '@react-native-community/netinfo';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import React, { Component } from 'react';
import HomeScreen from './components/HomeScreen/HomeScreen';
import ForMeScreen from './components/ForMeScreen/ForMeScreen';
import NearMeScreen from './components/NearMeScreen/NearMeScreen';
import NowScreen from './components/NowScreen/NowScreen';
import { getAllData, getMeetingData, getLocation } from './util/apiCalls';
import { setAllResources, setAllCategories, setAllMeetings } from './actions';
export const MainNavigator = createStackNavigator({
Home: { screen: HomeScreen },
ForMe: { screen: ForMeScreen },
NearMe: { screen: NearMeScreen },
Now: { screen: NowScreen },
});
const AppContainer = createAppContainer(MainNavigator);
class Home extends Component {
componentDidMount() {
this.internetCheck();
}
getDataWithConnection = async () => {
const { setAllResources, setAllCategories } = this.props;
const response = await getAllData();
setAllResources(response.data.resources);
setAllCategories(response.data.resources);
};
getMeetings = async () => {
const { setAllMeetings } = this.props;
const responseMeetings = await getMeetingData();
const results = await responseMeetings.data.recovery.map(async (meeting) => {
const location = await getLocation(meeting.address);
return { ...meeting, ...location };
});
const resolvedPromises = await Promise.all(results);
setAllMeetings(resolvedPromises);
};
internetCheck = () => {
NetInfo.fetch().then((state) => {
if (!state.isConnected) {
console.warn('PLEASE CONNECT TO INTERNET');
} else {
this.getDataWithConnection();
this.getMeetings();
}
});
};
render() {
return <AppContainer />;
}
}
export const mapStateToProps = (state) => ({
allResources: state.allResources,
allMeetings: state.allMeetings,
});
export const mapDispatchToProps = (dispatch) => {
return bindActionCreators(
{
setAllResources,
setAllCategories,
setAllMeetings,
},
dispatch,
);
};
export default connect(mapStateToProps, mapDispatchToProps)(Home);