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/token/token_actions.dart'; import 'package:invoiceninja_flutter/redux/ui/list_ui_state.dart'; import 'package:invoiceninja_flutter/redux/token/token_state.dart'; import 'package:invoiceninja_flutter/data/models/entities.dart'; EntityUIState tokenUIReducer(TokenUIState state, dynamic action) { return state.rebuild((b) => b ..listUIState.replace(tokenListReducer(state.listUIState, action)) ..editing.replace(editingReducer(state.editing, action)) ..selectedId = selectedIdReducer(state.selectedId, action)); } Reducer selectedIdReducer = combineReducers([ TypedReducer( (String selectedId, dynamic action) => action.tokenId), TypedReducer( (String selectedId, dynamic action) => action.token.id), TypedReducer((selectedId, action) => action.clearSelection ? '' : selectedId), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => ''), TypedReducer((selectedId, action) => action.entityType == EntityType.token ? action.entityId : selectedId), ]); final editingReducer = combineReducers([ TypedReducer(_updateEditing), TypedReducer(_updateEditing), TypedReducer((tokens, action) { return action.tokens[0]; }), TypedReducer((tokens, action) { return action.tokens[0]; }), TypedReducer((tokens, action) { return action.tokens[0]; }), TypedReducer(_updateEditing), TypedReducer((token, action) { return action.token.rebuild((b) => b..isChanged = true); }), TypedReducer(_clearEditing), TypedReducer(_clearEditing), ]); TokenEntity _clearEditing(TokenEntity token, dynamic action) { return TokenEntity(); } TokenEntity _updateEditing(TokenEntity token, dynamic action) { return action.token; } final tokenListReducer = combineReducers([ TypedReducer(_sortTokens), TypedReducer(_filterTokensByState), TypedReducer(_filterTokens), TypedReducer(_filterTokensByCustom1), TypedReducer(_filterTokensByCustom2), TypedReducer(_startListMultiselect), TypedReducer(_addToListMultiselect), TypedReducer( _removeFromListMultiselect), TypedReducer(_clearListMultiselect), TypedReducer( (state, action) => state.rebuild((b) => b ..filterEntityId = null ..filterEntityType = null)), ]); ListUIState _filterTokensByCustom1( ListUIState tokenListState, FilterTokensByCustom1 action) { if (tokenListState.custom1Filters.contains(action.value)) { return tokenListState .rebuild((b) => b..custom1Filters.remove(action.value)); } else { return tokenListState.rebuild((b) => b..custom1Filters.add(action.value)); } } ListUIState _filterTokensByCustom2( ListUIState tokenListState, FilterTokensByCustom2 action) { if (tokenListState.custom2Filters.contains(action.value)) { return tokenListState .rebuild((b) => b..custom2Filters.remove(action.value)); } else { return tokenListState.rebuild((b) => b..custom2Filters.add(action.value)); } } ListUIState _filterTokensByState( ListUIState tokenListState, FilterTokensByState action) { if (tokenListState.stateFilters.contains(action.state)) { return tokenListState.rebuild((b) => b..stateFilters.remove(action.state)); } else { return tokenListState.rebuild((b) => b..stateFilters.add(action.state)); } } ListUIState _filterTokens(ListUIState tokenListState, FilterTokens action) { return tokenListState.rebuild((b) => b ..filter = action.filter ..filterClearedAt = action.filter == null ? DateTime.now().millisecondsSinceEpoch : tokenListState.filterClearedAt); } ListUIState _sortTokens(ListUIState tokenListState, SortTokens action) { return tokenListState.rebuild((b) => b ..sortAscending = b.sortField != action.field || !b.sortAscending ..sortField = action.field); } ListUIState _startListMultiselect( ListUIState productListState, StartTokenMultiselect action) { return productListState.rebuild((b) => b..selectedIds = ListBuilder()); } ListUIState _addToListMultiselect( ListUIState productListState, AddToTokenMultiselect action) { return productListState.rebuild((b) => b..selectedIds.add(action.entity.id)); } ListUIState _removeFromListMultiselect( ListUIState productListState, RemoveFromTokenMultiselect action) { return productListState .rebuild((b) => b..selectedIds.remove(action.entity.id)); } ListUIState _clearListMultiselect( ListUIState productListState, ClearTokenMultiselect action) { return productListState.rebuild((b) => b..selectedIds = null); } final tokensReducer = combineReducers([ TypedReducer(_updateToken), TypedReducer(_addToken), TypedReducer(_setLoadedTokens), TypedReducer(_setLoadedToken), TypedReducer(_setLoadedCompany), TypedReducer(_archiveTokenRequest), TypedReducer(_archiveTokenSuccess), TypedReducer(_archiveTokenFailure), TypedReducer(_deleteTokenRequest), TypedReducer(_deleteTokenSuccess), TypedReducer(_deleteTokenFailure), TypedReducer(_restoreTokenRequest), TypedReducer(_restoreTokenSuccess), TypedReducer(_restoreTokenFailure), ]); TokenState _archiveTokenRequest( TokenState tokenState, ArchiveTokensRequest action) { final tokens = action.tokenIds.map((id) => tokenState.map[id]).toList(); for (int i = 0; i < tokens.length; i++) { tokens[i] = tokens[i] .rebuild((b) => b..archivedAt = DateTime.now().millisecondsSinceEpoch); } return tokenState.rebuild((b) { for (final token in tokens) { b.map[token.id] = token; } }); } TokenState _archiveTokenSuccess( TokenState tokenState, ArchiveTokensSuccess action) { return tokenState.rebuild((b) { for (final token in action.tokens) { b.map[token.id] = token; } }); } TokenState _archiveTokenFailure( TokenState tokenState, ArchiveTokensFailure action) { return tokenState.rebuild((b) { for (final token in action.tokens) { b.map[token.id] = token; } }); } TokenState _deleteTokenRequest( TokenState tokenState, DeleteTokensRequest action) { final tokens = action.tokenIds.map((id) => tokenState.map[id]).toList(); for (int i = 0; i < tokens.length; i++) { tokens[i] = tokens[i].rebuild((b) => b ..archivedAt = DateTime.now().millisecondsSinceEpoch ..isDeleted = true); } return tokenState.rebuild((b) { for (final token in tokens) { b.map[token.id] = token; } }); } TokenState _deleteTokenSuccess( TokenState tokenState, DeleteTokensSuccess action) { return tokenState.rebuild((b) { for (final token in action.tokens) { b.map[token.id] = token; } }); } TokenState _deleteTokenFailure( TokenState tokenState, DeleteTokensFailure action) { return tokenState.rebuild((b) { for (final token in action.tokens) { b.map[token.id] = token; } }); } TokenState _restoreTokenRequest( TokenState tokenState, RestoreTokensRequest action) { final tokens = action.tokenIds.map((id) => tokenState.map[id]).toList(); for (int i = 0; i < tokens.length; i++) { tokens[i] = tokens[i].rebuild((b) => b ..archivedAt = 0 ..isDeleted = false); } return tokenState.rebuild((b) { for (final token in tokens) { b.map[token.id] = token; } }); } TokenState _restoreTokenSuccess( TokenState tokenState, RestoreTokensSuccess action) { return tokenState.rebuild((b) { for (final token in action.tokens) { b.map[token.id] = token; } }); } TokenState _restoreTokenFailure( TokenState tokenState, RestoreTokensFailure action) { return tokenState.rebuild((b) { for (final token in action.tokens) { b.map[token.id] = token; } }); } TokenState _addToken(TokenState tokenState, AddTokenSuccess action) { return tokenState.rebuild((b) => b ..map[action.token.id] = action.token ..list.add(action.token.id)); } TokenState _updateToken(TokenState tokenState, SaveTokenSuccess action) { return tokenState.rebuild((b) => b..map[action.token.id] = action.token); } TokenState _setLoadedToken(TokenState tokenState, LoadTokenSuccess action) { return tokenState.rebuild((b) => b..map[action.token.id] = action.token); } TokenState _setLoadedTokens(TokenState tokenState, LoadTokensSuccess action) => tokenState.loadTokens(action.tokens); TokenState _setLoadedCompany(TokenState tokenState, LoadCompanySuccess action) { final company = action.userCompany.company; return tokenState.loadTokens(company.tokens); }