Refactor StoreBloc to Cubit and Fix Staff Assignment UI (#1)
Convertito StoreBloc in StoreCubit per coerenza con il resto del progetto. Sistemata la logica di assegnazione dipendenti nel modal dei negozi. Utilizzato il doppio BlocBuilder per garantire la reattività tra StaffCubit e StoreCubit. Reviewed-on: http://catelliub.zapto.org:3000/brontomark/flux/pulls/1 Co-authored-by: Mark M2 Macbook <marco@catelli.it> Co-committed-by: Mark M2 Macbook <marco@catelli.it>
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
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/master_data/store/data/store_repository.dart';
|
||||
import 'package:flux/features/master_data/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: [])) {
|
||||
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,
|
||||
);
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: StoreStatus.success,
|
||||
stores: stores, // Assicurati di avere 'stores' nello StoreState
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(status: StoreStatus.failure, errorMessage: e.toString()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
97
lib/features/master_data/store/bloc/store_cubit.dart
Normal file
97
lib/features/master_data/store/bloc/store_cubit.dart
Normal file
@@ -0,0 +1,97 @@
|
||||
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/master_data/staff/data/staff_repository.dart';
|
||||
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
||||
import 'package:flux/features/master_data/store/data/store_repository.dart';
|
||||
import 'package:flux/features/master_data/store/models/store_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
part 'store_state.dart';
|
||||
|
||||
class StoreCubit extends Cubit<StoreState> {
|
||||
final StoreRepository _repository = GetIt.I<StoreRepository>();
|
||||
final StaffRepository _staffRepository = GetIt.I<StaffRepository>();
|
||||
final SessionBloc _sessionBloc;
|
||||
|
||||
StoreCubit(this._sessionBloc) : super(const StoreState(stores: []));
|
||||
|
||||
Future<void> createStore(final StoreModel store) async {
|
||||
emit(state.copyWith(status: StoreStatus.loading));
|
||||
try {
|
||||
await _repository.createStore(store);
|
||||
emit(state.copyWith(status: StoreStatus.success));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(status: StoreStatus.failure, errorMessage: e.toString()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> loadStores() async {
|
||||
emit(state.copyWith(status: StoreStatus.loading));
|
||||
try {
|
||||
final stores = await _repository.getStoresByCompany(
|
||||
_sessionBloc.state.company!.id,
|
||||
);
|
||||
final Map<String, List<StaffMemberModel>> staffByStore = {};
|
||||
for (StoreModel store in stores) {
|
||||
staffByStore[store.id!] = await _staffRepository.getStaffMembersInStore(
|
||||
store.id!,
|
||||
);
|
||||
}
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: StoreStatus.success,
|
||||
stores: stores,
|
||||
staffByStore: staffByStore,
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(status: StoreStatus.failure, errorMessage: e.toString()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> assignStaffToStore(String storeId, String staffId) async {
|
||||
try {
|
||||
await _staffRepository.assignToStore(staffId, storeId);
|
||||
// Dopo l'assegnazione, potresti voler ricaricare lo staff per quel negozio
|
||||
final updatedStaff = await _staffRepository.getStaffMembersInStore(
|
||||
storeId,
|
||||
);
|
||||
final newMap = Map<String, List<StaffMemberModel>>.from(
|
||||
state.staffByStore,
|
||||
);
|
||||
newMap[storeId] = updatedStaff;
|
||||
emit(state.copyWith(status: StoreStatus.success, staffByStore: newMap));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(status: StoreStatus.failure, errorMessage: e.toString()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Rimuove un dipendente da un negozio
|
||||
Future<void> removeStaffFromStore(String staffId, String storeId) async {
|
||||
try {
|
||||
await _staffRepository.removeFromStore(staffId, storeId);
|
||||
final updatedStaff = await _staffRepository.getStaffMembersInStore(
|
||||
storeId,
|
||||
);
|
||||
final newMap = Map<String, List<StaffMemberModel>>.from(
|
||||
state.staffByStore,
|
||||
);
|
||||
newMap[storeId] = updatedStaff;
|
||||
emit(state.copyWith(staffByStore: newMap));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: StoreStatus.failure,
|
||||
errorMessage: "Errore nella rimozione: $e",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
part of 'store_bloc.dart';
|
||||
|
||||
abstract class StoreEvent extends Equatable {
|
||||
const StoreEvent();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class CreateStoreRequested extends StoreEvent {
|
||||
final StoreModel store;
|
||||
const CreateStoreRequested({required this.store});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [store];
|
||||
}
|
||||
|
||||
class LoadStoresRequested extends StoreEvent {}
|
||||
@@ -1,4 +1,4 @@
|
||||
part of 'store_bloc.dart';
|
||||
part of 'store_cubit.dart';
|
||||
|
||||
enum StoreStatus { initial, loading, success, failure }
|
||||
|
||||
@@ -7,12 +7,14 @@ class StoreState extends Equatable {
|
||||
final StoreModel? store;
|
||||
final String? errorMessage;
|
||||
final List<StoreModel> stores;
|
||||
final Map<String, List<StaffMemberModel>> staffByStore;
|
||||
|
||||
const StoreState({
|
||||
this.status = StoreStatus.initial,
|
||||
this.store,
|
||||
this.errorMessage,
|
||||
required this.stores,
|
||||
this.staffByStore = const {},
|
||||
});
|
||||
|
||||
StoreState copyWith({
|
||||
@@ -20,15 +22,23 @@ class StoreState extends Equatable {
|
||||
StoreModel? store,
|
||||
String? errorMessage,
|
||||
List<StoreModel>? stores,
|
||||
Map<String, 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,
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user