This commit is contained in:
unknown 2018-05-23 12:54:57 -07:00
parent b19ab9e45b
commit 269d9955a6
4 changed files with 28 additions and 34 deletions

View File

@ -21,18 +21,18 @@ AppState appReducer(AppState state, action) {
*/ */
return AppState( return AppState(
selectedCompanyId: selectedCompanyIdReducer(state.selectedCompanyId, action), selectedCompanyIndex: selectedCompanyIdReducer(state.selectedCompanyIndex, action),
isLoading: loadingReducer(state.isLoading, action), isLoading: loadingReducer(state.isLoading, action),
auth: authReducer(state.auth, action), auth: authReducer(state.auth, action),
companyState1: state.selectedCompanyId == 1 companyState1: state.selectedCompanyIndex == 1
? companyReducer(state.companyState1, action) : state.companyState1, ? companyReducer(state.companyState1, action) : state.companyState1,
companyState2: state.selectedCompanyId == 2 companyState2: state.selectedCompanyIndex == 2
? companyReducer(state.companyState2, action) : state.companyState2, ? companyReducer(state.companyState2, action) : state.companyState2,
companyState3: state.selectedCompanyId == 3 companyState3: state.selectedCompanyIndex == 3
? companyReducer(state.companyState3, action) : state.companyState3, ? companyReducer(state.companyState3, action) : state.companyState3,
companyState4: state.selectedCompanyId == 4 companyState4: state.selectedCompanyIndex == 4
? companyReducer(state.companyState4, action) : state.companyState4, ? companyReducer(state.companyState4, action) : state.companyState4,
companyState5: state.selectedCompanyId == 5 companyState5: state.selectedCompanyIndex == 5
? companyReducer(state.companyState5, action) : state.companyState5, ? companyReducer(state.companyState5, action) : state.companyState5,
); );
} }

View File

