creazione nuovo negozio
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/enums/enums.dart';
|
||||
import 'package:flux/features/settings/settings.dart';
|
||||
import 'package:flux/features/company/models/company_model.dart';
|
||||
import 'package:flux/features/store/models/store_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'dart:async';
|
||||
@@ -14,7 +15,7 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
||||
final SupabaseClient _supabase = GetIt.I.get<SupabaseClient>();
|
||||
StreamSubscription<AuthState>? _authSubscription;
|
||||
|
||||
SessionBloc() : super(const SessionState.unknown()) {
|
||||
SessionBloc() : super(const SessionState(status: SessionStatus.unknown)) {
|
||||
on<AppStarted>((event, emit) {
|
||||
// 1. Controlla la sessione attuale al boot
|
||||
final session = _supabase.auth.currentSession;
|
||||
@@ -32,33 +33,47 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
||||
});
|
||||
|
||||
on<UserChanged>((event, emit) async {
|
||||
GetIt.I.get<AppSettings>().setCurrentUserId(event.userId);
|
||||
if (event.userId == null) {
|
||||
emit(SessionState.unauthenticated());
|
||||
emit(SessionState(status: SessionStatus.unauthenticated));
|
||||
return;
|
||||
}
|
||||
// 1. Controlla se l'utente ha una Company
|
||||
final company = await _supabase
|
||||
final companyJson = await _supabase
|
||||
.from('company')
|
||||
.select()
|
||||
.eq('user_id', event.userId!)
|
||||
.maybeSingle();
|
||||
|
||||
if (company == null) {
|
||||
emit(SessionState.authenticatedNoCompany(event.userId!));
|
||||
if (companyJson == null) {
|
||||
emit(
|
||||
SessionState(
|
||||
status: SessionStatus.authenticatedNoCompany,
|
||||
userId: event.userId,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
CompanyModel company = CompanyModel.fromJson(companyJson);
|
||||
|
||||
// 2. Controlla i negozi
|
||||
final stores = await _supabase
|
||||
.from('store')
|
||||
.select()
|
||||
.eq('company_id', company['id']);
|
||||
.eq('company_id', companyJson['id']);
|
||||
|
||||
if (stores.isEmpty) {
|
||||
emit(SessionState.authenticatedNoStore(event.userId!, company['id']));
|
||||
emit(
|
||||
SessionState(
|
||||
status: SessionStatus.authenticatedNoStore,
|
||||
userId: event.userId,
|
||||
company: company,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
final availableStores = stores
|
||||
.map((s) => StoreModel.fromJson(s))
|
||||
.toList();
|
||||
|
||||
// 3. Tutto ok, gestiamo le SharedPreferences per il negozio
|
||||
final prefs = GetIt.I.get<SharedPreferences>();
|
||||
@@ -69,6 +84,18 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
||||
lastStoreId = stores.first['id'];
|
||||
await prefs.setString('last_store_id', lastStoreId!);
|
||||
}
|
||||
final selectedStore = StoreModel.fromJson(
|
||||
stores.firstWhere((s) => s['id'] == lastStoreId),
|
||||
);
|
||||
emit(
|
||||
SessionState(
|
||||
status: SessionStatus.ready,
|
||||
userId: event.userId,
|
||||
company: company,
|
||||
selectedStore: selectedStore,
|
||||
availableStores: availableStores,
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -78,5 +105,3 @@ class SessionBloc extends Bloc<SessionEvent, SessionState> {
|
||||
return super.close();
|
||||
}
|
||||
}
|
||||
|
||||
class SharedPreferencesKeys {}
|
||||
|
||||
@@ -11,27 +11,41 @@ enum SessionStatus {
|
||||
class SessionState extends Equatable {
|
||||
final SessionStatus status;
|
||||
final String? userId;
|
||||
final String? companyId;
|
||||
final CompanyModel? company;
|
||||
final StoreModel? selectedStore;
|
||||
final List<StoreModel> availableStores; // Utile per uno switcher in futuro
|
||||
|
||||
const SessionState._({
|
||||
const SessionState({
|
||||
this.status = SessionStatus.unknown,
|
||||
this.userId,
|
||||
this.companyId,
|
||||
this.company,
|
||||
this.selectedStore,
|
||||
this.availableStores = const [],
|
||||
});
|
||||
const SessionState.unknown() : this._();
|
||||
const SessionState.unauthenticated()
|
||||
: this._(status: SessionStatus.unauthenticated);
|
||||
const SessionState.authenticatedNoCompany(String userId)
|
||||
: this._(status: SessionStatus.authenticatedNoCompany, userId: userId);
|
||||
const SessionState.authenticatedNoStore(String userId, String companyId)
|
||||
: this._(
|
||||
status: SessionStatus.authenticatedNoStore,
|
||||
userId: userId,
|
||||
companyId: companyId,
|
||||
);
|
||||
const SessionState.ready(String userId)
|
||||
: this._(status: SessionStatus.ready, userId: userId);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, userId];
|
||||
List<Object?> get props => [
|
||||
status,
|
||||
userId,
|
||||
company,
|
||||
selectedStore,
|
||||
availableStores,
|
||||
];
|
||||
|
||||
// copyWith per aggiornare solo un pezzo (es. quando cambi negozio)
|
||||
SessionState copyWith({
|
||||
SessionStatus? status,
|
||||
String? userId,
|
||||
CompanyModel? company,
|
||||
StoreModel? selectedStore,
|
||||
List<StoreModel>? availableStores,
|
||||
}) {
|
||||
return SessionState(
|
||||
status: status ?? this.status,
|
||||
userId: userId ?? this.userId,
|
||||
company: company ?? this.company,
|
||||
selectedStore: selectedStore ?? this.selectedStore,
|
||||
availableStores: availableStores ?? this.availableStores,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user