@outerjoyn/react-native 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -0
- package/package.json +57 -0
- package/src/NativeSDK.ts +164 -0
- package/src/OuterJoynProvider.tsx +828 -0
- package/src/OuterJoynSDK.ts +261 -0
- package/src/OuterJoynSheet.tsx +218 -0
- package/src/PromotionsView.tsx +76 -0
- package/src/components/BalanceSummary.tsx +267 -0
- package/src/components/CheckoutPointsApply.tsx +787 -0
- package/src/components/LoyaltyBanner.tsx +281 -0
- package/src/components/LoyaltyProfileSection.tsx +273 -0
- package/src/components/PostCheckoutSavePrompt.tsx +369 -0
- package/src/components/index.ts +30 -0
- package/src/core/ErrorBoundary.tsx +51 -0
- package/src/core/analytics.ts +264 -0
- package/src/core/api-client.ts +253 -0
- package/src/core/colors.ts +20 -0
- package/src/core/event-stream.ts +229 -0
- package/src/core/i18n.ts +666 -0
- package/src/core/offline.ts +148 -0
- package/src/core/types.ts +214 -0
- package/src/core/version.ts +2 -0
- package/src/html.ts +154 -0
- package/src/index.ts +102 -0
- package/src/screens/AccountScreen.tsx +701 -0
- package/src/screens/ConnectScreen.tsx +1499 -0
- package/src/screens/ExchangeScreen.tsx +706 -0
- package/src/screens/RedeemScreen.tsx +683 -0
- package/src/screens/index.ts +21 -0
- package/src/types.ts +116 -0
- package/src/useOuterJoyn.ts +45 -0
- package/src/widgets.tsx +226 -0
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RedeemScreen -- Browse catalog and redeem points for rewards.
|
|
3
|
+
*
|
|
4
|
+
* State machine: catalog -> detail -> confirm -> processing -> success | error
|
|
5
|
+
*
|
|
6
|
+
* Ported from web RedeemWidget with native RN UI:
|
|
7
|
+
* 2-column FlatList catalog, search bar, product detail, confirm step,
|
|
8
|
+
* processing spinner, success with redemption code, error with retry.
|
|
9
|
+
*
|
|
10
|
+
* @module @outerjoyn/react-native/screens/RedeemScreen
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import React, { useState, useEffect, useCallback, useRef, useMemo } from 'react';
|
|
14
|
+
import {
|
|
15
|
+
View,
|
|
16
|
+
Text,
|
|
17
|
+
Modal,
|
|
18
|
+
Pressable,
|
|
19
|
+
TextInput,
|
|
20
|
+
ActivityIndicator,
|
|
21
|
+
FlatList,
|
|
22
|
+
Image,
|
|
23
|
+
StyleSheet,
|
|
24
|
+
Alert,
|
|
25
|
+
} from 'react-native';
|
|
26
|
+
import { useOuterJoyn } from '../useOuterJoyn';
|
|
27
|
+
import { APIClient } from '../core/api-client';
|
|
28
|
+
|
|
29
|
+
// =============================================================================
|
|
30
|
+
// Types
|
|
31
|
+
// =============================================================================
|
|
32
|
+
|
|
33
|
+
export interface RedeemScreenProps {
|
|
34
|
+
/** Controls modal visibility */
|
|
35
|
+
visible: boolean;
|
|
36
|
+
/** Called after a successful redemption */
|
|
37
|
+
onSuccess?: (result: RedemptionResult) => void;
|
|
38
|
+
/** Called when user dismisses the screen */
|
|
39
|
+
onClose?: () => void;
|
|
40
|
+
/** Called on error */
|
|
41
|
+
onError?: (error: Error) => void;
|
|
42
|
+
/** Override session token */
|
|
43
|
+
sessionToken?: string;
|
|
44
|
+
/** Override API base URL */
|
|
45
|
+
apiBaseUrl?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface RedemptionResult {
|
|
49
|
+
redemptionId: string;
|
|
50
|
+
productName: string;
|
|
51
|
+
pointsCost: number;
|
|
52
|
+
code?: string;
|
|
53
|
+
status: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface CatalogItem {
|
|
57
|
+
product_id: number;
|
|
58
|
+
name: string;
|
|
59
|
+
description: string;
|
|
60
|
+
short_description?: string;
|
|
61
|
+
category: string;
|
|
62
|
+
points_cost: number;
|
|
63
|
+
cash_price?: number;
|
|
64
|
+
image_url?: string;
|
|
65
|
+
thumbnail_url?: string;
|
|
66
|
+
inventory_count?: number;
|
|
67
|
+
terms_and_conditions?: string;
|
|
68
|
+
expiry_days?: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const OJ_LOGO_URL = 'https://app.outerjoyn.com/outerjoyn-logo.png';
|
|
72
|
+
|
|
73
|
+
type Step = 'catalog' | 'detail' | 'confirm' | 'processing' | 'success' | 'error';
|
|
74
|
+
|
|
75
|
+
// =============================================================================
|
|
76
|
+
// Demo data
|
|
77
|
+
// =============================================================================
|
|
78
|
+
|
|
79
|
+
/** Category badge colors for fallback display when no image is available */
|
|
80
|
+
const CATEGORY_COLORS: Record<string, string> = {
|
|
81
|
+
'Gift Cards': '#6366F1',
|
|
82
|
+
'Electronics': '#0EA5E9',
|
|
83
|
+
'Experiences': '#F59E0B',
|
|
84
|
+
'Merchandise': '#8B5CF6',
|
|
85
|
+
'Wellness': '#10B981',
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const DEMO_CATALOG: CatalogItem[] = [
|
|
89
|
+
{ product_id: 1, name: '$10 Coffee Gift Card', description: 'Redeemable at any Urban Coffee Co location.', category: 'Gift Cards', points_cost: 2000, cash_price: 1000, image_url: 'https://images.unsplash.com/photo-1509042239860-f550ce710b93?w=400&h=400&fit=crop' },
|
|
90
|
+
{ product_id: 2, name: 'Wireless Earbuds', description: 'Premium Bluetooth earbuds with ANC and 24h battery.', category: 'Electronics', points_cost: 15000, cash_price: 7999, image_url: 'https://images.unsplash.com/photo-1572569511254-d8f925fe2cbb?w=400&h=400&fit=crop' },
|
|
91
|
+
{ product_id: 3, name: 'Spa Day Voucher', description: 'Full-day spa experience for one.', category: 'Experiences', points_cost: 25000, cash_price: 12000, image_url: 'https://images.unsplash.com/photo-1540555700478-4be289fbecef?w=400&h=400&fit=crop' },
|
|
92
|
+
{ product_id: 4, name: 'Branded T-Shirt', description: 'Premium cotton tee, available in S-XL.', category: 'Merchandise', points_cost: 5000, cash_price: 2500, image_url: 'https://images.unsplash.com/photo-1521572163474-6864f9cf17ab?w=400&h=400&fit=crop' },
|
|
93
|
+
{ product_id: 5, name: '$25 Restaurant Card', description: 'Valid at participating restaurants nationwide.', category: 'Gift Cards', points_cost: 5000, cash_price: 2500, image_url: 'https://images.unsplash.com/photo-1414235077428-338989a2e8c0?w=400&h=400&fit=crop' },
|
|
94
|
+
{ product_id: 6, name: 'Free Shipping Pass', description: 'Unlimited free shipping for 30 days on all orders.', category: 'Wellness', points_cost: 8000, cash_price: 3500, image_url: 'https://images.unsplash.com/photo-1607082348824-0a96f2a4b9da?w=400&h=400&fit=crop' },
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
const DEMO_BALANCE = 30000;
|
|
98
|
+
|
|
99
|
+
// =============================================================================
|
|
100
|
+
// Component
|
|
101
|
+
// =============================================================================
|
|
102
|
+
|
|
103
|
+
/** Reward catalog screen with search, detail view, and redemption flow. */
|
|
104
|
+
export function RedeemScreen({
|
|
105
|
+
visible,
|
|
106
|
+
onSuccess,
|
|
107
|
+
onClose,
|
|
108
|
+
onError,
|
|
109
|
+
sessionToken: propToken,
|
|
110
|
+
apiBaseUrl,
|
|
111
|
+
}: RedeemScreenProps) {
|
|
112
|
+
const ctx = useOuterJoyn();
|
|
113
|
+
const { t } = ctx;
|
|
114
|
+
const token = propToken ?? ctx.sessionToken;
|
|
115
|
+
const isDemo = ctx.demoMode || !token;
|
|
116
|
+
|
|
117
|
+
// Use shared API client from context, fall back to creating one (fix #11)
|
|
118
|
+
const clientRef = useRef<APIClient | null>(null);
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (ctx.apiClient) {
|
|
121
|
+
clientRef.current = ctx.apiClient;
|
|
122
|
+
} else if (token) {
|
|
123
|
+
clientRef.current = new APIClient(token, apiBaseUrl ?? ctx.apiBaseUrl ?? 'https://api.outerjoyn.com');
|
|
124
|
+
}
|
|
125
|
+
}, [token, apiBaseUrl, ctx.apiClient, ctx.apiBaseUrl]);
|
|
126
|
+
|
|
127
|
+
// State
|
|
128
|
+
const [step, setStep] = useState<Step>('catalog');
|
|
129
|
+
const [catalog, setCatalog] = useState<CatalogItem[]>([]);
|
|
130
|
+
const [loading, setLoading] = useState(false);
|
|
131
|
+
const [search, setSearch] = useState('');
|
|
132
|
+
const [selectedItem, setSelectedItem] = useState<CatalogItem | null>(null);
|
|
133
|
+
const [result, setResult] = useState<RedemptionResult | null>(null);
|
|
134
|
+
const [error, setError] = useState<string | null>(null);
|
|
135
|
+
const [balance, setBalance] = useState(DEMO_BALANCE);
|
|
136
|
+
const searchTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
137
|
+
|
|
138
|
+
// Load catalog
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
if (!visible) return;
|
|
141
|
+
if (isDemo) {
|
|
142
|
+
setCatalog(DEMO_CATALOG);
|
|
143
|
+
setBalance(ctx.totalAvailablePoints || DEMO_BALANCE);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
// If apiClient not yet initialized, fall back to demo catalog
|
|
147
|
+
if (!clientRef.current) {
|
|
148
|
+
setCatalog(DEMO_CATALOG);
|
|
149
|
+
setBalance(ctx.totalAvailablePoints || DEMO_BALANCE);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
setLoading(true);
|
|
154
|
+
clientRef.current
|
|
155
|
+
.get<any>('/api/sdk/catalog')
|
|
156
|
+
.then((data) => {
|
|
157
|
+
// Handle various response shapes: { items: [...] }, { catalog: [...] }, { data: [...] }, or bare array
|
|
158
|
+
const rawItems = data?.items ?? data?.catalog ?? data?.data ?? (Array.isArray(data) ? data : []);
|
|
159
|
+
// Normalize snake_case/camelCase fields
|
|
160
|
+
const normalized: CatalogItem[] = rawItems.map((item: any) => ({
|
|
161
|
+
product_id: item.product_id ?? item.productId ?? 0,
|
|
162
|
+
name: item.name ?? 'Unknown',
|
|
163
|
+
description: item.description ?? item.short_description ?? '',
|
|
164
|
+
short_description: item.short_description ?? item.shortDescription,
|
|
165
|
+
category: item.category ?? 'Rewards',
|
|
166
|
+
points_cost: item.points_cost ?? item.pointsCost ?? item.points_value ?? item.pointsValue ?? 0,
|
|
167
|
+
cash_price: item.cash_price ?? item.cashPrice,
|
|
168
|
+
image_url: item.image_url ?? item.imageUrl ?? item.thumbnail_url ?? item.thumbnailUrl ?? undefined,
|
|
169
|
+
thumbnail_url: item.thumbnail_url ?? item.thumbnailUrl ?? item.image_url ?? item.imageUrl ?? undefined,
|
|
170
|
+
inventory_count: item.inventory_count ?? item.inventoryCount,
|
|
171
|
+
terms_and_conditions: item.terms_and_conditions ?? item.termsAndConditions,
|
|
172
|
+
expiry_days: item.expiry_days ?? item.expiryDays,
|
|
173
|
+
}));
|
|
174
|
+
setCatalog(normalized.length > 0 ? normalized : DEMO_CATALOG);
|
|
175
|
+
})
|
|
176
|
+
.catch(() => setCatalog(DEMO_CATALOG))
|
|
177
|
+
.finally(() => setLoading(false));
|
|
178
|
+
|
|
179
|
+
clientRef.current
|
|
180
|
+
.get<any>('/api/sdk/balance')
|
|
181
|
+
.then((data) => setBalance(data?.points_balance ?? data?.pointsBalance ?? data?.balance ?? DEMO_BALANCE))
|
|
182
|
+
.catch(() => {});
|
|
183
|
+
}, [visible, isDemo]);
|
|
184
|
+
|
|
185
|
+
// Reset on close
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
if (!visible) {
|
|
188
|
+
setStep('catalog');
|
|
189
|
+
setSearch('');
|
|
190
|
+
setSelectedItem(null);
|
|
191
|
+
setResult(null);
|
|
192
|
+
setError(null);
|
|
193
|
+
}
|
|
194
|
+
}, [visible]);
|
|
195
|
+
|
|
196
|
+
// Debounced search filter
|
|
197
|
+
const filteredCatalog = useMemo(() => {
|
|
198
|
+
if (!search.trim()) return catalog;
|
|
199
|
+
const q = search.toLowerCase();
|
|
200
|
+
return catalog.filter(
|
|
201
|
+
(item) =>
|
|
202
|
+
item.name.toLowerCase().includes(q) ||
|
|
203
|
+
item.category.toLowerCase().includes(q),
|
|
204
|
+
);
|
|
205
|
+
}, [catalog, search]);
|
|
206
|
+
|
|
207
|
+
const handleSearchChange = useCallback((text: string) => {
|
|
208
|
+
setSearch(text);
|
|
209
|
+
}, []);
|
|
210
|
+
|
|
211
|
+
const handleSelectItem = useCallback((item: CatalogItem) => {
|
|
212
|
+
setSelectedItem(item);
|
|
213
|
+
setStep('detail');
|
|
214
|
+
}, []);
|
|
215
|
+
|
|
216
|
+
const handleRedeem = useCallback(() => {
|
|
217
|
+
if (!selectedItem) return;
|
|
218
|
+
if (selectedItem.points_cost > balance) {
|
|
219
|
+
Alert.alert(t('redeem.insufficient'), t('redeem.points', { cost: String(selectedItem.points_cost) }));
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
setStep('confirm');
|
|
223
|
+
}, [selectedItem, balance]);
|
|
224
|
+
|
|
225
|
+
const handleConfirmRedeem = useCallback(async () => {
|
|
226
|
+
if (!selectedItem) return;
|
|
227
|
+
setStep('processing');
|
|
228
|
+
try {
|
|
229
|
+
if (isDemo) {
|
|
230
|
+
await new Promise((r) => setTimeout(r, 1500));
|
|
231
|
+
const res: RedemptionResult = {
|
|
232
|
+
redemptionId: `demo-${Date.now()}`,
|
|
233
|
+
productName: selectedItem.name,
|
|
234
|
+
pointsCost: selectedItem.points_cost,
|
|
235
|
+
code: 'DEMO-' + Math.random().toString(36).slice(2, 8).toUpperCase(),
|
|
236
|
+
status: 'fulfilled',
|
|
237
|
+
};
|
|
238
|
+
setResult(res);
|
|
239
|
+
setStep('success');
|
|
240
|
+
onSuccess?.(res);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Fix #8: add idempotency_key to mutation POST
|
|
245
|
+
if (!clientRef.current) {
|
|
246
|
+
throw new Error('API client not available');
|
|
247
|
+
}
|
|
248
|
+
const data = await clientRef.current.post<any>('/api/sdk/redeem', {
|
|
249
|
+
product_id: selectedItem.product_id,
|
|
250
|
+
idempotency_key: Date.now() + '_' + Math.random().toString(36).slice(2),
|
|
251
|
+
});
|
|
252
|
+
const res: RedemptionResult = {
|
|
253
|
+
redemptionId: data.redemption_id ?? data.id ?? '',
|
|
254
|
+
productName: selectedItem.name,
|
|
255
|
+
pointsCost: selectedItem.points_cost,
|
|
256
|
+
code: data.code ?? data.fulfillment?.code,
|
|
257
|
+
status: data.status ?? 'fulfilled',
|
|
258
|
+
};
|
|
259
|
+
setResult(res);
|
|
260
|
+
setStep('success');
|
|
261
|
+
onSuccess?.(res);
|
|
262
|
+
ctx.refreshBalances();
|
|
263
|
+
} catch (e: any) {
|
|
264
|
+
setError(e.message ?? 'Redemption failed');
|
|
265
|
+
setStep('error');
|
|
266
|
+
onError?.(e);
|
|
267
|
+
}
|
|
268
|
+
}, [selectedItem, isDemo, onSuccess, onError]);
|
|
269
|
+
|
|
270
|
+
const handleCopyCode = useCallback(() => {
|
|
271
|
+
if (!result?.code) return;
|
|
272
|
+
try {
|
|
273
|
+
// Try expo-clipboard if available, otherwise alert
|
|
274
|
+
const Clipboard = require('expo-clipboard');
|
|
275
|
+
Clipboard.setStringAsync(result.code);
|
|
276
|
+
Alert.alert('Copied', 'Redemption code copied to clipboard.');
|
|
277
|
+
} catch {
|
|
278
|
+
Alert.alert('Redemption Code', result.code);
|
|
279
|
+
}
|
|
280
|
+
}, [result]);
|
|
281
|
+
|
|
282
|
+
const fmt = (n: number) => n.toLocaleString();
|
|
283
|
+
|
|
284
|
+
// =========================================================================
|
|
285
|
+
// Render steps
|
|
286
|
+
// =========================================================================
|
|
287
|
+
|
|
288
|
+
const renderCatalogItem = ({ item }: { item: CatalogItem }) => (
|
|
289
|
+
<Pressable style={styles.catalogCard} onPress={() => handleSelectItem(item)}>
|
|
290
|
+
<View style={[styles.catalogImagePlaceholder, !item.image_url && { backgroundColor: CATEGORY_COLORS[item.category] ?? COLORS.accent }]}>
|
|
291
|
+
{item.image_url ? (
|
|
292
|
+
<Image source={{ uri: item.image_url }} style={styles.catalogImage} resizeMode="cover" />
|
|
293
|
+
) : (
|
|
294
|
+
<Text style={styles.catalogCategoryBadge}>{item.category}</Text>
|
|
295
|
+
)}
|
|
296
|
+
</View>
|
|
297
|
+
<Text style={styles.catalogName} numberOfLines={2}>{item.name}</Text>
|
|
298
|
+
<Text style={styles.catalogPoints}>{fmt(item.points_cost)} pts</Text>
|
|
299
|
+
{item.cash_price != null && (
|
|
300
|
+
<Text style={styles.catalogCash}>${(item.cash_price / 100).toFixed(2)}</Text>
|
|
301
|
+
)}
|
|
302
|
+
</Pressable>
|
|
303
|
+
);
|
|
304
|
+
|
|
305
|
+
const renderCatalog = () => (
|
|
306
|
+
<View style={styles.content}>
|
|
307
|
+
{/* Balance */}
|
|
308
|
+
<View style={styles.balanceBanner}>
|
|
309
|
+
<Text style={styles.balanceLabel}>{t('balance.total_value')}</Text>
|
|
310
|
+
<Text style={styles.balanceValue}>{fmt(balance)} pts</Text>
|
|
311
|
+
</View>
|
|
312
|
+
|
|
313
|
+
{/* Search */}
|
|
314
|
+
<View style={styles.searchContainer}>
|
|
315
|
+
<TextInput
|
|
316
|
+
style={styles.searchInput}
|
|
317
|
+
placeholder={t('redeem.search')}
|
|
318
|
+
placeholderTextColor={COLORS.muted}
|
|
319
|
+
value={search}
|
|
320
|
+
onChangeText={handleSearchChange}
|
|
321
|
+
returnKeyType="search"
|
|
322
|
+
/>
|
|
323
|
+
</View>
|
|
324
|
+
|
|
325
|
+
{loading ? (
|
|
326
|
+
<View style={styles.loadingContainer}>
|
|
327
|
+
<ActivityIndicator size="large" color={COLORS.primary} />
|
|
328
|
+
</View>
|
|
329
|
+
) : (
|
|
330
|
+
<FlatList
|
|
331
|
+
data={filteredCatalog}
|
|
332
|
+
renderItem={renderCatalogItem}
|
|
333
|
+
keyExtractor={(item) => String(item.product_id)}
|
|
334
|
+
numColumns={2}
|
|
335
|
+
columnWrapperStyle={styles.catalogRow}
|
|
336
|
+
contentContainerStyle={styles.catalogList}
|
|
337
|
+
showsVerticalScrollIndicator={false}
|
|
338
|
+
ListEmptyComponent={
|
|
339
|
+
<Text style={styles.emptyText}>No rewards found</Text>
|
|
340
|
+
}
|
|
341
|
+
/>
|
|
342
|
+
)}
|
|
343
|
+
</View>
|
|
344
|
+
);
|
|
345
|
+
|
|
346
|
+
const renderDetail = () => {
|
|
347
|
+
if (!selectedItem) return null;
|
|
348
|
+
const affordable = selectedItem.points_cost <= balance;
|
|
349
|
+
return (
|
|
350
|
+
<View style={styles.content}>
|
|
351
|
+
<Pressable onPress={() => setStep('catalog')} style={styles.backButton}>
|
|
352
|
+
<Text style={styles.backText}>{'\u2190'} Back</Text>
|
|
353
|
+
</Pressable>
|
|
354
|
+
|
|
355
|
+
<View style={[styles.detailImagePlaceholder, !selectedItem.image_url && { backgroundColor: CATEGORY_COLORS[selectedItem.category] ?? COLORS.accent }]}>
|
|
356
|
+
{selectedItem.image_url ? (
|
|
357
|
+
<Image source={{ uri: selectedItem.image_url }} style={styles.detailImage} resizeMode="cover" />
|
|
358
|
+
) : (
|
|
359
|
+
<Text style={styles.detailCategoryBadge}>{selectedItem.category}</Text>
|
|
360
|
+
)}
|
|
361
|
+
</View>
|
|
362
|
+
|
|
363
|
+
<Text style={styles.detailName}>{selectedItem.name}</Text>
|
|
364
|
+
<Text style={styles.detailCategory}>{selectedItem.category}</Text>
|
|
365
|
+
<Text style={styles.detailDescription}>{selectedItem.description}</Text>
|
|
366
|
+
|
|
367
|
+
<View style={styles.detailPriceRow}>
|
|
368
|
+
<Text style={styles.detailPoints}>{fmt(selectedItem.points_cost)} pts</Text>
|
|
369
|
+
{selectedItem.cash_price != null && (
|
|
370
|
+
<Text style={styles.detailCash}>${(selectedItem.cash_price / 100).toFixed(2)} value</Text>
|
|
371
|
+
)}
|
|
372
|
+
</View>
|
|
373
|
+
|
|
374
|
+
<Pressable
|
|
375
|
+
style={[styles.primaryButton, !affordable && styles.disabledButton]}
|
|
376
|
+
disabled={!affordable}
|
|
377
|
+
onPress={handleRedeem}
|
|
378
|
+
>
|
|
379
|
+
<Text style={styles.primaryButtonText}>
|
|
380
|
+
{affordable ? t('redeem.title') : t('redeem.insufficient')}
|
|
381
|
+
</Text>
|
|
382
|
+
</Pressable>
|
|
383
|
+
</View>
|
|
384
|
+
);
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
const renderConfirm = () => (
|
|
388
|
+
<View style={styles.centeredContent}>
|
|
389
|
+
<Text style={styles.stepTitle}>{t('redeem.confirm', { name: selectedItem?.name ?? '', cost: String(selectedItem?.points_cost ?? 0) })}</Text>
|
|
390
|
+
<View style={styles.confirmCard}>
|
|
391
|
+
<Text style={styles.confirmValue}>{selectedItem?.name}</Text>
|
|
392
|
+
<Text style={styles.confirmLabel}>{fmt(selectedItem?.points_cost ?? 0)} points</Text>
|
|
393
|
+
</View>
|
|
394
|
+
<Text style={styles.confirmBalance}>
|
|
395
|
+
Remaining balance: {fmt(balance - (selectedItem?.points_cost ?? 0))} pts
|
|
396
|
+
</Text>
|
|
397
|
+
<Pressable style={styles.primaryButton} onPress={handleConfirmRedeem}>
|
|
398
|
+
<Text style={styles.primaryButtonText}>{t('exchange.confirm')}</Text>
|
|
399
|
+
</Pressable>
|
|
400
|
+
<Pressable style={styles.secondaryButton} onPress={() => setStep('detail')}>
|
|
401
|
+
<Text style={styles.secondaryButtonText}>{t('common.cancel')}</Text>
|
|
402
|
+
</Pressable>
|
|
403
|
+
</View>
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
const renderProcessing = () => (
|
|
407
|
+
<View style={styles.centeredContent}>
|
|
408
|
+
<ActivityIndicator size="large" color={COLORS.primary} />
|
|
409
|
+
<Text style={styles.processingText}>{t('common.loading')}</Text>
|
|
410
|
+
</View>
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
const renderSuccess = () => (
|
|
414
|
+
<View style={styles.centeredContent}>
|
|
415
|
+
<View style={styles.checkCircle}>
|
|
416
|
+
<Text style={styles.checkMark}>{'\u2713'}</Text>
|
|
417
|
+
</View>
|
|
418
|
+
<Text style={styles.successTitle}>{t('redeem.success')}</Text>
|
|
419
|
+
<Text style={styles.successDetail}>{result?.productName}</Text>
|
|
420
|
+
|
|
421
|
+
{result?.code && (
|
|
422
|
+
<Pressable style={styles.codeBox} onPress={handleCopyCode}>
|
|
423
|
+
<Text style={styles.codeLabel}>Redemption Code</Text>
|
|
424
|
+
<Text style={styles.codeValue}>{result.code}</Text>
|
|
425
|
+
<Text style={styles.codeCopy}>{t('redeem.copy_code')}</Text>
|
|
426
|
+
</Pressable>
|
|
427
|
+
)}
|
|
428
|
+
|
|
429
|
+
<Pressable style={styles.primaryButton} onPress={() => { setStep('catalog'); setSelectedItem(null); }}>
|
|
430
|
+
<Text style={styles.primaryButtonText}>{t('redeem.browse_more')}</Text>
|
|
431
|
+
</Pressable>
|
|
432
|
+
<Pressable style={styles.secondaryButton} onPress={onClose}>
|
|
433
|
+
<Text style={styles.secondaryButtonText}>{t('common.done')}</Text>
|
|
434
|
+
</Pressable>
|
|
435
|
+
</View>
|
|
436
|
+
);
|
|
437
|
+
|
|
438
|
+
const renderError = () => (
|
|
439
|
+
<View style={styles.centeredContent}>
|
|
440
|
+
<View style={styles.errorCircle}>
|
|
441
|
+
<Text style={styles.errorMark}>!</Text>
|
|
442
|
+
</View>
|
|
443
|
+
<Text style={styles.errorTitle}>{t('common.error')}</Text>
|
|
444
|
+
<Text style={styles.errorDetail}>{error ?? t('common.error')}</Text>
|
|
445
|
+
<Pressable style={styles.primaryButton} onPress={() => setStep('confirm')}>
|
|
446
|
+
<Text style={styles.primaryButtonText}>{t('common.retry')}</Text>
|
|
447
|
+
</Pressable>
|
|
448
|
+
<Pressable style={styles.secondaryButton} onPress={onClose}>
|
|
449
|
+
<Text style={styles.secondaryButtonText}>{t('common.close')}</Text>
|
|
450
|
+
</Pressable>
|
|
451
|
+
</View>
|
|
452
|
+
);
|
|
453
|
+
|
|
454
|
+
const stepMap: Record<Step, () => React.ReactNode> = {
|
|
455
|
+
catalog: renderCatalog,
|
|
456
|
+
detail: renderDetail,
|
|
457
|
+
confirm: renderConfirm,
|
|
458
|
+
processing: renderProcessing,
|
|
459
|
+
success: renderSuccess,
|
|
460
|
+
error: renderError,
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
return (
|
|
464
|
+
<Modal visible={visible} animationType="slide" presentationStyle="pageSheet" onRequestClose={onClose}>
|
|
465
|
+
<View style={styles.container}>
|
|
466
|
+
<View style={styles.header}>
|
|
467
|
+
<Text style={styles.headerTitle}>{t('redeem.title')}</Text>
|
|
468
|
+
<Pressable onPress={onClose} hitSlop={12}>
|
|
469
|
+
<Text style={styles.closeButton}>{'\u2715'}</Text>
|
|
470
|
+
</Pressable>
|
|
471
|
+
</View>
|
|
472
|
+
|
|
473
|
+
{stepMap[step]()}
|
|
474
|
+
|
|
475
|
+
<View style={styles.poweredByRow}>
|
|
476
|
+
<Text style={styles.poweredByText}>Powered by</Text>
|
|
477
|
+
<Image
|
|
478
|
+
source={{ uri: OJ_LOGO_URL }}
|
|
479
|
+
style={styles.poweredByLogo}
|
|
480
|
+
resizeMode="contain"
|
|
481
|
+
/>
|
|
482
|
+
</View>
|
|
483
|
+
</View>
|
|
484
|
+
</Modal>
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// =============================================================================
|
|
489
|
+
// Colors
|
|
490
|
+
// =============================================================================
|
|
491
|
+
|
|
492
|
+
const COLORS = {
|
|
493
|
+
primary: '#0c90eb',
|
|
494
|
+
navy: '#0D1B4C',
|
|
495
|
+
accent: '#3B82F6',
|
|
496
|
+
bg: '#FFFFFF',
|
|
497
|
+
surface: '#F8FAFC',
|
|
498
|
+
border: '#E2E8F0',
|
|
499
|
+
text: '#0F172A',
|
|
500
|
+
muted: '#94A3B8',
|
|
501
|
+
success: '#10B981',
|
|
502
|
+
error: '#EF4444',
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
// =============================================================================
|
|
506
|
+
// Styles
|
|
507
|
+
// =============================================================================
|
|
508
|
+
|
|
509
|
+
const styles = StyleSheet.create({
|
|
510
|
+
container: { flex: 1, backgroundColor: COLORS.bg },
|
|
511
|
+
header: {
|
|
512
|
+
flexDirection: 'row',
|
|
513
|
+
justifyContent: 'space-between',
|
|
514
|
+
alignItems: 'center',
|
|
515
|
+
paddingHorizontal: 20,
|
|
516
|
+
paddingVertical: 16,
|
|
517
|
+
borderBottomWidth: 1,
|
|
518
|
+
borderBottomColor: COLORS.border,
|
|
519
|
+
backgroundColor: COLORS.navy,
|
|
520
|
+
},
|
|
521
|
+
headerTitle: { fontSize: 18, fontWeight: '700', color: '#FFFFFF' },
|
|
522
|
+
closeButton: { fontSize: 20, color: '#FFFFFF', fontWeight: '600' },
|
|
523
|
+
content: { flex: 1, padding: 16 },
|
|
524
|
+
centeredContent: { flex: 1, justifyContent: 'center', alignItems: 'center', padding: 20 },
|
|
525
|
+
poweredByRow: {
|
|
526
|
+
flexDirection: 'row',
|
|
527
|
+
alignItems: 'center',
|
|
528
|
+
justifyContent: 'center',
|
|
529
|
+
gap: 6,
|
|
530
|
+
paddingVertical: 16,
|
|
531
|
+
opacity: 0.5,
|
|
532
|
+
borderTopWidth: 1,
|
|
533
|
+
borderTopColor: COLORS.border,
|
|
534
|
+
},
|
|
535
|
+
poweredByText: { fontSize: 11, color: '#9CA3AF' },
|
|
536
|
+
poweredByLogo: { width: 80, height: 16 },
|
|
537
|
+
|
|
538
|
+
// Balance banner
|
|
539
|
+
balanceBanner: {
|
|
540
|
+
flexDirection: 'row',
|
|
541
|
+
justifyContent: 'space-between',
|
|
542
|
+
alignItems: 'center',
|
|
543
|
+
padding: 14,
|
|
544
|
+
borderRadius: 12,
|
|
545
|
+
backgroundColor: COLORS.primary + '0D',
|
|
546
|
+
marginBottom: 12,
|
|
547
|
+
},
|
|
548
|
+
balanceLabel: { fontSize: 14, color: COLORS.text, fontWeight: '500' },
|
|
549
|
+
balanceValue: { fontSize: 18, fontWeight: '700', color: COLORS.primary },
|
|
550
|
+
|
|
551
|
+
// Search
|
|
552
|
+
searchContainer: { marginBottom: 12 },
|
|
553
|
+
searchInput: {
|
|
554
|
+
borderWidth: 1,
|
|
555
|
+
borderColor: COLORS.border,
|
|
556
|
+
borderRadius: 10,
|
|
557
|
+
padding: 12,
|
|
558
|
+
fontSize: 15,
|
|
559
|
+
color: COLORS.text,
|
|
560
|
+
backgroundColor: COLORS.surface,
|
|
561
|
+
},
|
|
562
|
+
|
|
563
|
+
// Catalog grid
|
|
564
|
+
catalogList: { paddingBottom: 16 },
|
|
565
|
+
catalogRow: { justifyContent: 'space-between' },
|
|
566
|
+
catalogCard: {
|
|
567
|
+
width: '48%',
|
|
568
|
+
borderWidth: 1,
|
|
569
|
+
borderColor: COLORS.border,
|
|
570
|
+
borderRadius: 12,
|
|
571
|
+
backgroundColor: COLORS.bg,
|
|
572
|
+
marginBottom: 12,
|
|
573
|
+
overflow: 'hidden',
|
|
574
|
+
},
|
|
575
|
+
catalogImagePlaceholder: {
|
|
576
|
+
height: 100,
|
|
577
|
+
backgroundColor: COLORS.surface,
|
|
578
|
+
justifyContent: 'center',
|
|
579
|
+
alignItems: 'center',
|
|
580
|
+
},
|
|
581
|
+
catalogImage: { width: '100%', height: '100%' },
|
|
582
|
+
catalogCategoryBadge: { fontSize: 11, fontWeight: '700', color: '#FFFFFF', textTransform: 'uppercase', letterSpacing: 0.5 },
|
|
583
|
+
catalogName: { fontSize: 14, fontWeight: '600', color: COLORS.text, paddingHorizontal: 10, paddingTop: 8 },
|
|
584
|
+
catalogPoints: { fontSize: 14, fontWeight: '700', color: COLORS.primary, paddingHorizontal: 10, paddingTop: 4 },
|
|
585
|
+
catalogCash: { fontSize: 12, color: COLORS.muted, paddingHorizontal: 10, paddingBottom: 10 },
|
|
586
|
+
loadingContainer: { flex: 1, justifyContent: 'center', alignItems: 'center' },
|
|
587
|
+
emptyText: { textAlign: 'center', color: COLORS.muted, marginTop: 40, fontSize: 15 },
|
|
588
|
+
|
|
589
|
+
// Detail
|
|
590
|
+
backButton: { marginBottom: 12 },
|
|
591
|
+
backText: { fontSize: 15, color: COLORS.primary, fontWeight: '600' },
|
|
592
|
+
detailImagePlaceholder: {
|
|
593
|
+
height: 180,
|
|
594
|
+
borderRadius: 12,
|
|
595
|
+
backgroundColor: COLORS.surface,
|
|
596
|
+
justifyContent: 'center',
|
|
597
|
+
alignItems: 'center',
|
|
598
|
+
marginBottom: 16,
|
|
599
|
+
},
|
|
600
|
+
detailImage: { width: '100%', height: '100%', borderRadius: 12 },
|
|
601
|
+
detailCategoryBadge: { fontSize: 16, fontWeight: '700', color: '#FFFFFF', textTransform: 'uppercase', letterSpacing: 1 },
|
|
602
|
+
detailName: { fontSize: 22, fontWeight: '700', color: COLORS.text, marginBottom: 4 },
|
|
603
|
+
detailCategory: { fontSize: 13, color: COLORS.muted, textTransform: 'uppercase', marginBottom: 12 },
|
|
604
|
+
detailDescription: { fontSize: 15, color: COLORS.text, lineHeight: 22, marginBottom: 16 },
|
|
605
|
+
detailPriceRow: { flexDirection: 'row', alignItems: 'baseline', marginBottom: 8 },
|
|
606
|
+
detailPoints: { fontSize: 20, fontWeight: '700', color: COLORS.primary, marginRight: 10 },
|
|
607
|
+
detailCash: { fontSize: 14, color: COLORS.muted },
|
|
608
|
+
|
|
609
|
+
// Buttons
|
|
610
|
+
primaryButton: {
|
|
611
|
+
marginTop: 24,
|
|
612
|
+
paddingVertical: 16,
|
|
613
|
+
borderRadius: 14,
|
|
614
|
+
backgroundColor: COLORS.primary,
|
|
615
|
+
alignItems: 'center',
|
|
616
|
+
width: '100%',
|
|
617
|
+
},
|
|
618
|
+
primaryButtonText: { fontSize: 16, fontWeight: '700', color: '#FFFFFF' },
|
|
619
|
+
disabledButton: { opacity: 0.4 },
|
|
620
|
+
secondaryButton: { marginTop: 12, paddingVertical: 14, alignItems: 'center' },
|
|
621
|
+
secondaryButtonText: { fontSize: 15, fontWeight: '600', color: COLORS.muted },
|
|
622
|
+
|
|
623
|
+
// Confirm
|
|
624
|
+
stepTitle: { fontSize: 20, fontWeight: '700', color: COLORS.text, marginBottom: 20 },
|
|
625
|
+
confirmCard: {
|
|
626
|
+
padding: 20,
|
|
627
|
+
borderRadius: 12,
|
|
628
|
+
backgroundColor: COLORS.surface,
|
|
629
|
+
borderWidth: 1,
|
|
630
|
+
borderColor: COLORS.border,
|
|
631
|
+
alignItems: 'center',
|
|
632
|
+
width: '100%',
|
|
633
|
+
},
|
|
634
|
+
confirmValue: { fontSize: 18, fontWeight: '700', color: COLORS.text, marginBottom: 4 },
|
|
635
|
+
confirmLabel: { fontSize: 15, color: COLORS.primary, fontWeight: '600' },
|
|
636
|
+
confirmBalance: { fontSize: 14, color: COLORS.muted, marginTop: 12 },
|
|
637
|
+
|
|
638
|
+
// Processing
|
|
639
|
+
processingText: { fontSize: 16, color: COLORS.muted, marginTop: 16 },
|
|
640
|
+
|
|
641
|
+
// Success
|
|
642
|
+
checkCircle: {
|
|
643
|
+
width: 64,
|
|
644
|
+
height: 64,
|
|
645
|
+
borderRadius: 32,
|
|
646
|
+
backgroundColor: COLORS.success + '1A',
|
|
647
|
+
justifyContent: 'center',
|
|
648
|
+
alignItems: 'center',
|
|
649
|
+
marginBottom: 16,
|
|
650
|
+
},
|
|
651
|
+
checkMark: { fontSize: 32, color: COLORS.success, fontWeight: '700' },
|
|
652
|
+
successTitle: { fontSize: 22, fontWeight: '700', color: COLORS.text, marginBottom: 8 },
|
|
653
|
+
successDetail: { fontSize: 15, color: COLORS.muted, marginBottom: 16 },
|
|
654
|
+
|
|
655
|
+
// Code box
|
|
656
|
+
codeBox: {
|
|
657
|
+
padding: 16,
|
|
658
|
+
borderRadius: 12,
|
|
659
|
+
backgroundColor: COLORS.surface,
|
|
660
|
+
borderWidth: 1,
|
|
661
|
+
borderColor: COLORS.border,
|
|
662
|
+
alignItems: 'center',
|
|
663
|
+
width: '100%',
|
|
664
|
+
marginBottom: 8,
|
|
665
|
+
},
|
|
666
|
+
codeLabel: { fontSize: 12, color: COLORS.muted, marginBottom: 4 },
|
|
667
|
+
codeValue: { fontSize: 20, fontWeight: '700', color: COLORS.text, letterSpacing: 1 },
|
|
668
|
+
codeCopy: { fontSize: 12, color: COLORS.primary, marginTop: 6 },
|
|
669
|
+
|
|
670
|
+
// Error
|
|
671
|
+
errorCircle: {
|
|
672
|
+
width: 64,
|
|
673
|
+
height: 64,
|
|
674
|
+
borderRadius: 32,
|
|
675
|
+
backgroundColor: COLORS.error + '1A',
|
|
676
|
+
justifyContent: 'center',
|
|
677
|
+
alignItems: 'center',
|
|
678
|
+
marginBottom: 16,
|
|
679
|
+
},
|
|
680
|
+
errorMark: { fontSize: 32, color: COLORS.error, fontWeight: '700' },
|
|
681
|
+
errorTitle: { fontSize: 22, fontWeight: '700', color: COLORS.text, marginBottom: 8 },
|
|
682
|
+
errorDetail: { fontSize: 15, color: COLORS.muted, textAlign: 'center', marginBottom: 8 },
|
|
683
|
+
});
|