import 'dart:async'; import 'package:built_collection/built_collection.dart'; import 'package:invoiceninja_flutter/data/models/models.dart'; import 'package:invoiceninja_flutter/redux/app/app_actions.dart'; import 'package:invoiceninja_flutter/redux/client/client_actions.dart'; import 'package:invoiceninja_flutter/redux/client/client_state.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:redux/redux.dart'; EntityUIState clientUIReducer(ClientUIState state, dynamic action) { return state.rebuild((b) => b ..listUIState.replace(clientListReducer(state.listUIState, action)) ..editing.replace(editingReducer(state.editing, action)) ..editingContact .replace(editingContactReducer(state.editingContact, action)) ..selectedId = selectedIdReducer(state.selectedId, action) ..saveCompleter = saveCompleterReducer(state.saveCompleter, action) ..cancelCompleter = cancelCompleterReducer(state.cancelCompleter, action)); } final saveCompleterReducer = combineReducers>([ TypedReducer, EditClient>((completer, action) { return action.completer; }), ]); final cancelCompleterReducer = combineReducers>([ TypedReducer, EditClient>((completer, action) { return action.cancelCompleter; }), ]); final editingContactReducer = combineReducers([ TypedReducer((contact, action) { return action.contact ?? ContactEntity(); }), TypedReducer((contact, action) { return action.contact ?? ContactEntity(); }), ]); final selectedIdReducer = combineReducers([ TypedReducer((selectedId, action) { return action.clientId; }), TypedReducer((selectedId, action) { return action.client.id; }), TypedReducer( (selectedId, action) => action.entityId == null ? selectedId : '') ]); final editingReducer = combineReducers([ TypedReducer((client, action) { return action.client; }), TypedReducer((client, action) { return action.client; }), TypedReducer((clients, action) { return action.clients[0]; }), TypedReducer((clients, action) { return action.clients[0]; }), TypedReducer((clients, action) { return action.clients[0]; }), TypedReducer((client, action) { return action.client; }), TypedReducer((client, action) { return action.client.rebuild((b) => b..isChanged = true); }), TypedReducer((client, action) { return client .rebuild((b) => b..contacts.add(action.contact ?? ContactEntity())); }), TypedReducer((client, action) { return client.rebuild((b) => b..contacts.removeAt(action.index)); }), TypedReducer((client, action) { return client.rebuild((b) => b..contacts[action.index] = action.contact); }), TypedReducer((client, action) { return ClientEntity(); }), TypedReducer((client, action) { return ClientEntity(); }), TypedReducer((client, action) { return ClientEntity(); }), TypedReducer((client, action) { return ClientEntity(); }), ]); final clientListReducer = combineReducers([ TypedReducer(_sortClients), TypedReducer(_filterClientsByState), TypedReducer(_filterClients), TypedReducer(_filterClientsByEntity), TypedReducer(_filterClientsByCustom1), TypedReducer(_filterClientsByCustom2), TypedReducer(_filterClientsByCustom3), TypedReducer(_filterClientsByCustom4), TypedReducer(_startListMultiselect), TypedReducer(_addToListMultiselect), TypedReducer( _removeFromListMultiselect), TypedReducer(_clearListMultiselect), ]); ListUIState _filterClientsByCustom1( ListUIState clientListState, FilterClientsByCustom1 action) { if (clientListState.custom1Filters.contains(action.value)) { return clientListState .rebuild((b) => b..custom1Filters.remove(action.value)); } else { return clientListState.rebuild((b) => b..custom1Filters.add(action.value)); } } ListUIState _filterClientsByCustom2( ListUIState clientListState, FilterClientsByCustom2 action) { if (clientListState.custom2Filters.contains(action.value)) { return clientListState .rebuild((b) => b..custom2Filters.remove(action.value)); } else { return clientListState.rebuild((b) => b..custom2Filters.add(action.value)); } } ListUIState _filterClientsByCustom3( ListUIState clientListState, FilterClientsByCustom3 action) { if (clientListState.custom3Filters.contains(action.value)) { return clientListState .rebuild((b) => b..custom3Filters.remove(action.value)); } else { return clientListState.rebuild((b) => b..custom3Filters.add(action.value)); } } ListUIState _filterClientsByCustom4( ListUIState clientListState, FilterClientsByCustom4 action) { if (clientListState.custom4Filters.contains(action.value)) { return clientListState .rebuild((b) => b..custom4Filters.remove(action.value)); } else { return clientListState.rebuild((b) => b..custom4Filters.add(action.value)); } } ListUIState _filterClientsByState( ListUIState clientListState, FilterClientsByState action) { if (clientListState.stateFilters.contains(action.state)) { return clientListState.rebuild((b) => b..stateFilters.remove(action.state)); } else { return clientListState.rebuild((b) => b..stateFilters.add(action.state)); } } ListUIState _filterClientsByEntity( ListUIState invoiceListState, FilterClientsByEntity action) { return invoiceListState.rebuild((b) => b ..filterEntityId = action.entityId ..filterEntityType = action.entityType); } ListUIState _filterClients(ListUIState clientListState, FilterClients action) { return clientListState.rebuild((b) => b ..filter = action.filter ..filterClearedAt = action.filter == null ? DateTime.now().millisecondsSinceEpoch : clientListState.filterClearedAt); } ListUIState _sortClients(ListUIState clientListState, SortClients action) { return clientListState.rebuild((b) => b ..sortAscending = b.sortField != action.field || !b.sortAscending ..sortField = action.field); } ListUIState _startListMultiselect( ListUIState clientListState, StartClientMultiselect action) { return clientListState.rebuild((b) => b..selectedIds = ListBuilder()); } ListUIState _addToListMultiselect( ListUIState clientListState, AddToClientMultiselect action) { return clientListState.rebuild((b) => b..selectedIds.add(action.entity.id)); } ListUIState _removeFromListMultiselect( ListUIState clientListState, RemoveFromClientMultiselect action) { return clientListState .rebuild((b) => b..selectedIds.remove(action.entity.id)); } ListUIState _clearListMultiselect( ListUIState clientListState, ClearClientMultiselect action) { return clientListState.rebuild((b) => b..selectedIds = null); } final clientsReducer = combineReducers([ TypedReducer(_updateClient), TypedReducer(_addClient), TypedReducer(_setLoadedClients), TypedReducer(_setLoadedClient), TypedReducer(_companyLoaded), TypedReducer(_archiveClientRequest), TypedReducer(_archiveClientSuccess), TypedReducer(_archiveClientFailure), TypedReducer(_deleteClientRequest), TypedReducer(_deleteClientSuccess), TypedReducer(_deleteClientFailure), TypedReducer(_restoreClientRequest), TypedReducer(_restoreClientSuccess), TypedReducer(_restoreClientFailure), ]); ClientState _archiveClientRequest( ClientState clientState, ArchiveClientRequest action) { final clients = action.clientIds.map((id) => clientState.map[id]).toList(); for (int i = 0; i < clients.length; i++) { clients[i] = clients[i] .rebuild((b) => b..archivedAt = DateTime.now().millisecondsSinceEpoch); } return clientState.rebuild((b) { for (final client in clients) { b.map[client.id] = client; } }); } ClientState _archiveClientSuccess( ClientState clientState, ArchiveClientSuccess action) { return clientState.rebuild((b) { for (final client in action.clients) { b.map[client.id] = client; } }); } ClientState _archiveClientFailure( ClientState clientState, ArchiveClientFailure action) { return clientState.rebuild((b) { for (final client in action.clients) { b.map[client.id] = client; } }); } ClientState _deleteClientRequest( ClientState clientState, DeleteClientRequest action) { final clients = action.clientIds.map((id) => clientState.map[id]).toList(); for (int i = 0; i < clients.length; i++) { clients[i] = clients[i].rebuild((b) => b ..archivedAt = DateTime.now().millisecondsSinceEpoch ..isDeleted = true); } return clientState.rebuild((b) { for (final client in clients) { b.map[client.id] = client; } }); } ClientState _deleteClientSuccess( ClientState clientState, DeleteClientSuccess action) { return clientState.rebuild((b) { for (final client in action.clients) { b.map[client.id] = client; } }); } ClientState _deleteClientFailure( ClientState clientState, DeleteClientFailure action) { return clientState.rebuild((b) { for (final client in action.clients) { b.map[client.id] = client; } }); } ClientState _restoreClientRequest( ClientState clientState, RestoreClientRequest action) { final clients = action.clientIds.map((id) => clientState.map[id]).toList(); for (int i = 0; i < clients.length; i++) { clients[i] = clients[i].rebuild((b) => b ..archivedAt = null ..isDeleted = false); } return clientState.rebuild((b) { for (final client in clients) { b.map[client.id] = client; } }); } ClientState _restoreClientSuccess( ClientState clientState, RestoreClientSuccess action) { return clientState.rebuild((b) { for (final client in action.clients) { b.map[client.id] = client; } }); } ClientState _restoreClientFailure( ClientState clientState, RestoreClientFailure action) { return clientState.rebuild((b) { for (final client in action.clients) { b.map[client.id] = client; } }); } ClientState _addClient(ClientState clientState, AddClientSuccess action) { return clientState.rebuild((b) => b ..map[action.client.id] = action.client ..list.add(action.client.id)); } ClientState _updateClient(ClientState clientState, SaveClientSuccess action) { return clientState.rebuild((b) => b ..map[action.client.id] = action.client.rebuild((b) => b..lastUpdatedActivities = DateTime.now().millisecondsSinceEpoch)); } ClientState _setLoadedClient( ClientState clientState, LoadClientSuccess action) { return clientState.rebuild((b) => b ..map[action.client.id] = action.client.rebuild((b) => b..lastUpdatedActivities = DateTime.now().millisecondsSinceEpoch)); } ClientState _setLoadedClients( ClientState clientState, LoadClientsSuccess action) => clientState.loadClients(action.clients); ClientState _companyLoaded( ClientState clientState, LoadCompanySuccess action) { final clients = action.userCompany.company.clients; if (clients == null || clients.isEmpty) { return clientState; } return clientState.loadClients(clients); }