mmmmh
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:flux/core/blocs/session/session_bloc.dart';
|
import 'package:flux/core/blocs/session/session_bloc.dart';
|
||||||
|
import 'package:flux/features/staff/models/staff_member_model.dart';
|
||||||
import 'package:flux/features/store/data/store_repository.dart';
|
import 'package:flux/features/store/data/store_repository.dart';
|
||||||
import 'package:flux/features/store/models/store_model.dart';
|
import 'package:flux/features/store/models/store_model.dart';
|
||||||
import 'package:get_it/get_it.dart';
|
import 'package:get_it/get_it.dart';
|
||||||
@@ -12,7 +13,8 @@ class StoreBloc extends Bloc<StoreEvent, StoreState> {
|
|||||||
final StoreRepository _repository = GetIt.I<StoreRepository>();
|
final StoreRepository _repository = GetIt.I<StoreRepository>();
|
||||||
final SessionBloc _sessionBloc;
|
final SessionBloc _sessionBloc;
|
||||||
|
|
||||||
StoreBloc(this._sessionBloc) : super(const StoreState(stores: [])) {
|
StoreBloc(this._sessionBloc)
|
||||||
|
: super(const StoreState(stores: [], staffByStore: {})) {
|
||||||
on<CreateStoreRequested>(_onCreateStore);
|
on<CreateStoreRequested>(_onCreateStore);
|
||||||
on<LoadStoresRequested>(_onLoadStores);
|
on<LoadStoresRequested>(_onLoadStores);
|
||||||
}
|
}
|
||||||
@@ -41,10 +43,20 @@ class StoreBloc extends Bloc<StoreEvent, StoreState> {
|
|||||||
final stores = await _repository.getStoresByCompany(
|
final stores = await _repository.getStoresByCompany(
|
||||||
_sessionBloc.state.company!.id,
|
_sessionBloc.state.company!.id,
|
||||||
);
|
);
|
||||||
|
final staffByStore = <StoreModel, List<StaffMemberModel>>{};
|
||||||
|
|
||||||
|
for (final store in stores) {
|
||||||
|
final staff = await _repository.getStaffMembersInStore(
|
||||||
|
_sessionBloc.state.company!.id,
|
||||||
|
);
|
||||||
|
staffByStore[store] = staff;
|
||||||
|
}
|
||||||
|
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
status: StoreStatus.success,
|
status: StoreStatus.success,
|
||||||
stores: stores, // Assicurati di avere 'stores' nello StoreState
|
stores: stores, // Assicurati di avere 'stores' nello StoreState
|
||||||
|
staffByStore: staffByStore,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -7,12 +7,14 @@ class StoreState extends Equatable {
|
|||||||
final StoreModel? store;
|
final StoreModel? store;
|
||||||
final String? errorMessage;
|
final String? errorMessage;
|
||||||
final List<StoreModel> stores;
|
final List<StoreModel> stores;
|
||||||
|
final Map<StoreModel, List<StaffMemberModel>> staffByStore;
|
||||||
|
|
||||||
const StoreState({
|
const StoreState({
|
||||||
this.status = StoreStatus.initial,
|
this.status = StoreStatus.initial,
|
||||||
this.store,
|
this.store,
|
||||||
this.errorMessage,
|
this.errorMessage,
|
||||||
required this.stores,
|
required this.stores,
|
||||||
|
required this.staffByStore,
|
||||||
});
|
});
|
||||||
|
|
||||||
StoreState copyWith({
|
StoreState copyWith({
|
||||||
@@ -20,15 +22,23 @@ class StoreState extends Equatable {
|
|||||||
StoreModel? store,
|
StoreModel? store,
|
||||||
String? errorMessage,
|
String? errorMessage,
|
||||||
List<StoreModel>? stores,
|
List<StoreModel>? stores,
|
||||||
|
Map<StoreModel, List<StaffMemberModel>>? staffByStore,
|
||||||
}) {
|
}) {
|
||||||
return StoreState(
|
return StoreState(
|
||||||
status: status ?? this.status,
|
status: status ?? this.status,
|
||||||
store: store ?? this.store,
|
store: store ?? this.store,
|
||||||
errorMessage: errorMessage ?? this.errorMessage,
|
errorMessage: errorMessage ?? this.errorMessage,
|
||||||
stores: stores ?? this.stores,
|
stores: stores ?? this.stores,
|
||||||
|
staffByStore: staffByStore ?? this.staffByStore,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object?> get props => [status, store, errorMessage, stores];
|
List<Object?> get props => [
|
||||||
|
status,
|
||||||
|
store,
|
||||||
|
errorMessage,
|
||||||
|
stores,
|
||||||
|
staffByStore,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import 'package:flux/core/blocs/session/session_bloc.dart';
|
|||||||
import 'package:flux/core/theme/theme.dart';
|
import 'package:flux/core/theme/theme.dart';
|
||||||
import 'package:flux/core/widgets/flux_text_field.dart';
|
import 'package:flux/core/widgets/flux_text_field.dart';
|
||||||
import 'package:flux/features/staff/blocs/staff_cubit.dart';
|
import 'package:flux/features/staff/blocs/staff_cubit.dart';
|
||||||
|
import 'package:flux/features/staff/models/staff_member_model.dart';
|
||||||
import 'package:flux/features/store/bloc/store_bloc.dart';
|
import 'package:flux/features/store/bloc/store_bloc.dart';
|
||||||
import 'package:flux/features/store/models/store_model.dart';
|
import 'package:flux/features/store/models/store_model.dart';
|
||||||
|
|
||||||
@@ -29,15 +30,16 @@ class _StoresScreenState extends State<StoresScreen> {
|
|||||||
appBar: AppBar(title: const Text("I Tuoi Negozi")),
|
appBar: AppBar(title: const Text("I Tuoi Negozi")),
|
||||||
body: BlocBuilder<StoreBloc, StoreState>(
|
body: BlocBuilder<StoreBloc, StoreState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
if (state.status == StoreStatus.loading)
|
if (state.status == StoreStatus.loading) {
|
||||||
return const Center(child: CircularProgressIndicator());
|
return const Center(child: CircularProgressIndicator());
|
||||||
|
}
|
||||||
|
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
padding: const EdgeInsets.all(16),
|
padding: const EdgeInsets.all(16),
|
||||||
itemCount: state.stores.length,
|
itemCount: state.stores.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final store = state.stores[index];
|
final store = state.stores[index];
|
||||||
return _buildStoreCard(store);
|
return _buildStoreCard(store, state.staffByStore);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -50,7 +52,10 @@ class _StoresScreenState extends State<StoresScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildStoreCard(StoreModel store) {
|
Widget _buildStoreCard(
|
||||||
|
StoreModel store,
|
||||||
|
Map<StoreModel, List<StaffMemberModel>> map,
|
||||||
|
) {
|
||||||
return Card(
|
return Card(
|
||||||
margin: const EdgeInsets.only(bottom: 16),
|
margin: const EdgeInsets.only(bottom: 16),
|
||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
@@ -89,16 +94,10 @@ class _StoresScreenState extends State<StoresScreen> {
|
|||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
children: [
|
children: [
|
||||||
// Mostra quanti dipendenti ci sono (usando lo StaffCubit)
|
// Mostra quanti dipendenti ci sono (usando lo StaffCubit)
|
||||||
BlocBuilder<StaffCubit, StaffState>(
|
ActionChip(
|
||||||
builder: (context, staffState) {
|
|
||||||
final staffCount =
|
|
||||||
staffState.staffByStore[store.id]?.length ?? 0;
|
|
||||||
return ActionChip(
|
|
||||||
avatar: const Icon(Icons.people, size: 16),
|
avatar: const Icon(Icons.people, size: 16),
|
||||||
label: Text("$staffCount Dipendenti"),
|
label: Text("${map[store]?.length ?? 0} Dipendenti"),
|
||||||
onPressed: () => _manageStoreStaff(store),
|
onPressed: () => _manageStoreStaff(store),
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
TextButton.icon(
|
TextButton.icon(
|
||||||
onPressed: () => _openStoreForm(context, store: store),
|
onPressed: () => _openStoreForm(context, store: store),
|
||||||
|
|||||||
Reference in New Issue
Block a user