Implement transaction fetching and display in HomeScreen, update TransactionList for localization and improved data handling
This commit is contained in:
@@ -7,13 +7,13 @@ const TransactionList = ({ transactions }) => {
|
|||||||
if (!transactions || transactions.length === 0) {
|
if (!transactions || transactions.length === 0) {
|
||||||
return (
|
return (
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
<Text style={styles.emptyText}>No transactions yet.</Text>
|
<Text style={styles.emptyText}>Soňky 10 günde kart hereketleri ýok...</Text>
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const groupedTransactions = transactions.reduce((acc, transaction) => {
|
const groupedTransactions = transactions.reduce((acc, transaction) => {
|
||||||
const date = new Date(transaction.date).toLocaleDateString('en-US', {
|
const date = new Date(transaction.date).toLocaleDateString('tk', {
|
||||||
year: 'numeric',
|
year: 'numeric',
|
||||||
month: 'long',
|
month: 'long',
|
||||||
day: 'numeric',
|
day: 'numeric',
|
||||||
@@ -36,7 +36,7 @@ const TransactionList = ({ transactions }) => {
|
|||||||
</View>
|
</View>
|
||||||
<View style={styles.transactionDetails}>
|
<View style={styles.transactionDetails}>
|
||||||
<Text style={styles.transactionTitle}>{item.description}</Text>
|
<Text style={styles.transactionTitle}>{item.description}</Text>
|
||||||
<Text style={styles.transactionDate}>{new Date(item.date).toLocaleTimeString()}</Text>
|
<Text style={styles.transactionDate}>{item.time ?? '-'}</Text>
|
||||||
</View>
|
</View>
|
||||||
<Text
|
<Text
|
||||||
style={[
|
style={[
|
||||||
@@ -56,7 +56,7 @@ const TransactionList = ({ transactions }) => {
|
|||||||
<FlatList
|
<FlatList
|
||||||
data={groupedTransactions[date]}
|
data={groupedTransactions[date]}
|
||||||
renderItem={renderTransactionItem}
|
renderItem={renderTransactionItem}
|
||||||
keyExtractor={(item) => item.id.toString()}
|
keyExtractor={(item) => item.id?.toString()}
|
||||||
scrollEnabled={false}
|
scrollEnabled={false}
|
||||||
ItemSeparatorComponent={() => <View style={styles.separator} />}
|
ItemSeparatorComponent={() => <View style={styles.separator} />}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -110,7 +110,8 @@ const HomeScreen = () => {
|
|||||||
const [cardBalanceError, setCardBalanceError] = useState(null);
|
const [cardBalanceError, setCardBalanceError] = useState(null);
|
||||||
const [isBalanceVisible, setIsBalanceVisible] = useState(true);
|
const [isBalanceVisible, setIsBalanceVisible] = useState(true);
|
||||||
const [refreshing, setRefreshing] = useState(false);
|
const [refreshing, setRefreshing] = useState(false);
|
||||||
const [transactions, setTransactions] = useState(STATIC_TRANSACTIONS);
|
const [transactions, setTransactions] = useState([]);
|
||||||
|
const [loadingTransactions, setLoadingTransactions] = useState(true);
|
||||||
|
|
||||||
const showBalanceCard = !loadingCardBalance && cardBalanceError === null && cardBalance !== null;
|
const showBalanceCard = !loadingCardBalance && cardBalanceError === null && cardBalance !== null;
|
||||||
|
|
||||||
@@ -140,10 +141,12 @@ const HomeScreen = () => {
|
|||||||
// Ensure user has filled all required card & passport fields
|
// Ensure user has filled all required card & passport fields
|
||||||
if (!user?.passport_serie || !user?.passport_id || !user?.card_number || !user?.card_month || !user?.card_year) {
|
if (!user?.passport_serie || !user?.passport_id || !user?.card_number || !user?.card_month || !user?.card_year) {
|
||||||
setLoadingCardBalance(false);
|
setLoadingCardBalance(false);
|
||||||
|
setLoadingTransactions(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setLoadingCardBalance(true);
|
setLoadingCardBalance(true);
|
||||||
|
setLoadingTransactions(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await apiService.getCardBalanceQuickCheck();
|
const res = await apiService.getCardBalanceQuickCheck();
|
||||||
@@ -165,6 +168,19 @@ const HomeScreen = () => {
|
|||||||
} finally {
|
} finally {
|
||||||
setLoadingCardBalance(false);
|
setLoadingCardBalance(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const transRes = await apiService.getCardTransactionsLastMonth();
|
||||||
|
if (transRes.success) {
|
||||||
|
setTransactions(transRes.data || []);
|
||||||
|
} else {
|
||||||
|
console.warn('Failed to fetch transactions:', transRes.error);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Error fetching transactions:', e);
|
||||||
|
} finally {
|
||||||
|
setLoadingTransactions(false);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchCardData();
|
fetchCardData();
|
||||||
@@ -189,6 +205,8 @@ const HomeScreen = () => {
|
|||||||
if (!user?.passport_serie || !user?.passport_id || !user?.card_number || !user?.card_month || !user?.card_year) {
|
if (!user?.passport_serie || !user?.passport_id || !user?.card_number || !user?.card_month || !user?.card_year) {
|
||||||
setCardBalance(null); // Clear previous balance if conditions are not met
|
setCardBalance(null); // Clear previous balance if conditions are not met
|
||||||
setCardBalanceError(null);
|
setCardBalanceError(null);
|
||||||
|
setTransactions([]);
|
||||||
|
setLoadingTransactions(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@@ -208,6 +226,16 @@ const HomeScreen = () => {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn('Error fetching card balance during refresh:', e);
|
console.warn('Error fetching card balance during refresh:', e);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
const transRes = await apiService.getCardTransactionsLastMonth();
|
||||||
|
if (transRes.success) {
|
||||||
|
setTransactions(transRes.data || []);
|
||||||
|
} else {
|
||||||
|
console.warn('Failed to fetch transactions during refresh:', transRes.error);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Error fetching transactions during refresh:', e);
|
||||||
|
}
|
||||||
})(),
|
})(),
|
||||||
]);
|
]);
|
||||||
setRefreshing(false);
|
setRefreshing(false);
|
||||||
@@ -275,12 +303,16 @@ const HomeScreen = () => {
|
|||||||
{showBalanceCard && (
|
{showBalanceCard && (
|
||||||
<View style={styles.section}>
|
<View style={styles.section}>
|
||||||
<View style={styles.sectionHeader}>
|
<View style={styles.sectionHeader}>
|
||||||
<Text style={styles.sectionTitle}>Transactions</Text>
|
<Text style={styles.sectionTitle}>Kart hereketleri</Text>
|
||||||
<TouchableOpacity>
|
<TouchableOpacity>
|
||||||
<Text style={styles.seeAllText}>See All</Text>
|
{/* <Text style={styles.seeAllText}>Hemmesi</Text> */}
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
</View>
|
</View>
|
||||||
<TransactionList transactions={transactions} />
|
{loadingTransactions ? (
|
||||||
|
<ActivityIndicator color={COLORS.primary} style={{ marginTop: 16 }} />
|
||||||
|
) : (
|
||||||
|
<TransactionList transactions={transactions} />
|
||||||
|
)}
|
||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
|
|||||||
@@ -67,6 +67,37 @@ class ApiService {
|
|||||||
return authService.getTransactions(page, limit);
|
return authService.getTransactions(page, limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCardTransactionsLastMonth() {
|
||||||
|
try {
|
||||||
|
const response = await authService.getCardTransactionsLastMonth();
|
||||||
|
if (response && response.data && Array.isArray(response.data.transactions)) {
|
||||||
|
const mappedTransactions = response.data.transactions.map((t, index) => ({
|
||||||
|
id: `${t.rrn}-${index}`,
|
||||||
|
type: t.sign === '-' ? 'debit' : 'credit',
|
||||||
|
description: t.binfo || t.opername,
|
||||||
|
date: `${t.operdate}`,
|
||||||
|
time: t.trantime,
|
||||||
|
amount: t.currOperSum,
|
||||||
|
currency: t.currCode,
|
||||||
|
})).reverse();
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
data: mappedTransactions,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
console.warn('Invalid transaction data structure:', response);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: 'Could not parse transaction data.',
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
error: error.message,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Transfer and payments
|
// Transfer and payments
|
||||||
async transferMoney(data) {
|
async transferMoney(data) {
|
||||||
return authService.transferMoney(data);
|
return authService.transferMoney(data);
|
||||||
|
|||||||
@@ -162,6 +162,10 @@ class AuthService {
|
|||||||
return this.makeRequest(`/user/cards/${cardId}/unblock`, null, true);
|
return this.makeRequest(`/user/cards/${cardId}/unblock`, null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCardTransactionsLastMonth() {
|
||||||
|
return this.makeRequest('/card-transactions-last-month', null, true, 'GET');
|
||||||
|
}
|
||||||
|
|
||||||
async changePassword(currentPassword, newPassword) {
|
async changePassword(currentPassword, newPassword) {
|
||||||
return this.makeRequest('/user/change-password', {
|
return this.makeRequest('/user/change-password', {
|
||||||
current_password: currentPassword,
|
current_password: currentPassword,
|
||||||
|
|||||||
Reference in New Issue
Block a user