forked from TrustUp-app/TrustUp-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoanDetailScreen.tsx
More file actions
421 lines (381 loc) · 15.7 KB
/
Copy pathLoanDetailScreen.tsx
File metadata and controls
421 lines (381 loc) · 15.7 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import React from 'react';
import {
View,
Text,
TouchableOpacity,
ScrollView,
ActivityIndicator,
StyleSheet,
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { Ionicons } from '@expo/vector-icons';
import {
formatLoanAmount,
getRepaymentProgress,
hasOverdueInstallments,
} from '../../hooks/loans/use-loans';
import { useLoanRepayment } from '../../hooks/loans/use-loan-repayment';
import type { Loan, LoanInstallment, LoanStatus } from '../../types/Loan';
const colors = require('../../theme/colors.json');
// ─── Helpers ──────────────────────────────────────────────────────────────────
const STATUS_BADGE: Record<LoanStatus, { bg: string; fg: string; label: string }> = {
pending: { bg: colors.amberSoft, fg: colors.amber, label: 'Pending' },
active: { bg: colors.successSoft, fg: colors.successDeep, label: 'Active' },
completed: { bg: colors.primarySoft, fg: colors.primary, label: 'Completed' },
defaulted: { bg: colors.errorSoft, fg: colors.error, label: 'Defaulted' },
};
const formatDate = (dateStr: string): string => {
const date = new Date(dateStr);
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
});
};
// ─── Installment Timeline Item ────────────────────────────────────────────────
interface TimelineItemProps {
installment: LoanInstallment;
isLast: boolean;
}
const TimelineItem = ({ installment, isLast }: TimelineItemProps) => {
const isPaid = installment.status === 'paid';
const isOverdue = installment.status === 'overdue';
const isPending = installment.status === 'pending';
// Pick node color/icon based on status
let nodeColor = colors.textSubtle;
let nodeBg = '#F3F4F6';
let iconName: keyof typeof Ionicons.glyphMap = 'ellipse-outline';
if (isPaid) {
nodeColor = colors.successDeep;
nodeBg = colors.successSoft;
iconName = 'checkmark-circle';
} else if (isOverdue) {
nodeColor = colors.error;
nodeBg = colors.errorSoft;
iconName = 'alert-circle';
}
return (
<View className="flex-row">
{/* Timeline column */}
<View className="mr-4 w-8 items-center">
<View
className="h-8 w-8 items-center justify-center rounded-full"
style={{ backgroundColor: nodeBg }}>
<Ionicons name={iconName} size={18} color={nodeColor} />
</View>
{!isLast && (
<View
className="w-[2px] flex-1"
style={{
backgroundColor: isPaid ? colors.successSoft : colors.border,
minHeight: 40,
}}
/>
)}
</View>
{/* Content column */}
<View className="flex-1 pb-5">
<View className="flex-row items-center justify-between">
<Text
className={`text-sm font-semibold ${
isPaid ? 'text-textStrong' : isOverdue ? 'text-error' : 'text-textSecondary'
}`}>
Installment {installment.installmentNumber}
</Text>
<Text className="text-base font-bold text-textStrong">
{formatLoanAmount(installment.amount)}
</Text>
</View>
<View className="mt-1 flex-row items-center justify-between">
<Text className="text-xs text-textMuted">Due: {formatDate(installment.dueDate)}</Text>
{isPaid && installment.paidDate && (
<Text className="text-xs text-successDeep">
Paid {formatDate(installment.paidDate)}
</Text>
)}
{isOverdue && <Text className="text-xs font-semibold text-error">Overdue</Text>}
{isPending && <Text className="text-xs text-textMuted">Upcoming</Text>}
</View>
</View>
</View>
);
};
// ─── Payment Processing Overlay ───────────────────────────────────────────────
interface PaymentOverlayProps {
step: string;
stepLabel: string;
error: string | null;
onDismiss: () => void;
}
const PaymentOverlay = ({ step, stepLabel, error, onDismiss }: PaymentOverlayProps) => {
const isSuccess = step === 'success';
const isFailed = step === 'failed';
const showDismiss = isSuccess || isFailed;
return (
<View className="absolute inset-0 z-50 items-center justify-center" style={styles.overlayBg}>
<View
className="mx-8 w-full max-w-sm items-center rounded-3xl bg-white p-8"
style={styles.card}>
{/* Status icon */}
{isSuccess ? (
<View className="mb-4 h-16 w-16 items-center justify-center rounded-full bg-successSoft">
<Ionicons name="checkmark-circle" size={36} color={colors.successDeep} />
</View>
) : isFailed ? (
<View className="mb-4 h-16 w-16 items-center justify-center rounded-full bg-errorSoft">
<Ionicons name="close-circle" size={36} color={colors.error} />
</View>
) : (
<View className="mb-4">
<ActivityIndicator size="large" color={colors.primary} />
</View>
)}
<Text className="mb-2 text-center text-lg font-bold text-textStrong">
{isSuccess ? 'Payment Successful!' : isFailed ? 'Payment Failed' : 'Processing Payment'}
</Text>
<Text className="mb-6 text-center text-sm text-textSecondary">{error || stepLabel}</Text>
{/* Step indicators */}
{!showDismiss && (
<View className="mb-4 flex-row items-center gap-2">
{(['requesting', 'signing', 'submitting'] as const).map((s) => {
const steps = ['requesting', 'signing', 'submitting'];
const currentIdx = steps.indexOf(step);
const thisIdx = steps.indexOf(s);
const isDone = thisIdx < currentIdx;
const isCurrent = s === step;
return (
<View key={s} className="flex-1 items-center">
<View
className="h-1.5 w-full rounded-full"
style={{
backgroundColor: isDone
? colors.successDeep
: isCurrent
? colors.primary
: colors.border,
}}
/>
<Text className="mt-1 text-[10px] text-textMuted">
{s === 'requesting' ? 'Prepare' : s === 'signing' ? 'Sign' : 'Submit'}
</Text>
</View>
);
})}
</View>
)}
{showDismiss && (
<TouchableOpacity
onPress={onDismiss}
activeOpacity={0.8}
className="items-center rounded-xl px-8 py-3"
style={{ backgroundColor: isSuccess ? colors.primary : colors.error }}>
<Text className="text-sm font-semibold text-white">
{isSuccess ? 'Done' : 'Try Again'}
</Text>
</TouchableOpacity>
)}
</View>
</View>
);
};
// ─── Main Screen ──────────────────────────────────────────────────────────────
interface LoanDetailScreenProps {
loan: Loan;
onBack: () => void;
}
const LoanDetailScreen: React.FC<LoanDetailScreenProps> = ({ loan, onBack }) => {
const insets = useSafeAreaInsets();
const badge = STATUS_BADGE[loan.status];
const progress = getRepaymentProgress(loan);
const progressPercent = Math.round(progress * 100);
const showOverdueWarning = hasOverdueInstallments(loan);
const canPay = loan.status === 'active' && loan.nextPaymentAmount != null;
const {
isProcessing,
paymentStep,
stepLabel,
error: paymentError,
initiatePayment,
reset: resetPayment,
} = useLoanRepayment();
const showPaymentOverlay = paymentStep !== 'idle';
return (
<View style={{ flex: 1, backgroundColor: colors.background }}>
{/* Header */}
<View
style={{
backgroundColor: colors.white,
paddingHorizontal: 16,
paddingTop: insets.top + 16,
paddingBottom: 16,
}}>
<View className="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="flex-1 text-xl font-bold text-text" numberOfLines={1}>
{loan.merchantName}
</Text>
<View className="rounded-full px-3 py-1" style={{ backgroundColor: badge.bg }}>
<Text style={{ color: badge.fg }} className="text-xs font-semibold">
{badge.label}
</Text>
</View>
</View>
</View>
<ScrollView
className="flex-1"
contentContainerStyle={{ padding: 16, paddingBottom: 120 }}
showsVerticalScrollIndicator={false}>
{/* Loan Summary Card */}
<View className="mb-4 rounded-2xl bg-white p-6 shadow-sm">
<View className="mb-4 flex-row items-end justify-between">
<View>
<Text className="text-xs text-textMuted">Loan Amount</Text>
<Text className="text-4xl font-bold text-textStrong">
{formatLoanAmount(loan.amount)}
</Text>
</View>
<View className="items-end">
<Text className="text-xs text-textMuted">With Interest</Text>
<Text className="text-lg font-semibold text-textSecondary">
{formatLoanAmount(loan.totalWithInterest)}
</Text>
</View>
</View>
{/* Stats grid */}
<View className="mb-4 flex-row">
<View className="flex-1 rounded-xl bg-primarySoft p-3">
<Text className="text-[10px] text-textMuted">Interest Rate</Text>
<Text className="text-lg font-bold text-primary">{loan.interestRate}%</Text>
</View>
<View className="mx-2 flex-1 rounded-xl bg-successSoft p-3">
<Text className="text-[10px] text-textMuted">Installments</Text>
<Text className="text-lg font-bold text-successDeep">{loan.installmentCount}</Text>
</View>
<View className="flex-1 rounded-xl bg-amberSoft p-3">
<Text className="text-[10px] text-textMuted">Repaid</Text>
<Text className="text-lg font-bold" style={{ color: colors.amber }}>
{progressPercent}%
</Text>
</View>
</View>
{/* Progress bar */}
<View className="mb-2 h-2.5 w-full rounded-full bg-gray-200">
<View
className="h-2.5 rounded-full"
style={{
width: `${progressPercent}%`,
backgroundColor: loan.status === 'defaulted' ? colors.error : colors.primary,
}}
/>
</View>
<View className="flex-row justify-between">
<Text className="text-xs text-textMuted">
Paid: {formatLoanAmount(loan.amountPaid)}
</Text>
<Text className="text-xs text-textMuted">
Remaining: {formatLoanAmount(loan.totalWithInterest - loan.amountPaid)}
</Text>
</View>
</View>
{/* Overdue Warning */}
{showOverdueWarning && (
<View className="mb-4 flex-row items-start gap-3 rounded-2xl bg-errorSoft p-4">
<Ionicons name="warning" size={20} color={colors.error} />
<View className="flex-1">
<Text className="mb-1 text-sm font-semibold text-error">Overdue Payment Warning</Text>
<Text className="text-xs leading-4 text-textSecondary">
You have overdue installments. Late payments negatively impact your reputation score
and may reduce your available credit limit.
</Text>
</View>
</View>
)}
{/* Payment Timeline */}
<View className="mb-4 rounded-2xl bg-white p-6 shadow-sm">
<Text className="mb-4 text-base font-bold text-textStrong">Payment Timeline</Text>
{loan.installments.map((installment, index) => (
<TimelineItem
key={installment.id}
installment={installment}
isLast={index === loan.installments.length - 1}
/>
))}
</View>
{/* Loan Details Card */}
<View className="rounded-2xl bg-white p-6 shadow-sm">
<Text className="mb-3 text-base font-bold text-textStrong">Loan Details</Text>
<DetailRow label="Loan ID" value={loan.id} />
<DetailRow label="Merchant" value={loan.merchantName} />
<DetailRow label="Created" value={formatDate(loan.createdAt)} />
{loan.completedAt && <DetailRow label="Completed" value={formatDate(loan.completedAt)} />}
{loan.nextPaymentDue && (
<DetailRow label="Next Payment" value={formatDate(loan.nextPaymentDue)} />
)}
{loan.nextPaymentAmount != null && (
<DetailRow label="Next Amount" value={formatLoanAmount(loan.nextPaymentAmount)} />
)}
</View>
</ScrollView>
{/* Fixed CTA at bottom */}
{canPay && (
<View
className="absolute bottom-0 left-0 right-0 border-t border-border bg-white px-6 pb-2 pt-4"
style={{ paddingBottom: Math.max(insets.bottom, 16) }}>
<TouchableOpacity
activeOpacity={0.8}
className="items-center rounded-xl bg-cta py-4"
onPress={() => initiatePayment(loan.id)}
disabled={isProcessing}
accessibilityLabel="Make a payment"
accessibilityRole="button">
<View className="flex-row items-center gap-2">
<Text className="text-base font-semibold text-white">
Pay {formatLoanAmount(loan.nextPaymentAmount!)}
</Text>
<Ionicons name="arrow-forward" size={18} color="white" />
</View>
</TouchableOpacity>
</View>
)}
{/* Payment Processing Overlay */}
{showPaymentOverlay && (
<PaymentOverlay
step={paymentStep}
stepLabel={stepLabel}
error={paymentError}
onDismiss={resetPayment}
/>
)}
</View>
);
};
// ─── Small Components ─────────────────────────────────────────────────────────
const DetailRow = ({ label, value }: { label: string; value: string }) => (
<View className="flex-row items-center justify-between border-b border-borderSubtle py-3">
<Text className="text-sm text-textMuted">{label}</Text>
<Text className="text-sm font-medium text-textStrong" numberOfLines={1}>
{value}
</Text>
</View>
);
// ─── Styles ───────────────────────────────────────────────────────────────────
const styles = StyleSheet.create({
overlayBg: {
backgroundColor: 'rgba(0, 0, 0, 0.5)',
},
card: {
shadowColor: '#000',
shadowOffset: { width: 0, height: 8 },
shadowOpacity: 0.15,
shadowRadius: 24,
elevation: 20,
},
});
export default LoanDetailScreen;