ripristinato codice stabile da branch staff
This commit is contained in:
56
lib/features/master_data/store/bloc/store_bloc.dart
Normal file
56
lib/features/master_data/store/bloc/store_bloc.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
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()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
18
lib/features/master_data/store/bloc/store_events.dart
Normal file
18
lib/features/master_data/store/bloc/store_events.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
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 {}
|
||||
34
lib/features/master_data/store/bloc/store_state.dart
Normal file
34
lib/features/master_data/store/bloc/store_state.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
part of 'store_bloc.dart';
|
||||
|
||||
enum StoreStatus { initial, loading, success, failure }
|
||||
|
||||
class StoreState extends Equatable {
|
||||
final StoreStatus status;
|
||||
final StoreModel? store;
|
||||
final String? errorMessage;
|
||||
final List<StoreModel> stores;
|
||||
|
||||
const StoreState({
|
||||
this.status = StoreStatus.initial,
|
||||
this.store,
|
||||
this.errorMessage,
|
||||
required this.stores,
|
||||
});
|
||||
|
||||
StoreState copyWith({
|
||||
StoreStatus? status,
|
||||
StoreModel? store,
|
||||
String? errorMessage,
|
||||
List<StoreModel>? stores,
|
||||
}) {
|
||||
return StoreState(
|
||||
status: status ?? this.status,
|
||||
store: store ?? this.store,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
stores: stores ?? this.stores,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, store, errorMessage, stores];
|
||||
}
|
||||
Reference in New Issue
Block a user