-
Notifications
You must be signed in to change notification settings - Fork 0
/
DashboardScreen.js
200 lines (191 loc) · 6.2 KB
/
DashboardScreen.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
import React, { useState, useEffect } from 'react';
import { View, Text, FlatList, StyleSheet, Image, TouchableOpacity, Alert, SafeAreaView} from 'react-native';
import axiosInstance from '../api/axiosInstance'; // Adjust the import based on your file structure
const DashboardScreen = () => {
const [activeTab, setActiveTab] = useState('feed');
const [leaderboardData, setLeaderboardData] = useState([]);
const [recentActivities, setRecentActivities] = useState([]); // State to manage recent activities
const [loading, setLoading] = useState(true); // State to manage loading
// Fetch recent activities and leaderboard data from the API when the component mounts
useEffect(() => {
const fetchRecentActivities = async () => {
try {
const response = await axiosInstance.get('/recent-activities');
// Map the response to the format required for rendering
const formattedActivities = response.data.map((item) => ({
id: item.activityId.toString(), // Ensure id is a string for keyExtractor
name: item.username,
activity: item.actvityName, // Make sure this corresponds to the API response
date: item.completedAt, // Format the date if necessary
profilePic: item.profileImage,
}));
setRecentActivities(formattedActivities);
} catch (error) {
console.error('Error fetching recent activities:', error);
Alert.alert('Error', 'Could not fetch recent activities.');
}
};
const fetchLeaderboard = async () => {
try {
const response = await axiosInstance.get('/leaderboard');
// Map the response to the format required for rendering
const formattedData = response.data.map((item) => ({
id: item.user_id.toString(), // Ensure id is a string for keyExtractor
name: item.full_name, // Use full name from response
points: item.total_points, // Use total points from response
}));
setLeaderboardData(formattedData);
} catch (error) {
console.error('Error fetching leaderboard:', error);
Alert.alert('Error', 'Could not fetch leaderboard data.');
} finally {
setLoading(false); // Set loading to false regardless of success or error
}
};
fetchRecentActivities();
fetchLeaderboard();
}, []);
const renderFeedItem = ({ item }) => (
<View style={styles.activityCard}>
<Image source={{ uri: item.profilePic }} style={styles.profilePic} />
<View style={styles.activityInfo}>
<Text style={styles.friendName}>{item.name}</Text>
<Text style={styles.activityText}>{item.activity} ✔️</Text>
<Text style={styles.activityDate}>{item.date}</Text>
</View>
</View>
);
const renderLeaderboardItem = ({ item }) => (
<View style={styles.leaderboardCard}>
<Text style={styles.leaderboardName}>{item.name}</Text>
<Text style={styles.leaderboardPoints}>{item.points} points</Text>
</View>
);
return (
<SafeAreaView style={styles.container}>
<View style={styles.container}>
<Text style={styles.header}>🎯 Dashboard</Text>
<View style={styles.tabContainer}>
<TouchableOpacity onPress={() => setActiveTab('feed')} style={[styles.tab, activeTab === 'feed' && styles.activeTab]}>
<Text style={styles.tabText}>Feed ✅</Text>
</TouchableOpacity>
<TouchableOpacity onPress={() => setActiveTab('leaderboard')} style={[styles.tab, activeTab === 'leaderboard' && styles.activeTab]}>
<Text style={styles.tabText}>Leaderboard 🔥</Text>
</TouchableOpacity>
</View>
{activeTab === 'feed' ? (
<FlatList
data={recentActivities}
renderItem={renderFeedItem}
keyExtractor={(item) => item.id}
showsVerticalScrollIndicator={false}
/>
) : loading ? (
<Text style={styles.loadingText}>Loading...</Text>
) : (
<FlatList
data={leaderboardData}
renderItem={renderLeaderboardItem}
keyExtractor={(item) => item.id}
showsVerticalScrollIndicator={false}
/>
)}
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
backgroundColor: '#121212', // Dark background
},
header: {
fontSize: 26,
fontWeight: 'bold',
color: '#ffffff', // Light text color
textAlign: 'center',
padding: 20,
backgroundColor: '#121212', // Header background
marginBottom: 10,
},
tabContainer: {
flexDirection: 'row',
justifyContent: 'space-around',
marginBottom: 16,
},
tab: {
padding: 10,
borderBottomWidth: 2,
borderBottomColor: 'transparent',
},
activeTab: {
borderBottomColor: '#32CD32', // Green accent for active tab
},
tabText: {
fontSize: 18,
fontWeight: 'bold',
color: '#EAEAEA', // Light text for tabs
},
activityCard: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: '#2E2E2E', // Darker card background
padding: 16,
marginBottom: 12,
borderRadius: 8,
shadowColor: '#000',
shadowOpacity: 0.1,
shadowRadius: 5,
elevation: 4,
},
profilePic: {
width: 50,
height: 50,
borderRadius: 25,
marginRight: 12,
},
activityInfo: {
flex: 1,
},
friendName: {
fontSize: 18,
fontWeight: 'bold',
color: '#32CD32', // Green for friend names
},
activityText: {
fontSize: 14,
color: '#EAEAEA', // Light text for activity
marginVertical: 4,
},
activityDate: {
fontSize: 12,
color: '#888', // Dimmed date color for contrast
},
leaderboardCard: {
padding: 16,
marginBottom: 12,
backgroundColor: '#2E2E2E', // Darker card background for leaderboard
borderRadius: 8,
shadowColor: '#000',
shadowOpacity: 0.1,
shadowRadius: 5,
elevation: 4,
},
leaderboardName: {
fontSize: 18,
fontWeight: 'bold',
color: '#32CD32', // Green for leaderboard names
},
leaderboardPoints: {
fontSize: 14,
color: '#EAEAEA', // Light text for leaderboard points
},
loadingText: {
fontSize: 18,
textAlign: 'center',
marginTop: 20,
color: '#EAEAEA', // Light text for loading
},
});
export default DashboardScreen;