-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyEvents.js
139 lines (133 loc) · 4.13 KB
/
MyEvents.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
import React, {Component} from 'react';
import {ActivityIndicator, FlatList, BackHandler} from 'react-native';
import {ListItem, List, ListView} from 'react-native-elements';
import {firebaseApp} from './App';
import { StyleProvider, Container, Header, Title, Content, Button, Left, Right, Body, Icon} from 'native-base';
import getTheme from './native-base-theme/components';
import material from './native-base-theme/variables/material';
import Edit from './EditPage';
export default class MyEventsScreen extends Component {
constructor(props) {
super(props);
this.state = {
data: [],
loading: true,
viewEdit: false,
curItem: null,
userEmail: null,
uid: null,
userName: null,
};
this.itemsRef = firebaseApp.database().ref().child('items');
let uid = firebaseApp.auth().currentUser.uid;
this.userRef = firebaseApp.database().ref('users').child(uid).child("my_events");
console.ignoredYellowBox = ['Setting a timer'];
}
// manages whether to display edit page or not
onViewMyEvent = (item) => {
this.setState({
viewEdit: true,
curItem: item,
})
};
// takes the edit page back to the my events page
goBack = () => {
this.setState({
viewEdit: false,
loading: true,
})
this.listenForItems(this.itemsRef, this.userRef);
}
// handles hardware back button pressed on Android
// gets events from users' my_events section
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', () => {
this.props.goBack();
return true;
});
this.listenForItems(this.itemsRef, this.userRef);
}
// grabs appropriate events
listenForItems = (itemsRef, userRef) => {
userRef.once('value').then((snap) => {
var items = [];
snap.forEach((parent) => {
var children = [];
parent.forEach((child) => {
children.push(child.val().key);
});
items.push({
date: parent.key,
data: children,
})
});
var events = [];
items.forEach((item) => {
ref = itemsRef.child(item.date);
item.data.forEach((datum) => {
child = ref.child(datum).on('value', (snap) => {
if (snap.val() != null) {
events.push({
"key": snap.key,
"name": snap.val().name,
"startTime": snap.val().startTime,
"endTime": snap.val().endTime,
"date": item.date,
"who": snap.val().who,
"where": snap.val().where,
"what": snap.val().what
});
} else {
let eventRef = userRef.child("my_events").child(item.date);
eventRef.once('value', (snap) => {
snap.forEach((child) => {
if (child.val().key == datum) {
eventRef.child(child.key).remove();
}
})
});
}
});
});
});
this.setState({
data: events,
loading: false
});
});
}
render() {
var styles = require('./Styles');
if(!this.state.viewEdit) {
return (
<StyleProvider style={getTheme(material)}>
<Container>
<Header>
<Left>
<Button transparent onPress={() => this.props.goBack()}>
<Icon name='arrow-back'/>
</Button>
</Left>
<Body>
<Title>My Events</Title>
</Body>
<Right/>
</Header>
<Content style={{backgroundColor: 'white'}}>
{this.state.loading && <ActivityIndicator size="large" style={{marginTop: 200}}/>}
{!this.state.loading && <FlatList data={this.state.data} renderItem={({item}) =>
<ListItem style={styles.item} title={item.name} subtitle={item.startTime} containerStyle={{
borderBottomWidth: 0
}} onPress={() => this.onViewMyEvent(item)}/>} keyExtractor={(item) => item.key}/>}
</Content>
</Container>
</StyleProvider>
);
}
else {
return(
<Edit goBack={this.goBack} item={this.state.curItem}/>
);
}
}
}