staff
This commit is contained in:
110
lib/features/staff/blocs/staff_cubit.dart
Normal file
110
lib/features/staff/blocs/staff_cubit.dart
Normal file
@@ -0,0 +1,110 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_bloc.dart';
|
||||
import 'package:flux/features/staff/data/staff_repository.dart';
|
||||
import 'package:flux/features/staff/models/staff_member_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
part 'staff_state.dart';
|
||||
|
||||
class StaffCubit extends Cubit<StaffState> {
|
||||
final StaffRepository _repository = GetIt.I.get<StaffRepository>();
|
||||
final SessionBloc _sessionBloc;
|
||||
|
||||
StaffCubit(this._sessionBloc) : super(const StaffState());
|
||||
|
||||
// Carica tutto lo staff della compagnia
|
||||
Future<void> loadAllStaff() async {
|
||||
emit(state.copyWith(isLoading: true, error: null));
|
||||
try {
|
||||
final staff = await _repository.getStaffMembers(
|
||||
_sessionBloc.state.company!.id,
|
||||
);
|
||||
emit(state.copyWith(allStaff: staff, isLoading: false));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, error: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Carica lo staff di uno specifico negozio e aggiorna la mappa
|
||||
Future<void> loadStaffForStore(String storeId) async {
|
||||
try {
|
||||
final staffInStore = await _repository.getStaffMembersInStore(storeId);
|
||||
final newMap = Map<String, List<StaffMemberModel>>.from(
|
||||
state.staffByStore,
|
||||
);
|
||||
newMap[storeId] = staffInStore;
|
||||
emit(state.copyWith(staffByStore: newMap));
|
||||
} catch (e) {
|
||||
// Qui potresti gestire l'errore silenziosamente per non bloccare tutta l'UI
|
||||
}
|
||||
}
|
||||
|
||||
// Salva o aggiorna un membro
|
||||
Future<void> saveStaffMember(StaffMemberModel member) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
await _repository.saveStaffMember(member);
|
||||
await loadAllStaff(); // Ricarichiamo la lista aggiornata
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, error: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
// Associa un dipendente a un negozio
|
||||
Future<void> assignMemberToStore(String staffId, String storeId) async {
|
||||
try {
|
||||
await _repository.assignToStore(staffId, storeId);
|
||||
await loadStaffForStore(storeId); // Aggiorna solo quel negozio nell'UI
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: "Errore nell'assegnazione: $e"));
|
||||
}
|
||||
}
|
||||
|
||||
// Rimuove un dipendente da un negozio
|
||||
Future<void> removeMemberFromStore(String staffId, String storeId) async {
|
||||
try {
|
||||
await _repository.removeFromStore(staffId, storeId);
|
||||
await loadStaffForStore(storeId);
|
||||
} catch (e) {
|
||||
emit(state.copyWith(error: "Errore nella rimozione: $e"));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveStaffWithStores({
|
||||
required StaffMemberModel member,
|
||||
required List<String> selectedStoreIds,
|
||||
}) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
try {
|
||||
// 1. Salva o aggiorna l'anagrafica (ci serve l'ID)
|
||||
// Se è un nuovo membro, Supabase ci restituirà l'ID generato
|
||||
final savedMember = await _repository.saveStaffMember(member);
|
||||
final String staffId = savedMember.id!;
|
||||
|
||||
// 2. Sincronizzazione Negozi
|
||||
// Per semplicità e pulizia, rimuoviamo le vecchie assegnazioni e inseriamo le nuove
|
||||
// (Oppure facciamo un confronto tra liste, ma il reset & rewrite è più sicuro qui)
|
||||
await _repository.clearStoreAssignments(staffId);
|
||||
|
||||
if (selectedStoreIds.isNotEmpty) {
|
||||
await Future.wait(
|
||||
selectedStoreIds.map(
|
||||
(storeId) => _repository.assignToStore(staffId, storeId),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Rinfresca i dati
|
||||
await loadAllStaff();
|
||||
// Aggiorniamo anche lo stato dei negozi coinvolti
|
||||
for (var storeId in selectedStoreIds) {
|
||||
await loadStaffForStore(storeId);
|
||||
}
|
||||
|
||||
emit(state.copyWith(isLoading: false));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, error: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
33
lib/features/staff/blocs/staff_state.dart
Normal file
33
lib/features/staff/blocs/staff_state.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
part of 'staff_cubit.dart';
|
||||
|
||||
class StaffState extends Equatable {
|
||||
final List<StaffMemberModel> allStaff;
|
||||
final Map<String, List<StaffMemberModel>>
|
||||
staffByStore; // storeId -> List of staff
|
||||
final bool isLoading;
|
||||
final String? error;
|
||||
|
||||
const StaffState({
|
||||
this.allStaff = const [],
|
||||
this.staffByStore = const {},
|
||||
this.isLoading = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
StaffState copyWith({
|
||||
List<StaffMemberModel>? allStaff,
|
||||
Map<String, List<StaffMemberModel>>? staffByStore,
|
||||
bool? isLoading,
|
||||
String? error,
|
||||
}) {
|
||||
return StaffState(
|
||||
allStaff: allStaff ?? this.allStaff,
|
||||
staffByStore: staffByStore ?? this.staffByStore,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
error: error,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [allStaff, staffByStore, isLoading, error];
|
||||
}
|
||||
Reference in New Issue
Block a user