-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHomeScreen.js
76 lines (67 loc) · 2.81 KB
/
HomeScreen.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
import { Notifications } from 'expo';
import React from 'react';
import {Alert, AsyncStorage, FlatList, Linking, Platform, View} from 'react-native';
import {BASE_URL, KEY} from './Constants';
import { HelpRequester } from './HelpRequester';
import {registerForPushNotificationsAsync} from "./PushNotification";
export class HomeScreen extends React.Component {
constructor(props) {
super(props);
this.state = { helpProviderId: 1, helpRequesters: [] };
}
async componentDidMount() {
const expoPushToken = await registerForPushNotificationsAsync();
await AsyncStorage.setItem(KEY, id);
try {
const response = await fetch(`${BASE_URL}/help-providers/${this.state.helpProviderId}/help-requesters`);
const data = await response.json();
this.setState({ helpRequesters: data });
} catch (error) {
console.log(error);
}
Notifications.addListener(notification => {
console.log(notification);
const data = notification.data;
const helpRequester = this.state.helpRequesters.filter(helpRequester => helpRequester.id === data.id);
Alert.alert(
"Emergency",
`Received emergency from: ${helpRequester.name}, ${data.formattedAddress}`,
[
{ text: 'Accept', onPress: () => { this._acceptEmergency(data); HomeScreen._openGoogleMaps(data) } },
{ text: 'Decline', onPress: () => console.log('Cancel Pressed'), style: 'cancel' }
]
)
});
}
_acceptEmergency(emergency) {
console.log(emergency);
fetch(`${BASE_URL}/help-providers/accept-emergency`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ emergencyId: emergency.id, helpRequesterId: emergency.helpRequesterId, helpProviderId: this.state.helpProviderId })
})
}
static _openGoogleMaps(emergency) {
const scheme = Platform.OS === 'ios' ? 'maps:0,0?q=' : 'google.navigation:q=';
const lat = emergency.coordinates.latitude;
const lng = emergency.coordinates.longitude;
const latLng = `${lat}+${lng}`;
const url = Platform.OS === 'ios' ? `${scheme}@${latLng}` : `${scheme}${latLng}`;
Linking.openURL(url);
}
_deleteHelpRequester(id) {
const newState = this.state.helpRequesters.filter(helpRequester => helpRequester.id !== id);
this.setState({ helpRequesters: newState });
}
render() {
return (
<View style={{ flex: 1 }}>
<FlatList data={this.state.helpRequesters} keyExtractor={item => `${item.id}`} renderItem={({ item }) => {
return <HelpRequester name={item.name} lastLocation={item.lastLocation} handler={() => this._deleteHelpRequester(item.id)} />
}} />
</View>
);
}
}