-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.js
259 lines (226 loc) · 6.44 KB
/
Main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import React, { useState, useEffect } from 'react';
import { Image, Dimensions, PanResponder, Animated } from 'react-native';
import { TouchableOpacity } from 'react-native-gesture-handler';
import io from 'socket.io-client';
import AsyncStorage from '@react-native-community/async-storage';
import {
Container,
CardsContainer,
AnimatedCard,
Avatar,
InfoWrapper,
Name,
Bio,
Footer,
FooterButton,
MatchContainer,
MatchSymbol,
MatchAvatar,
MatchName,
MatchBio,
MatchButtonText,
AnimatedTag,
TagText,
} from './Main_Styles';
import logo from '~/assets/logo.png';
import like from '~/assets/like.png';
import dislike from '~/assets/dislike.png';
import itsamatch from '~/assets/itsamatch.png';
import api from '~/services/api';
const SCREEN_WIDTH = Dimensions.get('window').width;
export default function Main({ navigation }) {
const ID = navigation.getParam('user');
const [users, setUsers] = useState([]);
const [itsAMatch, setItsAMatch] = useState(null);
useEffect(() => {
(async () => {
const response = await api.get('devs', {
headers: {
user: ID,
},
});
setUsers(response.data);
})();
}, [ID]);
useEffect(() => {
const socket = io('http://localhost:3333', {
query: { user: ID },
});
socket.on('match', dev => {
setItsAMatch(dev);
});
}, [ID]);
async function handleLogout() {
await AsyncStorage.clear();
navigation.navigate('Login');
}
async function handleDislike() {
const [user, ...rest] = users;
await api.post(`devs/${user._id}/dislikes`, null, {
headers: {
user: ID,
},
});
setUsers(rest);
}
async function handleLike() {
const [user, ...rest] = users;
await api.post(`devs/${user._id}/likes`, null, {
headers: {
user: ID,
},
});
setUsers(rest);
}
const position = new Animated.ValueXY();
const rotate = position.x.interpolate({
inputRange: [-SCREEN_WIDTH, SCREEN_WIDTH],
outputRange: ['-10deg', '10deg'],
extrapolate: 'clamp',
});
const rotateAndTranslate = {
transform: [
{ rotate },
{ translateX: position.x },
{ translateY: position.y },
],
};
const nextCardOpacity = position.x.interpolate({
inputRange: [-SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2],
outputRange: [1, 0, 1],
extrapolate: 'clamp',
});
const nextCardScale = position.x.interpolate({
inputRange: [-SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 2],
outputRange: [1, 0.9, 1],
extrapolate: 'clamp',
});
const likeOpacity = position.x.interpolate({
inputRange: [50, SCREEN_WIDTH / 2],
outputRange: [0, 0.8],
extrapolate: 'clamp',
});
const dislikeOpacity = position.x.interpolate({
inputRange: [-SCREEN_WIDTH / 2, -50],
outputRange: [0.8, 0],
extrapolate: 'clamp',
});
const panReponder = PanResponder.create({
onMoveShouldSetPanResponder: (evt, gestureState) => {
if (itsAMatch) {
return false;
}
return true;
},
onPanResponderMove: (evt, gestureState) => {
position.setValue({ x: gestureState.dx, y: gestureState.dy });
},
onPanResponderRelease: (evt, gestureState) => {
if (gestureState.dx > 120) {
handleLike();
Animated.spring(position, {
toValue: { x: SCREEN_WIDTH + 100, y: gestureState.dy },
useNativeDriver: true,
}).start(() => {
position.setValue({ x: 0, y: 0 });
});
} else if (gestureState.dx < -120) {
handleDislike();
Animated.spring(position, {
toValue: { x: -SCREEN_WIDTH - 100, y: gestureState.dy },
useNativeDriver: true,
}).start();
} else {
Animated.spring(position, {
toValue: { x: 0, y: 0 },
useNativeDriver: true,
}).start();
}
},
});
return (
<Container>
<TouchableOpacity onPress={handleLogout}>
<Image source={logo} />
</TouchableOpacity>
<CardsContainer>
{users.map((user, index) => {
if (index === 0) {
return (
<AnimatedCard
key={user._id}
style={[
{
zIndex: users.length - index,
},
rotateAndTranslate,
]}
{...panReponder.panHandlers}
>
<AnimatedTag style={{ opacity: likeOpacity }} action="like">
<TagText action="like">LIKE</TagText>
</AnimatedTag>
<AnimatedTag
style={{ opacity: dislikeOpacity }}
action="dislike"
>
<TagText action="dislike">NOPE</TagText>
</AnimatedTag>
<Avatar source={{ uri: user.avatar }} />
<InfoWrapper>
<Name>{user.name}</Name>
<Bio>{user.bio}</Bio>
</InfoWrapper>
</AnimatedCard>
);
} else {
return (
<AnimatedCard
key={user._id}
style={{
zIndex: users.length - index,
opacity: index === 1 ? nextCardOpacity : 0,
transform: [{ scale: nextCardScale }],
}}
>
<Avatar source={{ uri: user.avatar }} />
<InfoWrapper>
<Name>{user.name}</Name>
<Bio>{user.bio}</Bio>
</InfoWrapper>
</AnimatedCard>
);
}
})}
</CardsContainer>
{users.length > 0 && (
<Footer>
<FooterButton onPress={handleDislike}>
<Image source={dislike} />
</FooterButton>
<FooterButton onPress={handleLike}>
<Image source={like} />
</FooterButton>
</Footer>
)}
{itsAMatch && (
<MatchContainer>
<MatchSymbol source={itsamatch} />
<MatchAvatar
source={{
uri: itsAMatch.avatar,
}}
/>
<MatchName>{itsAMatch.name}</MatchName>
<MatchBio>{itsAMatch.bio}</MatchBio>
<TouchableOpacity
onPress={() => setItsAMatch(null)}
hitSlop={{ top: 20, bottom: 20, left: 50, right: 50 }}
>
<MatchButtonText>Close</MatchButtonText>
</TouchableOpacity>
</MatchContainer>
)}
</Container>
);
}