-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHome.js
208 lines (200 loc) · 6.7 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
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
import React, {Component} from 'react';
import {ActivityIndicator, SectionList, Text, Keyboard} from 'react-native';
import {ListItem} from 'react-native-elements';
import {firebaseApp} from './App';
import Details from './Details';
import {StyleProvider, Container, Header, Title, Content, Footer, FooterTab, Button, Left, Right, Body, Icon, Item, Input, Label} from 'native-base';
import getTheme from './native-base-theme/components';
import material from './native-base-theme/variables/material';
export default class HomeScreen extends Component {
constructor(props) {
super(props);
this.state = {
dataSource: [],
data: [],
loading: true,
searchText: "",
searching: false,
viewDetails: false,
curItem: null,
isSaved: false,
};
this.itemsRef = firebaseApp.database().ref('items');
let uid = firebaseApp.auth().currentUser.uid;
this.userRef = firebaseApp.database().ref('users').child(uid).child("saved_events");
console.ignoredYellowBox = ['Setting a timer'];
}
// manages whether to display details of item or not
onLearnMore = (item, userRef) => {
let isCurItemSaved = false;
// get key of event + check if it is in user's saved events. if it is, color=red
key = item.key;
console.log(key);
userRef.child(item.date).once('value').then((snap) => {
snap.forEach((child) => {
console.log('key: ' + child.val().key)
if (child.val().key === key) {
isCurItemSaved = true;
}
});
this.setState({
viewDetails: true,
curItem: item,
isSaved: isCurItemSaved,
})
});
};
goBack = () => {
this.setState({
viewDetails: false
})
}
firstSearch() {
this.searchEvents(this.itemsRef);
}
// only return events with names/descriptions containing search text
searchEvents(itemsRef) {
var searchText = this.state.searchText.toString().toLowerCase();
var results = [];
if (searchText == ""){
this.listenForItems(itemsRef);
} else {
items = this.state.dataSource;
items.forEach((parent) => {
var children = [];
parent.data.forEach((child) => {
if (child.name.toLowerCase().includes(searchText) ||
child.what.toLowerCase().includes(searchText) ||
child.who.toLowerCase().includes(searchText)) {
children.push(child);
}
});
if (children.length != 0) {
results.push({
data: children,
key: parent.key.toUpperCase()
})
}
});
}
this.setState({data: results});
}
listenForItems(itemsRef) {
itemsRef.on('value', (snap) => {
// get children as an array
var items = [];
snap.forEach((parent) => {
var children = [];
parent.forEach((child) => {
children.push({
"key": child.key,
"name": child.val().name,
"startTime": child.val().startTime,
"endTime": child.val().endTime,
"date": parent.key,
"who": child.val().who,
"where": child.val().where,
"what": child.val().what,
"latitude": child.val().latitude,
"longitude": child.val().longitude
});
});
children.sort((a, b) => { // sort children by time
timeA = this.time(a.startTime);
timeB = this.time(b.startTime);
return new Date('2017/01/01 ' + timeA) - new Date('2017/01/01 ' + timeB);
});
items.push({
data: children,
key: parent.key.toUpperCase(),
})
});
let sorted = items.sort((a, b) => { // sort parents by date
var month = {"JAN": 1, "FEB": 2, "MAR": 3, "APR": 4, "MAY": 5, "JUN": 6,
"JUL": 7, "AUG": 8, "SEP": 9, "OCT": 10, "NOV": 11, "DEC": 12};
month1 = month[a.key.substring(0, 3)];
month2 = month[b.key.substring(0, 3)];
if (month1 - month2 > 10) { return -1; } // December, January
else if (month1 - month2 < -10){ return 1; } // January, December
else if (month1 < month2) { return -1; }
else if (month1 == month2) {
if (parseInt(a.key.substring(4)) < parseInt(b.key.substring(4))) {
return -1;
} else {
return 1;
}
}
else { return 1; }
});
this.setState({
dataSource: items,
data: items,
loading: false
});
});
}
// transform time into a format that javascript can easily sort
time(time) {
var newTime = time;
if (time.charAt(1) == ':') newTime = '0' + time;
newTime = newTime.substring(0, 6);
if (time.charAt(6) == 'a') newTime = newTime + "am";
else newTime = newTime + "pm";
return newTime;
}
// autoupdates on new event
componentDidMount() {
this.listenForItems(this.itemsRef);
}
// removes listener
componentWillUnmount() {
this.itemsRef.off();
}
render() {
var styles = require('./Styles');
if(!this.state.viewDetails) {
return (
<StyleProvider style={getTheme(material)}>
<Container>
{!this.state.searching &&
<Header>
<Body><Title>Princeton Events</Title></Body>
<Right><Button transparent onPress={() => this.setState({searching: true})}>
<Icon name="ios-search"/></Button>
</Right>
</Header>
}
{this.state.searching && <Header searchBar rounded>
<Item>
<Icon name="ios-search"/>
<Input placeholder="Search..." returnKeyType='search'
onChangeText={(text) => {this.setState({searchText:text}); this.firstSearch();}}
onSearch={() => this.firstSearch()}/>
<Label style={{color: 'black'}} onPress={() => {
Keyboard.dismiss();
this.setState({data: this.state.dataSource, searching: false})}}>
<Text style={{fontSize: 12}}>Cancel</Text>
</Label>
</Item>
</Header>}
<Content style={{backgroundColor: 'white'}}>
{this.state.loading && <ActivityIndicator size="large" style={{marginTop: 200}}/>}
{!this.state.loading && <SectionList renderItem={({item}) =>
<ListItem style={styles.item}
title={item.name} subtitle={item.startTime}
onPress={() => this.onLearnMore(item, this.userRef)}/>}
renderSectionHeader={({section}) =>
<Text style={styles.sectionHeader}>{section.key}</Text>}
sections={this.state.data} keyExtractor={(item) => item.key}/>}
</Content>
</Container>
</StyleProvider>
);
}
else {
return(
<Details goBack={this.goBack} item={this.state.curItem} isSaved={this.state.isSaved}/>
);
}
}
}