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