Repository navigation
Expand file tree
/
Copy pathHome.js
More file actions
174 lines (146 loc) · 4.98 KB
/
Copy pathHome.js
File metadata and controls
174 lines (146 loc) · 4.98 KB
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
import React, { Component } from 'react';
import { TouchableOpacity, FlatList, StyleSheet, Text, View, Image } from 'react-native';
import {f, auth, database, storage} from '../config/config';
import { NavigationActions, StackActions } from 'react-navigation';
export default class Home extends Component {
constructor(props) {
super(props);
this.state = ({
photo_feed: [],
refresh: false,
loading: true
});
}
componentDidMount = () => {
//load feed
this.loadFeed();
}
pluralCheck = (s) => {
if(s==1){
return 'ago';
}else {
return 's ago';
}
}
timeConverter = (timestamp) => {
var a = new Date(timestamp *1000);
var seconds = Math.floor((new Date() - a)/1000);
var interval = Math.floor(seconds / 31536000);
if(interval > 1){
return interval + 'year' +this.pluralCheck(interval);
}
interval = Math.floor(seconds / 2592000);
if(interval > 1){
return interval + 'month' +this.pluralCheck(interval);
}
interval = Math.floor(seconds / 86400);
if(interval > 1){
return interval + 'day' +this.pluralCheck(interval);
}
interval = Math.floor(seconds / 3600);
if(interval > 1){
return interval + 'hour' +this.pluralCheck(interval);
}
interval = Math.floor(seconds / 60);
if(interval > 1){
return interval + 'minute' +this.pluralCheck(interval);
}
return Math.floor(seconds) + 'second' +this.pluralCheck(seconds);
}
addToFlatList = (photo_feed, data, photo) => {
var that=this;
var photoObj = data[photo];
database.ref('users').child(photoObj.author).child('username').once('value').then(function(snapshot) {
const exists = (snapshot.val() !== null);
if(exists) data = snapshot.val();
photo_feed.push({
id: photo,
url: photoObj.url,
caption: photoObj.caption,
posted: that.timeConverter(photoObj.posted),
author: data,
authorId: photoObj.author
});
console.log(photo_feed)
that.setState({
refresh: false,
loading: false
});
}).catch(error => console.log(error));
}
loadFeed = () => {
this.setState({
refresh: true,
photo_feed: []
});
var that = this;
var data;
database.ref('photos').orderByChild('posted').once('value').then(function(snapshot) {
const exists = (snapshot.val() !== null);
if(exists) data = snapshot.val();
var photo_feed = that.state.photo_feed;
for(var photo in data){
that.addToFlatList(photo_feed, data, photo)
}
}).catch(error => console.log(error));
}
loadNew = () => {
this.loadFeed();
}
renderItem = ({item}) => {
return (
<View style={{flex: 1}}>
{/* <Flatlist
refreshing={this.state.refresh}
onRefresh = {this.loadNew}
data={this.state.photo_feed}
keyExtractor={(item, index) => index.toString()}
style={{flex: 1, backgroundColor: '#eee'}}
renderItem={({item, index}) => (
)}
/> */}
<View style={{width: '100%', overflow: 'hidden', marginBottom: 5, justifyContent: 'space-between', borderBottomWidth: 1, borderColor: 'grey'}}>
<View style={{padding: 5, width: '100%', flexDirection: 'row', justifyContent: 'space-between'}}>
<Text>{item.posted}</Text>
<TouchableOpacity onPress={() => this.props.navigation.navigate('User', {userId: item.authorId})}>
<Text>{item.author}</Text>
</TouchableOpacity>
</View>
<View>
<Image
source = {{uri: item.url}}
style={{resizeMode: 'cover', width: '100%', height: 275 }}
/>
</View>
</View>
<View style={{padding:5}}>
<Text>{item.caption}</Text>
<TouchableOpacity onPress={() => this.props.navigation.navigate('Comments', {userId: item.id})}>
<Text style={{marginTop: 10, textAlign: 'center', color: 'blue'}}>[ View Comments ] </Text>
</TouchableOpacity>
</View>
</View>
);
}
render() {
return(
<View style={{ flex: 1,}} >
<View style={{height: 70, paddingTop: 30, backgroundColor: 'white', borderColor: 'lightgrey', borderBottomWidth: 0.5, justifyContent: 'center', alignItems: 'center'}}>
<Text> Feed</Text>
</View>
<FlatList
refreshing={this.state.refresh}
onRefresh={this.loadNew}
data={this.state.photo_feed}
keyExtractor={(item, index) => index.toString()}
/* keyExtractor = {(item) => {
return item.name;
}} */
style={{flex: 1, backgroundColor: '#eee'}}
//renderItem={({item, index}) => {this.renderItem}}
renderItem={this.renderItem}
/>
</View>
);
}
}