urca non ci credo, potrebbe già funzionare

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-26 16:29:31 +02:00
parent 1d3a685368
commit 6cbf0479a1
9 changed files with 297 additions and 101 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:math';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/blocs/session/session_cubit.dart';
@@ -16,7 +18,7 @@ class StaffCubit extends Cubit<StaffState> {
// Carica tutto lo staff della compagnia
Future<void> loadAllStaff() async {
emit(state.copyWith(isLoading: true, error: null));
emit(state.copyWith(status: StaffStatus.loading, error: null));
try {
final staff = await _repository.getStaffMembers(
_sessionCubit.state.company!.id!,
@@ -27,18 +29,19 @@ class StaffCubit extends Cubit<StaffState> {
}
emit(
state.copyWith(
status: StaffStatus.success,
allStaff: staff,
isLoading: false,
storesByStaff: storesByStaff,
),
);
} catch (e) {
emit(state.copyWith(isLoading: false, error: e.toString()));
emit(state.copyWith(status: StaffStatus.error, error: e.toString()));
}
}
Future<List<StoreModel>> loadStoresByStaff(String staffId) async {
try {
emit(state.copyWith(error: null));
return await _repository.getStaffMemberStore(staffId);
} catch (e) {
emit(state.copyWith(error: e.toString()));
@@ -48,6 +51,7 @@ class StaffCubit extends Cubit<StaffState> {
// Carica lo staff di uno specifico negozio e aggiorna la mappa
Future<void> loadStaffForStore(String storeId) async {
emit(state.copyWith(error: null));
try {
final staffInStore = await _repository.getStaffMembersInStore(storeId);
final newMap = Map<String, List<StaffMemberModel>>.from(
@@ -56,48 +60,87 @@ class StaffCubit extends Cubit<StaffState> {
newMap[storeId] = staffInStore;
emit(state.copyWith(staffByStore: newMap));
} catch (e) {
// Qui potresti gestire l'errore silenziosamente per non bloccare tutta l'UI
emit(state.copyWith(status: StaffStatus.error, error: e.toString()));
}
}
// Salva o aggiorna un membro
Future<void> saveStaffMember(StaffMemberModel member) async {
emit(state.copyWith(isLoading: true));
emit(state.copyWith(status: StaffStatus.loading, error: null));
try {
await _repository.saveStaffMember(member);
await loadAllStaff(); // Ricarichiamo la lista aggiornata
} catch (e) {
emit(state.copyWith(isLoading: false, error: e.toString()));
emit(state.copyWith(status: StaffStatus.error, error: e.toString()));
}
}
Future<void> inviteStaffMember({
required StaffMemberModel member,
required List<String> selectedStoreIds,
}) async {
emit(state.copyWith(status: StaffStatus.loading, error: null));
try {
// 1. Invitiamo il membro e ci facciamo dare l'ID
final newStaffId = await _repository.inviteStaffMember(member);
// 2. Assegniamo i negozi uno ad uno (usando il metodo che avevi già nel repo!)
if (selectedStoreIds.isNotEmpty) {
final List<Future> assignTasks = [];
for (var storeId in selectedStoreIds) {
assignTasks.add(_repository.assignStaffToStore(newStaffId, storeId));
}
await Future.wait(assignTasks); // In parallelo per la massima velocità
}
// 3. Ricarichiamo la lista globale così la UI si aggiorna
await loadAllStaff();
// (Nota: se hai un loadStaffForStore o loadAllStaff, chiamalo qui per rinfrescare lo stato)
emit(state.copyWith(status: StaffStatus.success));
} catch (e) {
emit(state.copyWith(status: StaffStatus.error, error: e.toString()));
}
}
// Associa un dipendente a un negozio
Future<void> assignMemberToStore(String staffId, String storeId) async {
try {
await _repository.assignToStore(staffId, storeId);
await _repository.assignStaffToStore(staffId, storeId);
final stuffStores = await loadStoresByStaff(staffId);
final Map<String, List<StoreModel>> storesByStaff = Map.from(
state.storesByStaff,
);
storesByStaff[staffId] = stuffStores;
emit(state.copyWith(storesByStaff: storesByStaff));
emit(state.copyWith(storesByStaff: storesByStaff, error: null));
} catch (e) {
emit(state.copyWith(error: "Errore nell'assegnazione: $e"));
emit(
state.copyWith(
status: StaffStatus.error,
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 _repository.removeStaffFromStore(staffId, storeId);
final stuffStores = await loadStoresByStaff(staffId);
final Map<String, List<StoreModel>> storesByStaff = Map.from(
state.storesByStaff,
);
storesByStaff[staffId] = stuffStores;
emit(state.copyWith(storesByStaff: storesByStaff));
emit(state.copyWith(storesByStaff: storesByStaff, error: null));
} catch (e) {
emit(state.copyWith(error: "Errore nella rimozione: $e"));
emit(
state.copyWith(
status: StaffStatus.error,
error: "Errore nella rimozione: $e",
),
);
}
}
@@ -105,7 +148,7 @@ class StaffCubit extends Cubit<StaffState> {
required StaffMemberModel member,
required List<String> selectedStoreIds,
}) async {
emit(state.copyWith(isLoading: true));
emit(state.copyWith(status: StaffStatus.loading, error: null));
try {
// 1. Salva o aggiorna l'anagrafica (ci serve l'ID)
// Se è un nuovo membro, Supabase ci restituirà l'ID generato
@@ -120,7 +163,7 @@ class StaffCubit extends Cubit<StaffState> {
if (selectedStoreIds.isNotEmpty) {
await Future.wait(
selectedStoreIds.map(
(storeId) => _repository.assignToStore(staffId, storeId),
(storeId) => _repository.assignStaffToStore(staffId, storeId),
),
);
}
@@ -128,9 +171,9 @@ class StaffCubit extends Cubit<StaffState> {
// 3. Rinfresca i dati
await loadAllStaff();
emit(state.copyWith(isLoading: false));
emit(state.copyWith(status: StaffStatus.success));
} catch (e) {
emit(state.copyWith(isLoading: false, error: e.toString()));
emit(state.copyWith(status: StaffStatus.error, error: e.toString()));
}
}
}

View File

@@ -1,42 +1,44 @@
part of 'staff_cubit.dart';
enum StaffStatus { initial, loading, success, error }
class StaffState extends Equatable {
final StaffStatus status;
final List<StaffMemberModel> allStaff;
final Map<String, List<StoreModel>> storesByStaff;
final Map<String, List<StaffMemberModel>> staffByStore;
final bool isLoading;
final String? error;
const StaffState({
this.status = StaffStatus.initial,
this.allStaff = const [],
this.storesByStaff = const {},
this.staffByStore = const {},
this.isLoading = false,
this.error,
});
StaffState copyWith({
StaffStatus? status,
List<StaffMemberModel>? allStaff,
Map<String, List<StoreModel>>? storesByStaff,
Map<String, List<StaffMemberModel>>? staffByStore,
bool? isLoading,
String? error,
}) {
return StaffState(
status: status ?? this.status,
allStaff: allStaff ?? this.allStaff,
storesByStaff: storesByStaff ?? this.storesByStaff,
staffByStore: staffByStore ?? this.staffByStore,
isLoading: isLoading ?? this.isLoading,
error: error,
);
}
@override
List<Object?> get props => [
status,
allStaff,
storesByStaff,
staffByStore,
isLoading,
error,
];
}