-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove use of useActions #3911
Remove use of useActions #3911
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import React, { useState, useEffect, useCallback } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
import deepEqual from 'deep-equal'; | ||
import { t } from 'i18next'; | ||
|
||
import { | ||
getPayees, | ||
importPreviewTransactions, | ||
importTransactions, | ||
parseTransactions, | ||
} from 'loot-core/client/actions'; | ||
import { amountToInteger } from 'loot-core/src/shared/util'; | ||
|
||
import { useActions } from '../../../hooks/useActions'; | ||
import { useDateFormat } from '../../../hooks/useDateFormat'; | ||
import { useSyncedPrefs } from '../../../hooks/useSyncedPrefs'; | ||
import { theme } from '../../../style'; | ||
|
@@ -137,12 +143,7 @@ function parseCategoryFields(trans, categories) { | |
export function ImportTransactionsModal({ options }) { | ||
const dateFormat = useDateFormat() || 'MM/dd/yyyy'; | ||
const [prefs, savePrefs] = useSyncedPrefs(); | ||
const { | ||
parseTransactions, | ||
importTransactions, | ||
importPreviewTransactions, | ||
getPayees, | ||
} = useActions(); | ||
const dispatch = useDispatch(); | ||
|
||
const [multiplierAmount, setMultiplierAmount] = useState(''); | ||
const [loadingState, setLoadingState] = useState('parsing'); | ||
|
@@ -263,9 +264,8 @@ export function ImportTransactionsModal({ options }) { | |
} | ||
|
||
// Retreive the transactions that would be updated (along with the existing trx) | ||
const previewTrx = await importPreviewTransactions( | ||
accountId, | ||
previewTransactions, | ||
const previewTrx = await dispatch( | ||
importPreviewTransactions(accountId, previewTransactions), | ||
Comment on lines
+267
to
+268
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding error boundaries for dispatched actions While the transition to Apply this diff to add error handling: - const previewTrx = await dispatch(
- importPreviewTransactions(accountId, previewTransactions),
- );
+ let previewTrx;
+ try {
+ previewTrx = await dispatch(
+ importPreviewTransactions(accountId, previewTransactions),
+ );
+ } catch (error) {
+ console.error('Failed to preview transactions:', error);
+ setError({ parsed: false, message: 'Failed to preview transactions' });
+ setLoadingState(null);
+ return [];
+ }
- const { errors, transactions: parsedTransactions = [] } = await dispatch(
- parseTransactions(filename, options),
- );
+ let result;
+ try {
+ result = await dispatch(parseTransactions(filename, options));
+ } catch (error) {
+ console.error('Failed to parse transactions:', error);
+ setError({ parsed: true, message: 'Failed to parse transactions' });
+ return { errors: [error], transactions: [] };
+ }
+ const { errors, transactions: parsedTransactions = [] } = result;
- const didChange = await dispatch(
- importTransactions(accountId, finalTransactions, reconcile),
- );
- if (didChange) {
- await dispatch(getPayees());
- }
+ try {
+ const didChange = await dispatch(
+ importTransactions(accountId, finalTransactions, reconcile),
+ );
+ if (didChange) {
+ await dispatch(getPayees());
+ }
+ } catch (error) {
+ console.error('Failed to import transactions:', error);
+ setError({ parsed: false, message: 'Failed to import transactions' });
+ setLoadingState(null);
+ return;
+ } Also applies to: 323-325, 656-660 |
||
); | ||
const matchedUpdateMap = previewTrx.reduce((map, entry) => { | ||
map[entry.transaction.trx_id] = entry; | ||
|
@@ -309,7 +309,7 @@ export function ImportTransactionsModal({ options }) { | |
return next; | ||
}, []); | ||
}, | ||
[accountId, categories.list, clearOnImport, importPreviewTransactions], | ||
[accountId, categories.list, clearOnImport, dispatch], | ||
); | ||
|
||
const parse = useCallback( | ||
|
@@ -320,8 +320,9 @@ export function ImportTransactionsModal({ options }) { | |
setFilename(filename); | ||
setFileType(filetype); | ||
|
||
const { errors, transactions: parsedTransactions = [] } = | ||
await parseTransactions(filename, options); | ||
const { errors, transactions: parsedTransactions = [] } = await dispatch( | ||
parseTransactions(filename, options), | ||
); | ||
|
||
let index = 0; | ||
const transactions = parsedTransactions.map(trans => { | ||
|
@@ -399,11 +400,11 @@ export function ImportTransactionsModal({ options }) { | |
}, | ||
[ | ||
accountId, | ||
dispatch, | ||
getImportPreview, | ||
inOutMode, | ||
multiplierAmount, | ||
outValue, | ||
parseTransactions, | ||
prefs, | ||
], | ||
); | ||
|
@@ -427,7 +428,6 @@ export function ImportTransactionsModal({ options }) { | |
|
||
parse(options.filename, parseOptions); | ||
}, [ | ||
parseTransactions, | ||
options.filename, | ||
delimiter, | ||
hasHeaderRow, | ||
|
@@ -653,13 +653,11 @@ export function ImportTransactionsModal({ options }) { | |
}); | ||
} | ||
|
||
const didChange = await importTransactions( | ||
accountId, | ||
finalTransactions, | ||
reconcile, | ||
const didChange = await dispatch( | ||
importTransactions(accountId, finalTransactions, reconcile), | ||
); | ||
if (didChange) { | ||
await getPayees(); | ||
await dispatch(getPayees()); | ||
Comment on lines
+656
to
+660
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling for dispatched actions When dispatching Apply this diff to add error handling: +try {
const didChange = await dispatch(
importTransactions(accountId, finalTransactions, reconcile),
);
if (didChange) {
await dispatch(getPayees());
}
+} catch (error) {
+ // Handle the error appropriately
+ console.error('Error importing transactions:', error);
+ setError({ parsed: false, message: 'Failed to import transactions.' });
+ setLoadingState(null);
+}
|
||
} | ||
|
||
if (onImported) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add error handling and loading state for sync action
The sync action should include error handling and potentially a loading state for better user feedback.