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 {}
|
||||
|
||||
Reference in New Issue
Block a user