import 'package:built_collection/built_collection.dart'; import 'package:invoiceninja_flutter/redux/app/app_actions.dart'; import 'package:redux/redux.dart'; import 'package:invoiceninja_flutter/data/models/models.dart'; import 'package:invoiceninja_flutter/redux/company/company_actions.dart'; import 'package:invoiceninja_flutter/redux/ui/entity_ui_state.dart'; import 'package:invoiceninja_flutter/redux/ui/list_ui_state.dart'; import 'package:invoiceninja_flutter/redux/payment/payment_actions.dart'; import 'package:invoiceninja_flutter/redux/payment/payment_state.dart'; EntityUIState paymentUIReducer(PaymentUIState state, dynamic action) { return state.rebuild((b) => b ..listUIState.replace(paymentListReducer(state.listUIState, action)) ..editing.replace(editingReducer(state.editing, action)) ..selectedId = selectedIdReducer(state.selectedId, action)); } Reducer selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) => action.paymentId), TypedReducer( (selectedId, action) => action.payment.id), ]); final editingReducer = combineReducers([ TypedReducer(_updateEditing), TypedReducer(_updateEditing), TypedReducer(_updateEditing), TypedReducer(_updateEditing), TypedReducer(_updateEditing), TypedReducer(_updateEditing), TypedReducer((payment, action) { return action.payment.rebuild((b) => b..isChanged = true); }), TypedReducer(_clearEditing), TypedReducer(_clearEditing), ]); PaymentEntity _clearEditing(PaymentEntity payment, dynamic action) { return PaymentEntity(); } PaymentEntity _updateEditing(PaymentEntity payment, dynamic action) { return action.payment; } final paymentListReducer = combineReducers([ TypedReducer(_sortPayments), TypedReducer(_filterPaymentsByState), TypedReducer(_filterPayments), TypedReducer(_filterPaymentsByCustom1), TypedReducer(_filterPaymentsByCustom2), TypedReducer(_filterPaymentsByCustom3), TypedReducer(_filterPaymentsByCustom4), TypedReducer(_filterPaymentsByEntity), TypedReducer(_startListMultiselect), TypedReducer(_addToListMultiselect), TypedReducer( _removeFromListMultiselect), TypedReducer(_clearListMultiselect), ]); ListUIState _filterPaymentsByEntity( ListUIState paymentListState, FilterPaymentsByEntity action) { return paymentListState.rebuild((b) => b ..filterEntityId = action.entityId ..filterEntityType = action.entityType); } ListUIState _filterPaymentsByCustom1( ListUIState paymentListState, FilterPaymentsByCustom1 action) { if (paymentListState.custom1Filters.contains(action.value)) { return paymentListState .rebuild((b) => b..custom1Filters.remove(action.value)); } else { return paymentListState.rebuild((b) => b..custom1Filters.add(action.value)); } } ListUIState _filterPaymentsByCustom2( ListUIState paymentListState, FilterPaymentsByCustom2 action) { if (paymentListState.custom2Filters.contains(action.value)) { return paymentListState .rebuild((b) => b..custom2Filters.remove(action.value)); } else { return paymentListState.rebuild((b) => b..custom2Filters.add(action.value)); } } ListUIState _filterPaymentsByCustom3( ListUIState paymentListState, FilterPaymentsByCustom3 action) { if (paymentListState.custom3Filters.contains(action.value)) { return paymentListState .rebuild((b) => b..custom3Filters.remove(action.value)); } else { return paymentListState.rebuild((b) => b..custom3Filters.add(action.value)); } } ListUIState _filterPaymentsByCustom4( ListUIState paymentListState, FilterPaymentsByCustom4 action) { if (paymentListState.custom4Filters.contains(action.value)) { return paymentListState .rebuild((b) => b..custom4Filters.remove(action.value)); } else { return paymentListState.rebuild((b) => b..custom4Filters.add(action.value)); } } ListUIState _filterPaymentsByState( ListUIState paymentListState, FilterPaymentsByState action) { if (paymentListState.stateFilters.contains(action.state)) { return paymentListState .rebuild((b) => b..stateFilters.remove(action.state)); } else { return paymentListState.rebuild((b) => b..stateFilters.add(action.state)); } } ListUIState _filterPayments( ListUIState paymentListState, FilterPayments action) { return paymentListState.rebuild((b) => b ..filter = action.filter ..filterClearedAt = action.filter == null ? DateTime.now().millisecondsSinceEpoch : paymentListState.filterClearedAt); } ListUIState _sortPayments(ListUIState paymentListState, SortPayments action) { return paymentListState.rebuild((b) => b ..sortAscending = b.sortField != action.field || !b.sortAscending ..sortField = action.field); } ListUIState _startListMultiselect( ListUIState paymentListState, StartPaymentMultiselect action) { return paymentListState.rebuild((b) => b..selectedIds = ListBuilder()); } ListUIState _addToListMultiselect( ListUIState paymentListState, AddToPaymentMultiselect action) { return paymentListState.rebuild((b) => b..selectedIds.add(action.entity.id)); } ListUIState _removeFromListMultiselect( ListUIState paymentListState, RemoveFromPaymentMultiselect action) { return paymentListState .rebuild((b) => b..selectedIds.remove(action.entity.id)); } ListUIState _clearListMultiselect( ListUIState paymentListState, ClearPaymentMultiselect action) { return paymentListState.rebuild((b) => b..selectedIds = null); } final paymentsReducer = combineReducers([ TypedReducer(_updatePayment), TypedReducer(_addPayment), TypedReducer(_setLoadedPayments), TypedReducer(_setLoadedPayment), TypedReducer(_archivePaymentRequest), TypedReducer(_archivePaymentSuccess), TypedReducer(_archivePaymentFailure), TypedReducer(_deletePaymentRequest), TypedReducer(_deletePaymentSuccess), TypedReducer(_deletePaymentFailure), TypedReducer(_restorePaymentRequest), TypedReducer(_restorePaymentSuccess), TypedReducer(_restorePaymentFailure), ]); PaymentState _archivePaymentRequest( PaymentState paymentState, ArchivePaymentRequest action) { final payments = action.paymentIds.map((id) => paymentState.map[id]).toList(); for (int i = 0; i < payments.length; i++) { payments[i] = payments[i] .rebuild((b) => b..archivedAt = DateTime.now().millisecondsSinceEpoch); } return paymentState.rebuild((b) { for (final payment in payments) { b.map[payment.id] = payment; } }); } PaymentState _archivePaymentSuccess( PaymentState paymentState, ArchivePaymentSuccess action) { return paymentState.rebuild((b) { for (final payment in action.payments) { b.map[payment.id] = payment; } }); } PaymentState _archivePaymentFailure( PaymentState paymentState, ArchivePaymentFailure action) { return paymentState.rebuild((b) { for (final payment in action.payments) { b.map[payment.id] = payment; } }); } PaymentState _deletePaymentRequest( PaymentState paymentState, DeletePaymentRequest action) { final payments = action.paymentIds.map((id) => paymentState.map[id]).toList(); for (int i = 0; i < payments.length; i++) { payments[i] = payments[i].rebuild((b) => b ..archivedAt = DateTime.now().millisecondsSinceEpoch ..isDeleted = true); } return paymentState.rebuild((b) { for (final payment in payments) { b.map[payment.id] = payment; } }); } PaymentState _deletePaymentSuccess( PaymentState paymentState, DeletePaymentSuccess action) { return paymentState.rebuild((b) { for (final payment in action.payments) { b.map[payment.id] = payment; } }); } PaymentState _deletePaymentFailure( PaymentState paymentState, DeletePaymentFailure action) { return paymentState.rebuild((b) { for (final payment in action.payments) { b.map[payment.id] = payment; } }); } PaymentState _restorePaymentRequest( PaymentState paymentState, RestorePaymentRequest action) { final payments = action.paymentIds.map((id) => paymentState.map[id]).toList(); for (int i = 0; i < payments.length; i++) { payments[i] = payments[i].rebuild((b) => b ..archivedAt = null ..isDeleted = false); } return paymentState.rebuild((b) { for (final payment in payments) { b.map[payment.id] = payment; } }); } PaymentState _restorePaymentSuccess( PaymentState paymentState, RestorePaymentSuccess action) { return paymentState.rebuild((b) { for (final payment in action.payments) { b.map[payment.id] = payment; } }); } PaymentState _restorePaymentFailure( PaymentState paymentState, RestorePaymentFailure action) { return paymentState.rebuild((b) { for (final payment in action.payments) { b.map[payment.id] = payment; } }); } PaymentState _addPayment(PaymentState paymentState, AddPaymentSuccess action) { return paymentState.rebuild((b) => b ..map[action.payment.id] = action.payment ..list.add(action.payment.id)); } PaymentState _updatePayment( PaymentState paymentState, SavePaymentSuccess action) { return paymentState .rebuild((b) => b..map[action.payment.id] = action.payment); } PaymentState _setLoadedPayment( PaymentState paymentState, LoadPaymentSuccess action) { return paymentState .rebuild((b) => b..map[action.payment.id] = action.payment); } PaymentState _setLoadedPayments( PaymentState paymentState, LoadPaymentsSuccess action) { final state = paymentState.rebuild((b) => b ..lastUpdated = DateTime.now().millisecondsSinceEpoch ..map.addAll(Map.fromIterable( action.payments, key: (dynamic item) => item.id, value: (dynamic item) => item, ))); return state.rebuild((b) => b..list.replace(state.map.keys)); }