-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
123 lines (109 loc) · 3.81 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
import * as Amplitude from 'expo-analytics-amplitude';
import React, { useEffect, useState } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { AsyncStorage } from 'react-native';
import Contact, { ContactsContext } from './contacts';
import ImportContacts from './pages/ImportContacts';
import ImportAddressBook from './pages/ImportAddressBook';
import PersonDetails from './pages/PersonDetail';
import EditBirthday from './pages/EditBirthday';
import EditCounty from './pages/EditCounty';
import CheckVoteStatus from './pages/CheckVoteStatus';
import PersonList from './pages/PersonList';
import ReachOut from './pages/ReachOut';
const Stack = createStackNavigator();
export default function App() {
const [contacts, _setContacts] = useState({});
async function loadContacts() {
try {
const contacts = Contact.deserializeList(await AsyncStorage.getItem('CONTACTS'));
if (contacts == null) return;
_setContacts(contacts);
} catch (e) {
console.error('Failed to load contacts', e);
}
}
useEffect(() => {
loadContacts();
if (__DEV__) {
Amplitude.initialize('e7f3f95efb0bca7a310f9cd2d253449a');
} else {
Amplitude.initialize('73614b74fd0c321cbbb7f05db8a92a1e');
}
}, []);
async function setContacts(contacts) {
try {
await AsyncStorage.setItem('CONTACTS', Contact.serializeList(contacts));
_setContacts(contacts);
} catch (e) {
console.error('Failed to save contacts', e);
}
}
async function updateContact(id, data) {
const logProperties = {
field: Object.keys(data)[0],
};
if ('voteStatus' in data) {
logProperties['voteStatus'] = data['voteStatus'];
}
Amplitude.logEventWithProperties('UPDATE_CONTACT', logProperties);
const newContact = Contact.deserialize(contacts[id].serialize()); // deep copy
Object.assign(newContact.data, data);
newContact.data.modified = new Date();
const newContacts = {...contacts};
newContacts[id] = newContact;
setContacts(newContacts);
}
function addToContacts(newContact) {
Amplitude.logEventWithProperties('ADD_CONTACT', {
source: newContact.source
});
Amplitude.setUserProperties({
numContacts: Object.keys(contacts).length + 1
});
setContacts({
...contacts,
[newContact.id]: newContact
});
}
function removeContact(id) {
const newContacts = {...contacts};
delete newContacts[id];
setContacts(newContacts);
}
function clearContacts() {
setContacts({});
}
return (
<ContactsContext.Provider value={{
contacts: contacts,
clearContacts: clearContacts,
addToContacts: addToContacts,
updateContact: updateContact,
removeContact: removeContact,
}}>
<NavigationContainer>
<Stack.Navigator initialRouteName="Your Contacts"
screenOptions={{
headerStyle: {
backgroundColor: '#55dff1',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
}} >
<Stack.Screen name="Your Contacts" component={PersonList} />
<Stack.Screen name="Import Contacts" component={ImportContacts} />
<Stack.Screen name="Import from Address Book" component={ImportAddressBook} />
<Stack.Screen name="Person Details" component={PersonDetails} />
<Stack.Screen name="Edit Birthday" component={EditBirthday} />
<Stack.Screen name="Edit County" component={EditCounty} />
<Stack.Screen name="Check Vote Status" component={CheckVoteStatus} />
<Stack.Screen name="Reach Out" component={ReachOut} />
</Stack.Navigator>
</NavigationContainer>
</ContactsContext.Provider>
);
}