feat: add addTransaction function
- Add addTransaction callback to hook - Use useCallback for performance - Save to AsyncStorage on add
This commit is contained in:
parent
9e41d16f8a
commit
2b532c7472
@ -1,4 +1,4 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect, useCallback } from 'react';
|
||||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||||
import { Transaction } from '../types';
|
import { Transaction } from '../types';
|
||||||
|
|
||||||
@ -8,7 +8,7 @@ export const useTransactions = () => {
|
|||||||
const [transactions, setTransactions] = useState<Transaction[]>([]);
|
const [transactions, setTransactions] = useState<Transaction[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
const loadTransactions = async () => {
|
const loadTransactions = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
const stored = await AsyncStorage.getItem(STORAGE_KEY);
|
const stored = await AsyncStorage.getItem(STORAGE_KEY);
|
||||||
@ -20,15 +20,24 @@ export const useTransactions = () => {
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadTransactions();
|
loadTransactions();
|
||||||
|
}, [loadTransactions]);
|
||||||
|
|
||||||
|
const addTransaction = useCallback((transaction: Transaction) => {
|
||||||
|
setTransactions((prev) => {
|
||||||
|
const updated = [transaction, ...prev];
|
||||||
|
AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(updated));
|
||||||
|
return updated;
|
||||||
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
transactions,
|
transactions,
|
||||||
loading,
|
loading,
|
||||||
|
addTransaction,
|
||||||
refresh: loadTransactions,
|
refresh: loadTransactions,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
Loading…
Reference in New Issue
Block a user