From 3e9a67d1aac9baa6713f9f2d493ad18e87e8a416 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 8 Jul 2020 00:26:04 +0300 Subject: [PATCH] Code refactor --- lib/redux/app/app_middleware.dart | 8 +++++--- lib/redux/auth/auth_middleware.dart | 2 +- lib/redux/client/client_reducer.dart | 3 ++- lib/redux/company/company_actions.dart | 7 ++++++- lib/redux/company_gateway/company_gateway_reducer.dart | 2 +- lib/redux/credit/credit_reducer.dart | 2 +- lib/redux/design/design_reducer.dart | 2 +- lib/redux/document/document_reducer.dart | 2 +- lib/redux/expense/expense_reducer.dart | 2 +- lib/redux/group/group_reducer.dart | 2 +- lib/redux/invoice/invoice_reducer.dart | 2 +- lib/redux/payment/payment_reducer.dart | 2 +- lib/redux/payment_term/payment_term_reducer.dart | 2 +- lib/redux/product/product_reducer.dart | 2 +- lib/redux/project/project_reducer.dart | 2 +- lib/redux/quote/quote_reducer.dart | 2 +- lib/redux/task/task_reducer.dart | 2 +- lib/redux/tax_rate/tax_rate_reducer.dart | 2 +- lib/redux/token/token_reducer.dart | 2 +- lib/redux/user/user_reducer.dart | 2 +- lib/redux/vendor/vendor_reducer.dart | 2 +- lib/redux/webhook/webhook_reducer.dart | 2 +- lib/ui/app/menu_drawer_vm.dart | 2 +- lib/ui/settings/account_management_vm.dart | 2 +- stubs/redux/stub/stub_reducer | 2 +- 25 files changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/redux/app/app_middleware.dart b/lib/redux/app/app_middleware.dart index 185c184f0..6cc11f4d6 100644 --- a/lib/redux/app/app_middleware.dart +++ b/lib/redux/app/app_middleware.dart @@ -419,12 +419,14 @@ Middleware _createAccountLoaded() { final action = dynamicAction as LoadAccountSuccess; final response = action.loginResponse; final selectedCompanyIndex = store.state.uiState.selectedCompanyIndex; + final loadedStaticData = response.static.currencies.isNotEmpty; - if (response.static.currencies.isNotEmpty) { + if (loadedStaticData) { store.dispatch(LoadStaticSuccess(data: response.static)); } print('## userCompanies.length: ${response.userCompanies.length}'); + for (int i = 0; i < response.userCompanies.length; i++) { final UserCompanyEntity userCompany = response.userCompanies[i]; @@ -433,11 +435,11 @@ Middleware _createAccountLoaded() { prefs.setString(kSharedPrefToken, userCompany.token.obscuredToken); } - store.dispatch(SelectCompany(i)); + store.dispatch(SelectCompany(companyIndex: i, clearSelection: loadedStaticData)); store.dispatch(LoadCompanySuccess(userCompany)); } - store.dispatch(SelectCompany(selectedCompanyIndex)); + store.dispatch(SelectCompany(companyIndex: selectedCompanyIndex, clearSelection: loadedStaticData)); store.dispatch(UserLoginSuccess()); print('## Account is loaded'); diff --git a/lib/redux/auth/auth_middleware.dart b/lib/redux/auth/auth_middleware.dart index 59404585f..3a66df2c2 100644 --- a/lib/redux/auth/auth_middleware.dart +++ b/lib/redux/auth/auth_middleware.dart @@ -270,7 +270,7 @@ Middleware _createCompany(AuthRepository repository) { store.dispatch(RefreshData( completer: Completer() ..future.then((_) { - store.dispatch(SelectCompany(state.companies.length)); + store.dispatch(SelectCompany(companyIndex: state.companies.length)); store.dispatch(ViewDashboard( navigator: Navigator.of(action.context), force: true)); }), diff --git a/lib/redux/client/client_reducer.dart b/lib/redux/client/client_reducer.dart index a697da8cf..421411ecc 100644 --- a/lib/redux/client/client_reducer.dart +++ b/lib/redux/client/client_reducer.dart @@ -49,7 +49,8 @@ final selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) { return action.client.id; }), - TypedReducer((selectedId, action) => ''), + TypedReducer( + (selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/company/company_actions.dart b/lib/redux/company/company_actions.dart index 145528476..da79bc0d0 100644 --- a/lib/redux/company/company_actions.dart +++ b/lib/redux/company/company_actions.dart @@ -6,9 +6,13 @@ import 'package:invoiceninja_flutter/data/models/company_model.dart'; import 'package:invoiceninja_flutter/redux/app/app_actions.dart'; class SelectCompany { - SelectCompany(this.companyIndex); + SelectCompany({ + @required this.companyIndex, + this.clearSelection = true, + }); final int companyIndex; + final bool clearSelection; } class LoadCompanySuccess { @@ -44,6 +48,7 @@ class SaveCompanyFailure implements StopSaving { class AddCompany implements StartSaving { AddCompany(this.context); + final BuildContext context; } diff --git a/lib/redux/company_gateway/company_gateway_reducer.dart b/lib/redux/company_gateway/company_gateway_reducer.dart index b3fff3cad..3af4e5d37 100644 --- a/lib/redux/company_gateway/company_gateway_reducer.dart +++ b/lib/redux/company_gateway/company_gateway_reducer.dart @@ -21,7 +21,7 @@ Reducer selectedIdReducer = combineReducers([ (String selectedId, action) => action.companyGatewayId), TypedReducer( (String selectedId, action) => action.companyGateway.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer( (selectedId, action) => ''), diff --git a/lib/redux/credit/credit_reducer.dart b/lib/redux/credit/credit_reducer.dart index 87a60266a..3fd730795 100644 --- a/lib/redux/credit/credit_reducer.dart +++ b/lib/redux/credit/credit_reducer.dart @@ -37,7 +37,7 @@ Reducer selectedIdReducer = combineReducers([ (selectedId, action) => action.credit.id), TypedReducer( (selectedId, action) => action.credit.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/design/design_reducer.dart b/lib/redux/design/design_reducer.dart index 380b7f556..64ed3873e 100644 --- a/lib/redux/design/design_reducer.dart +++ b/lib/redux/design/design_reducer.dart @@ -20,7 +20,7 @@ Reducer selectedIdReducer = combineReducers([ (String selectedId, dynamic action) => action.designId), TypedReducer( (String selectedId, dynamic action) => action.design.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/document/document_reducer.dart b/lib/redux/document/document_reducer.dart index a3108554e..49b791e9a 100644 --- a/lib/redux/document/document_reducer.dart +++ b/lib/redux/document/document_reducer.dart @@ -18,7 +18,7 @@ EntityUIState documentUIReducer(DocumentUIState state, dynamic action) { Reducer selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) => action.documentId), //TypedReducer((selectedId, action) => action.document.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/expense/expense_reducer.dart b/lib/redux/expense/expense_reducer.dart index 9849497f0..6f18df829 100644 --- a/lib/redux/expense/expense_reducer.dart +++ b/lib/redux/expense/expense_reducer.dart @@ -19,7 +19,7 @@ Reducer selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) => action.expenseId), TypedReducer( (selectedId, action) => action.expense.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/group/group_reducer.dart b/lib/redux/group/group_reducer.dart index 1a4bd9e6d..c1b4675a4 100644 --- a/lib/redux/group/group_reducer.dart +++ b/lib/redux/group/group_reducer.dart @@ -21,7 +21,7 @@ Reducer selectedIdReducer = combineReducers([ (String selectedId, action) => action.groupId), TypedReducer( (String selectedId, action) => action.group.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/invoice/invoice_reducer.dart b/lib/redux/invoice/invoice_reducer.dart index cda712118..97f110418 100644 --- a/lib/redux/invoice/invoice_reducer.dart +++ b/lib/redux/invoice/invoice_reducer.dart @@ -39,7 +39,7 @@ Reducer selectedIdReducer = combineReducers([ (selectedId, action) => action.invoice.id), TypedReducer( (selectedId, action) => action.invoice.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/payment/payment_reducer.dart b/lib/redux/payment/payment_reducer.dart index 3a6959254..9148ddf5f 100644 --- a/lib/redux/payment/payment_reducer.dart +++ b/lib/redux/payment/payment_reducer.dart @@ -19,7 +19,7 @@ Reducer selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) => action.paymentId), TypedReducer( (selectedId, action) => action.payment.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/payment_term/payment_term_reducer.dart b/lib/redux/payment_term/payment_term_reducer.dart index 2f1b85dee..420be77db 100644 --- a/lib/redux/payment_term/payment_term_reducer.dart +++ b/lib/redux/payment_term/payment_term_reducer.dart @@ -20,7 +20,7 @@ Reducer selectedIdReducer = combineReducers([ (String selectedId, dynamic action) => action.paymentTermId), TypedReducer( (String selectedId, dynamic action) => action.paymentTerm.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/product/product_reducer.dart b/lib/redux/product/product_reducer.dart index 6819bc058..5a975ff9b 100644 --- a/lib/redux/product/product_reducer.dart +++ b/lib/redux/product/product_reducer.dart @@ -56,7 +56,7 @@ Reducer selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) => action.productId), TypedReducer( (selectedId, action) => action.product.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/project/project_reducer.dart b/lib/redux/project/project_reducer.dart index 20b4926d3..31e3ab960 100644 --- a/lib/redux/project/project_reducer.dart +++ b/lib/redux/project/project_reducer.dart @@ -35,7 +35,7 @@ Reducer selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) => action.projectId), TypedReducer( (selectedId, action) => action.project.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/quote/quote_reducer.dart b/lib/redux/quote/quote_reducer.dart index 42bfe4f03..0f4d5da3e 100644 --- a/lib/redux/quote/quote_reducer.dart +++ b/lib/redux/quote/quote_reducer.dart @@ -37,7 +37,7 @@ Reducer selectedIdReducer = combineReducers([ TypedReducer( (selectedId, action) => action.quote.id), TypedReducer((selectedId, action) => action.quote.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/task/task_reducer.dart b/lib/redux/task/task_reducer.dart index 87bad8140..221dd8395 100644 --- a/lib/redux/task/task_reducer.dart +++ b/lib/redux/task/task_reducer.dart @@ -28,7 +28,7 @@ TaskTime editTaskTime(TaskTime taskTime, dynamic action) { Reducer selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) => action.taskId), TypedReducer((selectedId, action) => action.task.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/tax_rate/tax_rate_reducer.dart b/lib/redux/tax_rate/tax_rate_reducer.dart index 97b681d09..1bca237e5 100644 --- a/lib/redux/tax_rate/tax_rate_reducer.dart +++ b/lib/redux/tax_rate/tax_rate_reducer.dart @@ -20,7 +20,7 @@ Reducer selectedIdReducer = combineReducers([ (String selectedId, action) => action.taxRateId), TypedReducer( (String selectedId, action) => action.taxRate.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/token/token_reducer.dart b/lib/redux/token/token_reducer.dart index 1e0c890c8..70b013c11 100644 --- a/lib/redux/token/token_reducer.dart +++ b/lib/redux/token/token_reducer.dart @@ -21,7 +21,7 @@ Reducer selectedIdReducer = combineReducers([ (String selectedId, dynamic action) => action.tokenId), TypedReducer( (String selectedId, dynamic action) => action.token.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/user/user_reducer.dart b/lib/redux/user/user_reducer.dart index 9afbeee0f..362f6fe81 100644 --- a/lib/redux/user/user_reducer.dart +++ b/lib/redux/user/user_reducer.dart @@ -19,7 +19,7 @@ Reducer selectedIdReducer = combineReducers([ TypedReducer((String selectedId, action) => action.userId), TypedReducer( (String selectedId, action) => action.user.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/vendor/vendor_reducer.dart b/lib/redux/vendor/vendor_reducer.dart index 9351c20da..c4e1400e1 100644 --- a/lib/redux/vendor/vendor_reducer.dart +++ b/lib/redux/vendor/vendor_reducer.dart @@ -47,7 +47,7 @@ Reducer selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) => action.vendorId), TypedReducer( (selectedId, action) => action.vendor.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/redux/webhook/webhook_reducer.dart b/lib/redux/webhook/webhook_reducer.dart index 25abdcd8b..45f759445 100644 --- a/lib/redux/webhook/webhook_reducer.dart +++ b/lib/redux/webhook/webhook_reducer.dart @@ -21,7 +21,7 @@ Reducer selectedIdReducer = combineReducers([ (String selectedId, dynamic action) => action.webhookId), TypedReducer( (String selectedId, dynamic action) => action.webhook.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), diff --git a/lib/ui/app/menu_drawer_vm.dart b/lib/ui/app/menu_drawer_vm.dart index 50c08ba73..b2e141545 100644 --- a/lib/ui/app/menu_drawer_vm.dart +++ b/lib/ui/app/menu_drawer_vm.dart @@ -60,7 +60,7 @@ class MenuDrawerVM { onCompanyChanged: (BuildContext context, String companyIndex, CompanyEntity company) { store.dispatch(ClearEntityFilter()); - store.dispatch(SelectCompany(int.parse(companyIndex))); + store.dispatch(SelectCompany(companyIndex: int.parse(companyIndex))); if (store.state.isStale) { if (!store.state.isLoaded && store.state.company.isLarge) { store.dispatch(LoadClients()); diff --git a/lib/ui/settings/account_management_vm.dart b/lib/ui/settings/account_management_vm.dart index f07ca4d6d..f4b6141c6 100644 --- a/lib/ui/settings/account_management_vm.dart +++ b/lib/ui/settings/account_management_vm.dart @@ -66,7 +66,7 @@ class AccountManagementVM { break; } } - store.dispatch(SelectCompany(index)); + store.dispatch(SelectCompany(companyIndex: index)); store.dispatch(ViewDashboard(navigator: Navigator.of(context))); } else { store.dispatch(UserLogout(context)); diff --git a/stubs/redux/stub/stub_reducer b/stubs/redux/stub/stub_reducer index 9f2940c5c..0b815bcdb 100644 --- a/stubs/redux/stub/stub_reducer +++ b/stubs/redux/stub/stub_reducer @@ -21,7 +21,7 @@ Reducer selectedIdReducer = combineReducers([ (String selectedId, dynamic action) => action.stubId), TypedReducer( (String selectedId, dynamic action) => action.stub.id), - TypedReducer((selectedId, action) => ''), + TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''),