-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrainerScreen.tsx
More file actions
164 lines (150 loc) · 3.98 KB
/
TrainerScreen.tsx
File metadata and controls
164 lines (150 loc) · 3.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
import React, { useState, useEffect, useCallback } from 'react';
import {
View,
Image,
ScrollView,
TouchableOpacity,
Text,
FlatList,
StyleSheet,
} from 'react-native';
import { useTranslation } from 'react-i18next';
import { useUser } from './UserContext';
import Ionicons from 'react-native-vector-icons/Ionicons';
import { authFetch } from './src/utils/api';
const TrainerScreen = ({navigation}) => {
const { t } = useTranslation();
const [trainers, setTrainers] = useState([]);
const [loading, setLoading] = useState(true);
const userContext = useUser();
const user = userContext?.user;
useEffect(() => {
const fetchTrainers = async () => {
try {
const response = await authFetch(`/trainers`, {
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error('Failed to fetch trainers data');
}
const data = await response.json();
setTrainers(data.trainer_list);
} catch (error) {
console.error('Error fetching trainers:', error);
} finally {
setLoading(false);
}
};
fetchTrainers();
}, []);
if (loading) {
return (
<View style={styles.screenContainer}>
<Text>Loading trainers...</Text>
</View>
);
}
const renderTrainerItem = ({item}) => {
const handlePress = () => {
navigation.navigate('TrainerDetail', { id: item.id });
};
const isUserTrainer = user?.trainer?.id === item.id;
return (
<TouchableOpacity onPress={handlePress} style={[styles.trainerItem,isUserTrainer && styles.highlightedTrainerItem]}>
<View style={styles.trainerImageContainer}>
{item.picture && item.picture.picture_url ? (
<Image
source={{uri: `https://humake.blob.core.windows.net/humake/employee/${item.branch_id}/${item.picture.picture_url}`}}
style={styles.trainerImage}
resizeMode="cover"
/>
) : (
<Image
source={require('./assets/photo_none.gif')}
style={styles.trainerImage}
resizeMode="cover"
/>
)}
</View>
<View style={styles.trainerInfo}>
<Text style={[styles.trainerName,isUserTrainer && { color : '#ff8d1d', fontWeight: 'bold'}]}>{item.name}</Text>
</View>
{isUserTrainer && (
<Ionicons
name="checkmark-circle"
size={24}
color="green"
style={styles.checkIcon}
/>
)}
</TouchableOpacity>
);
};
return (
<View style={{ flex: 1 }}>
<FlatList
data={trainers}
renderItem={renderTrainerItem}
keyExtractor={(item) => item.id.toString()}
contentContainerStyle={styles.trainerList}
style={{ flexGrow: 1 }}
/>
</View>
);
};
const styles = StyleSheet.create({
screenContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 0,
},
trainerList: {
padding: 8,
},
trainerItem: {
flexDirection: 'row',
padding: 12,
borderBottomWidth: 1,
borderBottomColor: '#ccc',
marginBottom: 8,
position: 'relative', // Add position relative to make space for icon
},
trainerImageContainer: {
width: 60,
height: 60,
borderRadius: 30,
marginRight: 12,
overflow: 'hidden',
},
trainerImage: {
width: '100%',
height: '100%',
},
trainerInfo: {
flex: 1,
justifyContent: 'center',
marginRight: 8, // Add margin to make space for icon
},
trainerName: {
fontSize: 16,
color : '#333'
},
trainerCreatedAt: {
fontSize: 12,
color: '#666',
},
highlightedTrainerItem: {
backgroundColor: '#fff',
borderRadius : 5
},
checkIcon: {
position: 'absolute',
right: 10,
top: '50%',
transform: [{ translateY: -12 }],
},
});
export default TrainerScreen;