import 'package:redux/redux.dart'; import 'package:built_collection/built_collection.dart'; import 'package:invoiceninja_flutter/redux/app/app_actions.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/webhook/webhook_actions.dart'; import 'package:invoiceninja_flutter/redux/ui/list_ui_state.dart'; import 'package:invoiceninja_flutter/redux/webhook/webhook_state.dart'; import 'package:invoiceninja_flutter/data/models/entities.dart'; EntityUIState webhookUIReducer(WebhookUIState state, dynamic action) { return state.rebuild((b) => b ..listUIState.replace(webhookListReducer(state.listUIState, action)) ..editing.replace(editingReducer(state.editing, action)) ..selectedId = selectedIdReducer(state.selectedId, action) ..forceSelected = forceSelectedReducer(state.forceSelected, action)); } final forceSelectedReducer = combineReducers([ TypedReducer((completer, action) => true), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), TypedReducer((completer, action) => false), ]); Reducer selectedIdReducer = combineReducers([ TypedReducer((completer, action) => ''), TypedReducer((completer, action) => ''), TypedReducer((selectedId, action) => action.entityType == EntityType.webhook ? action.entityId : selectedId), TypedReducer( (String selectedId, dynamic action) => action.webhookId), TypedReducer( (String selectedId, dynamic action) => action.webhook.id), TypedReducer( (selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer( (selectedId, action) => action.clearSelection ? '' : action.entityType == EntityType.webhook ? action.entityId : selectedId), ]); final editingReducer = combineReducers([ TypedReducer(_updateEditing), TypedReducer(_updateEditing), TypedReducer((webhooks, action) { return action.webhooks[0]; }), TypedReducer((webhooks, action) { return action.webhooks[0]; }), TypedReducer((webhooks, action) { return action.webhooks[0]; }), TypedReducer(_updateEditing), TypedReducer((webhook, action) { return action.webhook.rebuild((b) => b..isChanged = true); }), TypedReducer(_clearEditing), ]); WebhookEntity _clearEditing(WebhookEntity webhook, dynamic action) { return WebhookEntity(); } WebhookEntity _updateEditing(WebhookEntity webhook, dynamic action) { return action.webhook; } final webhookListReducer = combineReducers([ TypedReducer(_sortWebhooks), TypedReducer(_filterWebhooksByState), TypedReducer(_filterWebhooks), TypedReducer(_filterWebhooksByCustom1), TypedReducer(_filterWebhooksByCustom2), TypedReducer(_startListMultiselect), TypedReducer(_addToListMultiselect), TypedReducer( _removeFromListMultiselect), TypedReducer(_clearListMultiselect), TypedReducer(_viewWebhookList), ]); ListUIState _viewWebhookList( ListUIState webhookListState, ViewWebhookList action) { return webhookListState.rebuild((b) => b ..selectedIds = null ..filter = null ..filterClearedAt = DateTime.now().millisecondsSinceEpoch); } ListUIState _filterWebhooksByCustom1( ListUIState webhookListState, FilterWebhooksByCustom1 action) { if (webhookListState.custom1Filters.contains(action.value)) { return webhookListState .rebuild((b) => b..custom1Filters.remove(action.value)); } else { return webhookListState.rebuild((b) => b..custom1Filters.add(action.value)); } } ListUIState _filterWebhooksByCustom2( ListUIState webhookListState, FilterWebhooksByCustom2 action) { if (webhookListState.custom2Filters.contains(action.value)) { return webhookListState .rebuild((b) => b..custom2Filters.remove(action.value)); } else { return webhookListState.rebuild((b) => b..custom2Filters.add(action.value)); } } ListUIState _filterWebhooksByState( ListUIState webhookListState, FilterWebhooksByState action) { if (webhookListState.stateFilters.contains(action.state)) { return webhookListState .rebuild((b) => b..stateFilters.remove(action.state)); } else { return webhookListState.rebuild((b) => b..stateFilters.add(action.state)); } } ListUIState _filterWebhooks( ListUIState webhookListState, FilterWebhooks action) { return webhookListState.rebuild((b) => b ..filter = action.filter ..filterClearedAt = action.filter == null ? DateTime.now().millisecondsSinceEpoch : webhookListState.filterClearedAt); } ListUIState _sortWebhooks(ListUIState webhookListState, SortWebhooks action) { return webhookListState.rebuild((b) => b ..sortAscending = b.sortField != action.field || !b.sortAscending ..sortField = action.field); } ListUIState _startListMultiselect( ListUIState productListState, StartWebhookMultiselect action) { return productListState.rebuild((b) => b..selectedIds = ListBuilder()); } ListUIState _addToListMultiselect( ListUIState productListState, AddToWebhookMultiselect action) { return productListState.rebuild((b) => b..selectedIds.add(action.entity.id)); } ListUIState _removeFromListMultiselect( ListUIState productListState, RemoveFromWebhookMultiselect action) { return productListState .rebuild((b) => b..selectedIds.remove(action.entity.id)); } ListUIState _clearListMultiselect( ListUIState productListState, ClearWebhookMultiselect action) { return productListState.rebuild((b) => b..selectedIds = null); } final webhooksReducer = combineReducers([ TypedReducer(_updateWebhook), TypedReducer(_addWebhook), TypedReducer(_setLoadedWebhooks), TypedReducer(_setLoadedWebhook), TypedReducer(_setLoadedCompany), TypedReducer(_archiveWebhookSuccess), TypedReducer(_deleteWebhookSuccess), TypedReducer(_restoreWebhookSuccess), ]); WebhookState _archiveWebhookSuccess( WebhookState webhookState, ArchiveWebhooksSuccess action) { return webhookState.rebuild((b) { for (final webhook in action.webhooks) { b.map[webhook.id] = webhook; } }); } WebhookState _deleteWebhookSuccess( WebhookState webhookState, DeleteWebhooksSuccess action) { return webhookState.rebuild((b) { for (final webhook in action.webhooks) { b.map[webhook.id] = webhook; } }); } WebhookState _restoreWebhookSuccess( WebhookState webhookState, RestoreWebhooksSuccess action) { return webhookState.rebuild((b) { for (final webhook in action.webhooks) { b.map[webhook.id] = webhook; } }); } WebhookState _addWebhook(WebhookState webhookState, AddWebhookSuccess action) { return webhookState.rebuild((b) => b ..map[action.webhook.id] = action.webhook ..list.add(action.webhook.id)); } WebhookState _updateWebhook( WebhookState webhookState, SaveWebhookSuccess action) { return webhookState .rebuild((b) => b..map[action.webhook.id] = action.webhook); } WebhookState _setLoadedWebhook( WebhookState webhookState, LoadWebhookSuccess action) { return webhookState .rebuild((b) => b..map[action.webhook.id] = action.webhook); } WebhookState _setLoadedWebhooks( WebhookState webhookState, LoadWebhooksSuccess action) => webhookState.loadWebhooks(action.webhooks); WebhookState _setLoadedCompany( WebhookState webhookState, LoadCompanySuccess action) { final company = action.userCompany.company; return webhookState.loadWebhooks(company.webhooks); }