forked from TrustUp-app/TrustUp-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-invest.ts
More file actions
145 lines (120 loc) · 4.1 KB
/
Copy pathuse-invest.ts
File metadata and controls
145 lines (120 loc) · 4.1 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
import { useState, useRef, useCallback } from 'react';
import { ScrollView } from 'react-native';
/**
* Return type for the useInvest hook
*/
export interface UseInvestReturn {
depositAmount: string;
scrollViewRef: React.RefObject<ScrollView | null>;
formatCurrency: (value: string) => string;
handleAmountChange: (text: string) => void;
isDepositValid: () => boolean;
handleDeposit: () => void;
}
/**
* Formats the currency value with thousand separators,
* preserving up to 2 decimal places.
*/
export const formatCurrency = (value: string): string => {
if (!value || value.trim() === '') {
return '';
}
// Filter out non-numeric characters except decimal point
let filtered = value.replace(/[^\d.]/g, '');
// Handle multiple decimal points - keep only the first one
const decimalCount = (filtered.match(/\./g) || []).length;
if (decimalCount > 1) {
const parts = filtered.split('.');
filtered = parts[0] + '.' + parts.slice(1).join('');
}
// Remove leading zeros (except for values < 1)
if (filtered.startsWith('0') && filtered.length > 1 && filtered[1] !== '.') {
filtered = filtered.replace(/^0+/, '');
}
// Handle case where only decimal point remains after filtering
if (filtered === '.' || filtered === '') {
return '';
}
// Separate integer and decimal portions
const [integerPart, decimalPart] = filtered.split('.');
// Add thousand separators to the integer portion
const formattedInteger = (integerPart || '0').replace(
/\B(?=(\d{3})+(?!\d))/g,
',',
);
// Preserve the decimal point while typing and limit to 2 decimal places
if (decimalPart !== undefined) {
return `${formattedInteger}.${decimalPart.substring(0, 2)}`;
}
return formattedInteger;
};
/**
* Validates if the given deposit amount is at least $10.00.
*/
export const validateDepositAmount = (depositAmount: string): boolean => {
// Handle empty input
if (!depositAmount || depositAmount === '') {
return false;
}
// Parse depositAmount to number
const amount = parseFloat(depositAmount);
// Handle NaN or zero cases
if (isNaN(amount) || amount === 0) {
return false;
}
// Check if amount is >= 10.00
return amount >= 10.0;
};
/**
* Custom hook for Invest logic
* Handles formatting, form state, and handlers for the Invest Screen.
*/
export const useInvest = (): UseInvestReturn => {
const [depositAmount, setDepositAmount] = useState<string>('');
const scrollViewRef = useRef<ScrollView>(null);
// Handle amount input changes
const handleAmountChange = useCallback((text: string): void => {
// Remove $ sign and spaces if present
let filtered = text.replace(/[$\s,]/g, '');
// Filter out non-numeric characters except decimal point
filtered = filtered.replace(/[^\d.]/g, '');
// Handle multiple decimal points - keep only the first one
const decimalCount = (filtered.match(/\./g) || []).length;
if (decimalCount > 1) {
const parts = filtered.split('.');
filtered = parts[0] + '.' + parts.slice(1).join('');
}
// Remove leading zeros (except for values < 1)
if (filtered.startsWith('0') && filtered.length > 1 && filtered[1] !== '.') {
filtered = filtered.replace(/^0+/, '');
}
// Handle empty input
if (filtered === '' || filtered === '.') {
setDepositAmount('');
return;
}
// Limit to 2 decimal places
const parts = filtered.split('.');
if (parts.length > 1 && parts[1].length > 2) {
filtered = parts[0] + '.' + parts[1].substring(0, 2);
}
// Update state with the raw numeric value (without $ sign)
setDepositAmount(filtered);
}, []);
// Validate deposit amount using derived pure function
const isDepositValid = useCallback((): boolean => {
return validateDepositAmount(depositAmount);
}, [depositAmount]);
// Handle deposit button press
const handleDeposit = useCallback((): void => {
console.log(`Deposit initiated: $${formatCurrency(depositAmount)}`);
}, [depositAmount]);
return {
depositAmount,
scrollViewRef,
formatCurrency,
handleAmountChange,
isDepositValid,
handleDeposit,
};
};