Refactor
This commit is contained in:
parent
b19ab9e45b
commit
269d9955a6
|
|
@ -21,18 +21,18 @@ AppState appReducer(AppState state, action) {
|
|||
*/
|
||||
|
||||
return AppState(
|
||||
selectedCompanyId: selectedCompanyIdReducer(state.selectedCompanyId, action),
|
||||
selectedCompanyIndex: selectedCompanyIdReducer(state.selectedCompanyIndex, action),
|
||||
isLoading: loadingReducer(state.isLoading, action),
|
||||
auth: authReducer(state.auth, action),
|
||||
companyState1: state.selectedCompanyId == 1
|
||||
companyState1: state.selectedCompanyIndex == 1
|
||||
? companyReducer(state.companyState1, action) : state.companyState1,
|
||||
companyState2: state.selectedCompanyId == 2
|
||||
companyState2: state.selectedCompanyIndex == 2
|
||||
? companyReducer(state.companyState2, action) : state.companyState2,
|
||||
companyState3: state.selectedCompanyId == 3
|
||||
companyState3: state.selectedCompanyIndex == 3
|
||||
? companyReducer(state.companyState3, action) : state.companyState3,
|
||||
companyState4: state.selectedCompanyId == 4
|
||||
companyState4: state.selectedCompanyIndex == 4
|
||||
? companyReducer(state.companyState4, action) : state.companyState4,
|
||||
companyState5: state.selectedCompanyId == 5
|
||||
companyState5: state.selectedCompanyIndex == 5
|
||||
? companyReducer(state.companyState5, action) : state.companyState5,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import 'package:invoiceninja/redux/dashboard/dashboard_state.dart';
|
|||
class AppState {
|
||||
final bool isLoading;
|
||||
final AuthState auth;
|
||||
final int selectedCompanyId;
|
||||
final int selectedCompanyIndex;
|
||||
final CompanyState companyState1;
|
||||
final CompanyState companyState2;
|
||||
final CompanyState companyState3;
|
||||
|
|
@ -18,7 +18,7 @@ class AppState {
|
|||
|
||||
AppState(
|
||||
{this.isLoading = false,
|
||||
this.selectedCompanyId = 0,
|
||||
this.selectedCompanyIndex = 0,
|
||||
AuthState auth,
|
||||
CompanyState companyState1,
|
||||
CompanyState companyState2,
|
||||
|
|
@ -44,9 +44,9 @@ class AppState {
|
|||
*/
|
||||
|
||||
AppState copyWith({
|
||||
String selectedCompany,
|
||||
bool isLoading,
|
||||
AuthState auth,
|
||||
int selectedCompanyIndex,
|
||||
CompanyState companyState1,
|
||||
CompanyState companyState2,
|
||||
CompanyState companyState3,
|
||||
|
|
@ -54,9 +54,9 @@ class AppState {
|
|||
CompanyState companyState5,
|
||||
}) {
|
||||
return AppState(
|
||||
selectedCompanyId : selectedCompany ?? this.selectedCompanyId,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
auth: auth ?? this.auth,
|
||||
selectedCompanyIndex : selectedCompanyIndex ?? this.selectedCompanyIndex,
|
||||
companyState1: companyState1 ?? this.companyState1,
|
||||
companyState2: companyState2 ?? this.companyState2,
|
||||
companyState3: companyState3 ?? this.companyState3,
|
||||
|
|
@ -67,7 +67,7 @@ class AppState {
|
|||
|
||||
@override
|
||||
int get hashCode =>
|
||||
selectedCompanyId.hashCode ^
|
||||
selectedCompanyIndex.hashCode ^
|
||||
isLoading.hashCode ^
|
||||
auth.hashCode ^
|
||||
companyState1.hashCode ^
|
||||
|
|
@ -81,7 +81,7 @@ class AppState {
|
|||
identical(this, other) ||
|
||||
other is AppState &&
|
||||
runtimeType == other.runtimeType &&
|
||||
selectedCompanyId == other.selectedCompanyId &&
|
||||
selectedCompanyIndex == other.selectedCompanyIndex &&
|
||||
companyState1 == other.companyState1 &&
|
||||
companyState2 == other.companyState2 &&
|
||||
companyState3 == other.companyState3 &&
|
||||
|
|
@ -91,11 +91,11 @@ class AppState {
|
|||
|
||||
@override
|
||||
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() {
|
||||
switch (this.selectedCompanyId) {
|
||||
switch (this.selectedCompanyIndex) {
|
||||
case 1:
|
||||
return this.companyState1;
|
||||
case 2:
|
||||
|
|
|
|||
|
|
@ -3,18 +3,16 @@ import 'package:invoiceninja/routes.dart';
|
|||
import 'package:invoiceninja/data/models/entities.dart';
|
||||
|
||||
class CustomDrawer extends StatelessWidget {
|
||||
final String companyName;
|
||||
final bool hasMultipleCompanies;
|
||||
final List<CompanyEntity> companies;
|
||||
final String selectedCompanyId;
|
||||
final String selectedCompanyName;
|
||||
final String selectedCompanyIndex;
|
||||
final Function(String) onCompanyChanged;
|
||||
|
||||
CustomDrawer({
|
||||
Key key,
|
||||
@required this.companyName,
|
||||
@required this.hasMultipleCompanies,
|
||||
@required this.companies,
|
||||
@required this.selectedCompanyId,
|
||||
@required this.selectedCompanyName,
|
||||
@required this.selectedCompanyIndex,
|
||||
@required this.onCompanyChanged,
|
||||
}) : super(key: key);
|
||||
|
||||
|
|
@ -22,14 +20,14 @@ class CustomDrawer extends StatelessWidget {
|
|||
Widget build(BuildContext context) {
|
||||
final _singleCompany = Align(
|
||||
alignment: FractionalOffset.bottomLeft,
|
||||
child: Text(companyName),
|
||||
child: Text(selectedCompanyName),
|
||||
);
|
||||
|
||||
final _multipleCompanies = Align(
|
||||
alignment: FractionalOffset.bottomLeft,
|
||||
child: new DropdownButton<String>(
|
||||
isDense: true,
|
||||
value: this.selectedCompanyId,
|
||||
value: this.selectedCompanyIndex,
|
||||
items: this.companies.map((CompanyEntity company) =>
|
||||
DropdownMenuItem<String>(
|
||||
value: (this.companies.indexOf(company) + 1).toString(),
|
||||
|
|
|
|||
|
|
@ -17,10 +17,9 @@ class CustomDrawerVM extends StatelessWidget {
|
|||
converter: _ViewModel.fromStore,
|
||||
builder: (context, vm) {
|
||||
return CustomDrawer(
|
||||
companyName: vm.companyName,
|
||||
hasMultipleCompanies: vm.hasMultipleCompanies,
|
||||
companies: vm.companies,
|
||||
selectedCompanyId: vm.selectedCompanyId,
|
||||
selectedCompanyName: vm.selectedCompanyName,
|
||||
selectedCompanyIndex: vm.selectedCompanyIndex,
|
||||
onCompanyChanged: vm.onCompanyChanged,
|
||||
);
|
||||
},
|
||||
|
|
@ -29,26 +28,23 @@ class CustomDrawerVM extends StatelessWidget {
|
|||
}
|
||||
|
||||
class _ViewModel {
|
||||
final String companyName;
|
||||
final bool hasMultipleCompanies;
|
||||
final List<CompanyEntity> companies;
|
||||
final String selectedCompanyId;
|
||||
final String selectedCompanyName;
|
||||
final String selectedCompanyIndex;
|
||||
final Function(String) onCompanyChanged;
|
||||
|
||||
_ViewModel({
|
||||
@required this.companyName,
|
||||
@required this.hasMultipleCompanies,
|
||||
@required this.companies,
|
||||
@required this.selectedCompanyId,
|
||||
@required this.selectedCompanyName,
|
||||
@required this.selectedCompanyIndex,
|
||||
@required this.onCompanyChanged,
|
||||
});
|
||||
|
||||
static _ViewModel fromStore(Store<AppState> store) {
|
||||
return _ViewModel(
|
||||
companyName: store.state.selectedCompany().name,
|
||||
hasMultipleCompanies: store.state.companyState2.company.token != null,
|
||||
companies: companiesSelector(store.state),
|
||||
selectedCompanyId: store.state.selectedCompanyId.toString(),
|
||||
selectedCompanyName: store.state.selectedCompany().name,
|
||||
selectedCompanyIndex: store.state.selectedCompanyIndex.toString(),
|
||||
onCompanyChanged: (String companyId) {
|
||||
store.dispatch(SelectCompany(int.parse(companyId)));
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue