-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reactions.js
178 lines (146 loc) · 4.47 KB
/
Reactions.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
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {
StyleSheet,
FlatList,
Text,
requireNativeComponent,
NativeModules,
View,
ViewPropTypes,
Image,
Dimensions,
Alert
} from 'react-native';
import Api from './Api';
import Tag from './Tag';
import Gif from './Gif';
/**
* Basic styles that are added to the component in addition to the nativeprops
*/
const styles = StyleSheet.create({
base: {
overflow: 'hidden',
},
gfy: {
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
right: 0,
},
});
/**
* Backup cache that is not in the state, so that we can return to this whenever a user clears their input without hitting the API every time.
*/
const tagCache = [];
export default class Reactions extends PureComponent {
constructor(props) {
super();
this.state = {
cache: [],
mode: "tag"
};
}
componentDidMount() {
this._populateEmptyCache();
}
/**
* Populate tags once from the API if necessary. Other requests are made via state since the UI needs to respond.
* @private
*/
_populateEmptyCache() {
// grab from the API only if backup cache is empty.
if (tagCache.length > 0) {
this.setState({
cache: tagCache
})
return;
}
Api.get("/reactions/populated?gfyCount=2").then(body => {
if (body && body.tags) {
body.tags.forEach(gfy => {
if(gfy && gfy.gfycats && gfy.gfycats.length > 0) {
if(tagCache.length < 20)
tagCache.push(gfy);
}
});
this.setState({
cache: tagCache,
mode: "tag"
});
}
});
}
render() {
const nativeProps = Object.assign({}, this.props);
Object.assign(nativeProps, {
style: [styles.base, nativeProps.style],
});
return <View style={nativeProps.style}>
<FlatList
style={{
flex: 1
}}
initialNumToRender={8}
data={this.state.cache}
keyboardShouldPersistTaps={true}
directionalLockEnabled={true}
removeClippedSubviews={true}
numColumns={nativeProps.reactionsPerRow}
renderItem={this._renderItem}
/>
</View>
}
_handleTagSelection = (tag) => {
console.log(tag)
Api.get(`/reactions/populated?tagName=${tag}&gfyCount=20`).then(body => {
console.log(body);
if (body && body.gfycats) {
const results = [];
body.gfycats.forEach(gfy => {
if(results.length < 20) {
console.log("pushing gfy");
results.push(gfy);
}
});
this.setState({
cache: results,
mode: "gfy"
});
}
});
}
_handleGifSelection = (gif) => {
this.props.callback(gif);
}
_renderTag = ({item, index}) => {
const nativeProps = Object.assign({}, this.props);
const necessaryProps = {
src: item.gfycats[0].miniUrl,
poster: item.gfycats[0].miniPosterUrl,
tag: item.tag
}
return <Tag style={nativeProps.tagStyle} reactionsPerRow={nativeProps.reactionsPerRow} tag={necessaryProps} handleTagSelection={this._handleTagSelection}/>
}
_renderGif = ({item, index}) => {
const nativeProps = Object.assign({}, this.props);
const necessaryProps = {
src: item.miniUrl,
poster: item.miniPosterUrl,
}
return <Gif style={nativeProps.tagStyle} reactionsPerRow={nativeProps.reactionsPerRow} gif={necessaryProps} handleGifSelection={this._handleGifSelection}/>
}
_renderItem = ({item, index}) => {
return this.state.mode === "tag" ? this._renderTag({item, index}) : this._renderGif({item, index})
};
}
Reactions.propTypes = {
/* Native only */
reactionsPerRow: PropTypes.number,
autoPlayEnabled: PropTypes.boolean,
rowStyle: PropTypes.object,
tagStyle: PropTypes.object,
callback: PropTypes.function,
...ViewPropTypes,
};