From c358ffec16858616ec65da4154539a67c9c7f10a Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 23 Jun 2020 14:44:06 +0300 Subject: [PATCH] Only set loaded if initial load has data --- lib/data/models/company_model.dart | 2 ++ lib/redux/app/app_middleware.dart | 4 ---- lib/redux/client/client_reducer.dart | 8 ++++++-- lib/redux/credit/credit_reducer.dart | 8 ++++++-- lib/redux/design/design_reducer.dart | 8 ++++++-- lib/redux/expense/expense_reducer.dart | 8 ++++++-- lib/redux/invoice/invoice_reducer.dart | 8 ++++++-- lib/redux/payment/payment_reducer.dart | 8 ++++++-- lib/redux/product/product_reducer.dart | 8 ++++++-- lib/redux/project/project_reducer.dart | 8 ++++++-- lib/redux/quote/quote_reducer.dart | 7 ++++--- lib/redux/task/task_reducer.dart | 9 +++++++-- lib/redux/vendor/vendor_reducer.dart | 8 ++++++-- stubs/redux/stub/stub_reducer | 8 ++++++-- 14 files changed, 73 insertions(+), 29 deletions(-) diff --git a/lib/data/models/company_model.dart b/lib/data/models/company_model.dart index afbe63925..37644b9de 100644 --- a/lib/data/models/company_model.dart +++ b/lib/data/models/company_model.dart @@ -268,6 +268,8 @@ abstract class CompanyEntity extends Object bool get enableThirdItemTaxRate => (numberOfItemTaxRates ?? 0) >= 3; + bool get hasData => clients.isNotEmpty || products.isNotEmpty || tasks.isNotEmpty; + bool get hasCustomSurcharge => hasCustomField(CustomFieldType.surcharge1) || hasCustomField(CustomFieldType.surcharge2) || diff --git a/lib/redux/app/app_middleware.dart b/lib/redux/app/app_middleware.dart index d76a30b4f..4957a71e0 100644 --- a/lib/redux/app/app_middleware.dart +++ b/lib/redux/app/app_middleware.dart @@ -400,10 +400,6 @@ Middleware _createAccountLoaded() { store.dispatch(SelectCompany(i)); store.dispatch(LoadCompanySuccess(userCompany)); - - /* - stub - */ } store.dispatch(SelectCompany(selectedCompanyIndex)); diff --git a/lib/redux/client/client_reducer.dart b/lib/redux/client/client_reducer.dart index fba732e2c..af95b8c2b 100644 --- a/lib/redux/client/client_reducer.dart +++ b/lib/redux/client/client_reducer.dart @@ -363,5 +363,9 @@ ClientState _setLoadedClients( clientState.loadClients(action.clients); ClientState _setLoadedCompany( - ClientState clientState, LoadCompanySuccess action) => - clientState.loadClients(action.userCompany.company.clients); + ClientState clientState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? clientState.loadClients(company.clients) + : clientState; +} diff --git a/lib/redux/credit/credit_reducer.dart b/lib/redux/credit/credit_reducer.dart index fce2dec36..dd065576c 100644 --- a/lib/redux/credit/credit_reducer.dart +++ b/lib/redux/credit/credit_reducer.dart @@ -400,5 +400,9 @@ CreditState _setLoadedCredits( creditState.loadCredits(action.credits); CreditState _setLoadedCompany( - CreditState creditState, LoadCompanySuccess action) => - creditState.loadCredits(action.userCompany.company.credits); + CreditState creditState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? creditState.loadCredits(company.credits) + : creditState; +} diff --git a/lib/redux/design/design_reducer.dart b/lib/redux/design/design_reducer.dart index 20a7dcde5..a26b5bacb 100644 --- a/lib/redux/design/design_reducer.dart +++ b/lib/redux/design/design_reducer.dart @@ -279,5 +279,9 @@ DesignState _setLoadedDesigns( designState.loadDesigns(action.designs); DesignState _setLoadedCompany( - DesignState designState, LoadCompanySuccess action) => - designState.loadDesigns(action.userCompany.company.designs); + DesignState designState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? designState.loadDesigns(company.designs) + : designState; +} diff --git a/lib/redux/expense/expense_reducer.dart b/lib/redux/expense/expense_reducer.dart index 7e9e9520d..349762c8d 100644 --- a/lib/redux/expense/expense_reducer.dart +++ b/lib/redux/expense/expense_reducer.dart @@ -316,5 +316,9 @@ ExpenseState _setLoadedExpenses( expenseState.loadExpenses(action.expenses); ExpenseState _setLoadedCompany( - ExpenseState expenseState, LoadCompanySuccess action) => - expenseState.loadExpenses(action.userCompany.company.expenses); + ExpenseState expenseState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? expenseState.loadExpenses(company.expenses) + : expenseState; +} diff --git a/lib/redux/invoice/invoice_reducer.dart b/lib/redux/invoice/invoice_reducer.dart index efa9e3326..b3d4a42ef 100644 --- a/lib/redux/invoice/invoice_reducer.dart +++ b/lib/redux/invoice/invoice_reducer.dart @@ -451,5 +451,9 @@ InvoiceState _setLoadedInvoices( invoiceState.loadInvoices(action.invoices); InvoiceState _setLoadedCompany( - InvoiceState invoiceState, LoadCompanySuccess action) => - invoiceState.loadInvoices(action.userCompany.company.invoices); + InvoiceState invoiceState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? invoiceState.loadInvoices(company.invoices) + : invoiceState; +} diff --git a/lib/redux/payment/payment_reducer.dart b/lib/redux/payment/payment_reducer.dart index 254289fb0..f1c40c20b 100644 --- a/lib/redux/payment/payment_reducer.dart +++ b/lib/redux/payment/payment_reducer.dart @@ -317,5 +317,9 @@ PaymentState _setLoadedPayments( paymentState.loadPayments(action.payments); PaymentState _setLoadedCompany( - PaymentState paymentState, LoadCompanySuccess action) => - paymentState.loadPayments(action.userCompany.company.payments); + PaymentState paymentState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? paymentState.loadPayments(company.payments) + : paymentState; +} diff --git a/lib/redux/product/product_reducer.dart b/lib/redux/product/product_reducer.dart index f4ed766ab..9c9b53a7c 100644 --- a/lib/redux/product/product_reducer.dart +++ b/lib/redux/product/product_reducer.dart @@ -295,5 +295,9 @@ ProductState _setLoadedProducts( productState.loadProducts(action.products); ProductState _setLoadedCompany( - ProductState productState, LoadCompanySuccess action) => - productState.loadProducts(action.userCompany.company.products); + ProductState productState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? productState.loadProducts(company.products) + : productState; +} diff --git a/lib/redux/project/project_reducer.dart b/lib/redux/project/project_reducer.dart index 6ba7cfb84..ea17c4c21 100644 --- a/lib/redux/project/project_reducer.dart +++ b/lib/redux/project/project_reducer.dart @@ -321,5 +321,9 @@ ProjectState _setLoadedProjects( projectState.loadProjects(action.projects); ProjectState _setLoadedCompany( - ProjectState projectState, LoadCompanySuccess action) => - projectState.loadProjects(action.userCompany.company.projects); + ProjectState projectState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? projectState.loadProjects(company.projects) + : projectState; +} diff --git a/lib/redux/quote/quote_reducer.dart b/lib/redux/quote/quote_reducer.dart index 271a65095..ba2b22b1a 100644 --- a/lib/redux/quote/quote_reducer.dart +++ b/lib/redux/quote/quote_reducer.dart @@ -408,6 +408,7 @@ QuoteState _updateQuote(QuoteState quoteState, dynamic action) { QuoteState _setLoadedQuotes(QuoteState quoteState, LoadQuotesSuccess action) => quoteState.loadQuotes(action.quotes); -QuoteState _setLoadedCompany( - QuoteState quoteState, LoadCompanySuccess action) => - quoteState.loadQuotes(action.userCompany.company.quotes); +QuoteState _setLoadedCompany(QuoteState quoteState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData ? quoteState.loadQuotes(company.quotes) : quoteState; +} diff --git a/lib/redux/task/task_reducer.dart b/lib/redux/task/task_reducer.dart index a103b787e..1c666ebeb 100644 --- a/lib/redux/task/task_reducer.dart +++ b/lib/redux/task/task_reducer.dart @@ -297,5 +297,10 @@ TaskState _setLoadedTask(TaskState taskState, LoadTaskSuccess action) { TaskState _setLoadedTasks(TaskState taskState, LoadTasksSuccess action) => taskState.loadTasks(action.tasks); -TaskState _setLoadedCompany(TaskState taskState, LoadCompanySuccess action) => - taskState.loadTasks(action.userCompany.company.tasks); +TaskState _setLoadedCompany(TaskState taskState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? taskState.loadTasks(company.tasks) + : taskState; +} + diff --git a/lib/redux/vendor/vendor_reducer.dart b/lib/redux/vendor/vendor_reducer.dart index dc00be37c..01b371073 100644 --- a/lib/redux/vendor/vendor_reducer.dart +++ b/lib/redux/vendor/vendor_reducer.dart @@ -344,5 +344,9 @@ VendorState _setLoadedVendors( vendorState.loadVendors(action.vendors); VendorState _setLoadedCompany( - VendorState vendorState, LoadCompanySuccess action) => - vendorState.loadVendors(action.userCompany.company.vendors); + VendorState vendorState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? vendorState.loadVendors(company.vendors) + : vendorState; +} diff --git a/stubs/redux/stub/stub_reducer b/stubs/redux/stub/stub_reducer index f45037efd..ad1842f68 100644 --- a/stubs/redux/stub/stub_reducer +++ b/stubs/redux/stub/stub_reducer @@ -285,5 +285,9 @@ StubState _setLoadedStubs( stubState.loadStubs(action.stubs); StubState _setLoadedCompany( - StubState stubState, LoadCompanySuccess action) => - stubState.loadStubs(action.userCompany.company.stubs); + StubState stubState, LoadCompanySuccess action) { + final company = action.userCompany.company; + return company.hasData + ? stubState.loadStubs(company.stubs) + : stubState; +}