@ -9,7 +9,7 @@ import 'package:invoiceninja/redux/dashboard/dashboard_state.dart';
class AppState { class AppState {
final bool isLoading; final bool isLoading;
final AuthState auth; final AuthState auth;
final int selectedCompanyId; final int selectedCompanyIndex;
final CompanyState companyState1; final CompanyState companyState1;
final CompanyState companyState2; final CompanyState companyState2;
final CompanyState companyState3; final CompanyState companyState3;
@ -18,7 +18,7 @@ class AppState {
AppState( AppState(
{this.isLoading = false, {this.isLoading = false,
this.selectedCompanyId = 0, this.selectedCompanyIndex = 0,
AuthState auth, AuthState auth,
CompanyState companyState1, CompanyState companyState1,
CompanyState companyState2, CompanyState companyState2,
@ -44,9 +44,9 @@ class AppState {
*/ */
AppState copyWith({ AppState copyWith({
String selectedCompany,
bool isLoading, bool isLoading,
AuthState auth, AuthState auth,
int selectedCompanyIndex,
CompanyState companyState1, CompanyState companyState1,
CompanyState companyState2, CompanyState companyState2,
CompanyState companyState3, CompanyState companyState3,
@ -54,9 +54,9 @@ class AppState {
CompanyState companyState5, CompanyState companyState5,
}) { }) {
return AppState( return AppState(
selectedCompanyId : selectedCompany ?? this.selectedCompanyId,
isLoading: isLoading ?? this.isLoading, isLoading: isLoading ?? this.isLoading,
auth: auth ?? this.auth, auth: auth ?? this.auth,
selectedCompanyIndex : selectedCompanyIndex ?? this.selectedCompanyIndex,
companyState1: companyState1 ?? this.companyState1, companyState1: companyState1 ?? this.companyState1,
companyState2: companyState2 ?? this.companyState2, companyState2: companyState2 ?? this.companyState2,
companyState3: companyState3 ?? this.companyState3, companyState3: companyState3 ?? this.companyState3,
@ -67,7 +67,7 @@ class AppState {
@override @override
int get hashCode => int get hashCode =>
selectedCompanyId.hashCode ^ selectedCompanyIndex.hashCode ^
isLoading.hashCode ^ isLoading.hashCode ^
auth.hashCode ^ auth.hashCode ^
companyState1.hashCode ^ companyState1.hashCode ^
@ -81,7 +81,7 @@ class AppState {
identical(this, other) || identical(this, other) ||
other is AppState && other is AppState &&
runtimeType == other.runtimeType && runtimeType == other.runtimeType &&
selectedCompanyId == other.selectedCompanyId && selectedCompanyIndex == other.selectedCompanyIndex &&
companyState1 == other.companyState1 && companyState1 == other.companyState1 &&
companyState2 == other.companyState2 && companyState2 == other.companyState2 &&
companyState3 == other.companyState3 && companyState3 == other.companyState3 &&
@ -91,11 +91,11 @@ class AppState {
@override @override
String toString() { String toString() {
return 'AppState{isLoading: $isLoading, url: ${auth.url}, companyId: ${selectedCompanyId}, company1: ${companyState1.company.name}, company2: ${companyState2.company.name}'; return 'AppState{isLoading: $isLoading, url: ${auth.url}, companyId: ${selectedCompanyIndex}, company1: ${companyState1.company.name}, company2: ${companyState2.company.name}';
} }
CompanyState selectedCompanyState() { CompanyState selectedCompanyState() {
switch (this.selectedCompanyId) { switch (this.selectedCompanyIndex) {
case 1: case 1:
return this.companyState1; return this.companyState1;
case 2: case 2:

View File

@ -3,18 +3,16 @@ import 'package:invoiceninja/routes.dart';
import 'package:invoiceninja/data/models/entities.dart'; import 'package:invoiceninja/data/models/entities.dart';
class CustomDrawer extends StatelessWidget { class CustomDrawer extends StatelessWidget {
final String companyName;
final bool hasMultipleCompanies;
final List<CompanyEntity> companies; final List<CompanyEntity> companies;
final String selectedCompanyId; final String selectedCompanyName;
final String selectedCompanyIndex;
final Function(String) onCompanyChanged; final Function(String) onCompanyChanged;
CustomDrawer({ CustomDrawer({
Key key, Key key,
@required this.companyName,
@required this.hasMultipleCompanies,
@required this.companies, @required this.companies,
@required this.selectedCompanyId, @required this.selectedCompanyName,
@required this.selectedCompanyIndex,
@required this.onCompanyChanged, @required this.onCompanyChanged,
}) : super(key: key); }) : super(key: key);
@ -22,14 +20,14 @@ class CustomDrawer extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final _singleCompany = Align( final _singleCompany = Align(
alignment: FractionalOffset.bottomLeft, alignment: FractionalOffset.bottomLeft,
child: Text(companyName), child: Text(selectedCompanyName),
); );
final _multipleCompanies = Align( final _multipleCompanies = Align(
alignment: FractionalOffset.bottomLeft, alignment: FractionalOffset.bottomLeft,
child: new DropdownButton<String>( child: new DropdownButton<String>(
isDense: true, isDense: true,
value: this.selectedCompanyId, value: this.selectedCompanyIndex,
items: this.companies.map((CompanyEntity company) => items: this.companies.map((CompanyEntity company) =>
DropdownMenuItem<String>( DropdownMenuItem<String>(
value: (this.companies.indexOf(company) + 1).toString(), value: (this.companies.indexOf(company) + 1).toString(),

View File

@ -17,10 +17,9 @@ class CustomDrawerVM extends StatelessWidget {
converter: _ViewModel.fromStore, converter: _ViewModel.fromStore,
builder: (context, vm) { builder: (context, vm) {
return CustomDrawer( return CustomDrawer(
companyName: vm.companyName,
hasMultipleCompanies: vm.hasMultipleCompanies,
companies: vm.companies, companies: vm.companies,
selectedCompanyId: vm.selectedCompanyId, selectedCompanyName: vm.selectedCompanyName,
selectedCompanyIndex: vm.selectedCompanyIndex,
onCompanyChanged: vm.onCompanyChanged, onCompanyChanged: vm.onCompanyChanged,
); );
}, },
@ -29,26 +28,23 @@ class CustomDrawerVM extends StatelessWidget {
} }
class _ViewModel { class _ViewModel {
final String companyName;
final bool hasMultipleCompanies;
final List<CompanyEntity> companies; final List<CompanyEntity> companies;
final String selectedCompanyId; final String selectedCompanyName;
final String selectedCompanyIndex;
final Function(String) onCompanyChanged; final Function(String) onCompanyChanged;
_ViewModel({ _ViewModel({
@required this.companyName,
@required this.hasMultipleCompanies,
@required this.companies, @required this.companies,
@required this.selectedCompanyId, @required this.selectedCompanyName,
@required this.selectedCompanyIndex,
@required this.onCompanyChanged, @required this.onCompanyChanged,
}); });
static _ViewModel fromStore(Store<AppState> store) { static _ViewModel fromStore(Store<AppState> store) {
return _ViewModel( return _ViewModel(
companyName: store.state.selectedCompany().name,
hasMultipleCompanies: store.state.companyState2.company.token != null,
companies: companiesSelector(store.state), companies: companiesSelector(store.state),
selectedCompanyId: store.state.selectedCompanyId.toString(), selectedCompanyName: store.state.selectedCompany().name,
selectedCompanyIndex: store.state.selectedCompanyIndex.toString(),
onCompanyChanged: (String companyId) { onCompanyChanged: (String companyId) {
store.dispatch(SelectCompany(int.parse(companyId))); store.dispatch(SelectCompany(int.parse(companyId)));
}, },