Files
flux/lib/features/store/bloc/store_bloc.dart
2026-04-13 19:27:23 +02:00

69 lines
2.0 KiB
Dart

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';
part 'store_events.dart';
part 'store_state.dart';
class StoreBloc extends Bloc<StoreEvent, StoreState> {
final StoreRepository _repository = GetIt.I<StoreRepository>();
final SessionBloc _sessionBloc;
StoreBloc(this._sessionBloc)
: super(const StoreState(stores: [], staffByStore: {})) {
on<CreateStoreRequested>(_onCreateStore);
on<LoadStoresRequested>(_onLoadStores);
}
Future<void> _onCreateStore(
CreateStoreRequested event,
Emitter<StoreState> emit,
) async {
emit(state.copyWith(status: StoreStatus.loading));
try {
await _repository.createStore(event.store);
emit(state.copyWith(status: StoreStatus.success));
} catch (e) {
emit(
state.copyWith(status: StoreStatus.failure, errorMessage: e.toString()),
);
}
}
Future<void> _onLoadStores(
LoadStoresRequested event,
Emitter<StoreState> emit,
) async {
emit(state.copyWith(status: StoreStatus.loading));
try {
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) {
emit(
state.copyWith(status: StoreStatus.failure, errorMessage: e.toString()),
);
}
}
}