forked from TrustUp-app/TrustUp-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerchantsScreen.tsx
More file actions
212 lines (193 loc) · 7.51 KB
/
Copy pathMerchantsScreen.tsx
File metadata and controls
212 lines (193 loc) · 7.51 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
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
import React from 'react';
import {
View,
Text,
TextInput,
TouchableOpacity,
ScrollView,
ActivityIndicator,
Image,
NativeSyntheticEvent,
NativeScrollEvent,
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { Ionicons } from '@expo/vector-icons';
import { useMerchants } from '../../hooks/merchants/use-merchants';
import type { MerchantSummary } from '../../types/api';
const colors = require('../../theme/colors.json');
// ─── Sub-components ─────────────────────────────────────────────────────────
const MerchantCard = ({
merchant,
onPress,
}: {
merchant: MerchantSummary;
onPress: (merchant: MerchantSummary) => void;
}) => (
<TouchableOpacity
activeOpacity={0.7}
onPress={() => onPress(merchant)}
className="mb-3 flex-row items-center gap-3 rounded-2xl bg-white p-4 shadow-sm"
accessibilityRole="button"
accessibilityLabel={`${merchant.name}, ${merchant.category}`}>
<View className="h-12 w-12 items-center justify-center overflow-hidden rounded-xl bg-primarySoft">
{merchant.logo ? (
<Image source={{ uri: merchant.logo }} className="h-full w-full" resizeMode="cover" />
) : (
<Ionicons name="storefront-outline" size={22} color={colors.primary} />
)}
</View>
<View className="flex-1">
<Text className="text-base font-semibold text-text" numberOfLines={1}>
{merchant.name}
</Text>
<Text className="text-xs text-textMuted">{merchant.category}</Text>
</View>
{merchant.isActive && (
<View className="rounded-full bg-successSoft px-3 py-1">
<Text className="text-xs font-semibold text-successDeep">Active</Text>
</View>
)}
<Ionicons name="chevron-forward" size={18} color={colors.textSubtle} />
</TouchableOpacity>
);
const MerchantCardSkeleton = () => (
<View className="mb-3 flex-row items-center gap-3 rounded-2xl bg-white p-4 shadow-sm">
<View className="h-12 w-12 rounded-xl bg-gray-100" />
<View className="flex-1 gap-2">
<View className="h-3.5 w-2/3 rounded-full bg-gray-100" />
<View className="h-3 w-1/3 rounded-full bg-gray-100" />
</View>
</View>
);
const EmptyState = ({
icon,
title,
description,
}: {
icon: keyof typeof Ionicons.glyphMap;
title: string;
description: string;
}) => (
<View className="flex-1 items-center justify-center px-8 pt-24">
<View className="mb-4 h-16 w-16 items-center justify-center rounded-full bg-gray-100">
<Ionicons name={icon} size={32} color={colors.textSubtle} />
</View>
<Text className="mb-1 text-base font-semibold text-text">{title}</Text>
<Text className="text-center text-sm leading-5 text-textSecondary">{description}</Text>
</View>
);
// ─── Main Screen ──────────────────────────────────────────────────────────────
interface MerchantsScreenProps {
onBack: () => void;
onMerchantPress: (merchant: MerchantSummary) => void;
}
const MerchantsScreen: React.FC<MerchantsScreenProps> = ({ onBack, onMerchantPress }) => {
const insets = useSafeAreaInsets();
const { merchants, isLoading, error, hasMore, query, setQuery, loadMore, refresh } =
useMerchants();
const isInitialLoad = isLoading && merchants.length === 0;
const showEmpty = !isLoading && !error && merchants.length === 0;
const handleScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
const { layoutMeasurement, contentOffset, contentSize } = event.nativeEvent;
const distanceFromBottom = contentSize.height - layoutMeasurement.height - contentOffset.y;
// Load more when user scrolls within 100px of the bottom
if (distanceFromBottom < 100 && hasMore && !isLoading) {
loadMore();
}
};
return (
<View style={{ flex: 1, backgroundColor: colors.background }}>
{/* Header */}
<View
style={{
backgroundColor: colors.white,
paddingHorizontal: 16,
paddingTop: insets.top + 16,
paddingBottom: 16,
}}>
<View className="mb-4 flex-row items-center gap-3">
<TouchableOpacity
onPress={onBack}
activeOpacity={0.7}
accessibilityLabel="Go back"
accessibilityRole="button"
className="h-10 w-10 items-center justify-center rounded-full border border-border bg-white">
<Ionicons name="chevron-back" size={20} color={colors.text} />
</TouchableOpacity>
<Text className="text-xl font-bold text-text">Merchants</Text>
</View>
{/* Search bar */}
<View className="flex-row items-center gap-2 rounded-xl border border-border bg-background px-3 py-2.5">
<Ionicons name="search" size={18} color={colors.textSubtle} />
<TextInput
value={query}
onChangeText={setQuery}
placeholder="Search merchants"
placeholderTextColor={colors.placeholder}
className="flex-1 text-sm text-text"
accessibilityLabel="Search merchants by name"
autoCapitalize="none"
autoCorrect={false}
/>
{query.length > 0 && (
<TouchableOpacity
onPress={() => setQuery('')}
accessibilityLabel="Clear search"
accessibilityRole="button">
<Ionicons name="close-circle" size={18} color={colors.textSubtle} />
</TouchableOpacity>
)}
</View>
</View>
<ScrollView
className="flex-1"
contentContainerStyle={{ padding: 16, paddingBottom: 40 }}
showsVerticalScrollIndicator={false}
onScroll={handleScroll}
scrollEventThrottle={400}>
{/* Error banner */}
{error && (
<View className="mb-3 flex-row items-center gap-2 rounded-xl bg-errorSoft px-4 py-3">
<Ionicons name="alert-circle" size={18} color={colors.error} />
<Text className="flex-1 text-sm text-error">{error}</Text>
<TouchableOpacity onPress={refresh}>
<Text className="text-sm font-semibold text-error">Retry</Text>
</TouchableOpacity>
</View>
)}
{/* Loading skeleton (initial load) */}
{isInitialLoad && Array.from({ length: 6 }).map((_, i) => <MerchantCardSkeleton key={i} />)}
{/* Merchant list */}
{!isInitialLoad &&
merchants.map((merchant) => (
<MerchantCard key={merchant.id} merchant={merchant} onPress={onMerchantPress} />
))}
{/* Load-more spinner */}
{isLoading && !isInitialLoad && (
<View className="items-center py-6">
<ActivityIndicator size="small" color={colors.primary} />
</View>
)}
{/* Empty states */}
{showEmpty && query.trim() ? (
<EmptyState
icon="search-outline"
title="No merchants found"
description={`No merchants match "${query.trim()}". Try a different search.`}
/>
) : (
showEmpty && (
<EmptyState
icon="storefront-outline"
title="Merchants coming soon"
description={
"We're onboarding merchants that accept Buy Now, Pay Later.\nCheck back shortly."
}
/>
)
)}
</ScrollView>
</View>
);
};
export default MerchantsScreen;