Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
95 lines (89 loc) · 2.23 KB
/
Copy pathApp.js
File metadata and controls
95 lines (89 loc) · 2.23 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
import React, { useState, useEffect, useRef } from "react";
import { StyleSheet, View, Alert } from "react-native";
import Constants from "expo-constants";
import TopBar from "./components/TopBar";
import axios from "axios";
import SwipeableImages from "./components/SwipeableImages";
import BottomBar from "./components/BottomBar";
import Swipes from "./components/Swipes";
export default function App() {
const [users, setUsers] = useState([]);
const [currentIndex, setCurrentIndex] = useState(0);
const swipRef = useRef(null);
async function fetchUsers() {
try {
const { data } = await axios.get(
"https://randomuser.me/api/?gender=female&results=100"
);
setUsers(data.results);
} catch (error) {
console.log(error);
Alert.alert("Error getting users", "", [
{ text: "Retry", onPress: () => fetchUsers() },
]);
}
}
useEffect(() => {
fetchUsers();
}, []);
function handleLike() {
nextUser();
}
function handlePass() {
nextUser();
}
function nextUser() {
const nextIndex = users.length - 2 === currentIndex ? 0 : currentIndex + 1;
setCurrentIndex(nextIndex);
}
function handleLikePress() {
swipRef.current.openLeft();
}
function handlePassPress() {
swipRef.current.openRight();
}
return (
<View style={styles.container}>
<TopBar />
<View style={styles.swipes}>
{users.length > 1 &&
users.map(
(u, i) =>
currentIndex === i && (
<Swipes
key={i}
ref={swipRef}
users={users}
currentIndex={currentIndex}
handleLike={handleLike}
handlePass={handlePass}
/>
)
)}
</View>
<BottomBar
handleLikePress={handleLikePress}
handlePassPress={handlePassPress}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: Constants.statusBarHeight,
},
swipes: {
flex: 1,
padding: 10,
paddingTop: 8,
shadowColor: "#000",
shadowOffset: {
width: 0,
height: 3,
},
shadowOpacity: 0.29,
shadowRadius: 4.65,
elevation: 7,
},
});