refined responsive ui for dashboard and start customer work
This commit is contained in:
82
lib/features/customers/blocs/customer_bloc.dart
Normal file
82
lib/features/customers/blocs/customer_bloc.dart
Normal file
@@ -0,0 +1,82 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/features/customers/data/customer_repository.dart';
|
||||
import 'package:flux/features/customers/models/customer_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
part 'customer_events.dart';
|
||||
part 'customer_state.dart';
|
||||
|
||||
class CustomerBloc extends Bloc<CustomerEvent, CustomerState> {
|
||||
final CustomerRepository _repository = GetIt.I<CustomerRepository>();
|
||||
|
||||
CustomerBloc() : super(const CustomerState()) {
|
||||
on<LoadCustomersRequested>(_onLoadCustomers);
|
||||
on<CreateCustomerRequested>(_onCreateCustomer);
|
||||
on<SearchCustomersRequested>(_onSearchCustomers);
|
||||
}
|
||||
|
||||
Future<void> _onLoadCustomers(
|
||||
LoadCustomersRequested event,
|
||||
Emitter<CustomerState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: CustomerStatus.loading));
|
||||
try {
|
||||
final customers = await _repository.getCustomers(event.companyId);
|
||||
emit(
|
||||
state.copyWith(status: CustomerStatus.success, customers: customers),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onCreateCustomer(
|
||||
CreateCustomerRequested event,
|
||||
Emitter<CustomerState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: CustomerStatus.loading));
|
||||
try {
|
||||
final newCustomer = await _repository.createCustomer(event.customer);
|
||||
|
||||
// Aggiorniamo la lista locale aggiungendo il nuovo cliente in cima
|
||||
final updatedList = List<CustomerModel>.from(state.customers)
|
||||
..insert(0, newCustomer);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.success,
|
||||
customers: updatedList,
|
||||
lastCreatedCustomer:
|
||||
newCustomer, // Lo passiamo per le Dialog "al volo"
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: CustomerStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSearchCustomers(
|
||||
SearchCustomersRequested event,
|
||||
Emitter<CustomerState> emit,
|
||||
) async {
|
||||
// Non mettiamo loading per evitare flickering durante la digitazione
|
||||
try {
|
||||
final results = await _repository.searchCustomers(
|
||||
event.companyId,
|
||||
event.query,
|
||||
);
|
||||
emit(state.copyWith(status: CustomerStatus.success, customers: results));
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
26
lib/features/customers/blocs/customer_events.dart
Normal file
26
lib/features/customers/blocs/customer_events.dart
Normal file
@@ -0,0 +1,26 @@
|
||||
part of 'customer_bloc.dart';
|
||||
|
||||
abstract class CustomerEvent extends Equatable {
|
||||
const CustomerEvent();
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
// Carica tutti i clienti dell'azienda
|
||||
class LoadCustomersRequested extends CustomerEvent {
|
||||
final String companyId;
|
||||
const LoadCustomersRequested(this.companyId);
|
||||
}
|
||||
|
||||
// Crea un cliente (usato sia dalla lista che dalla Dialog operazioni)
|
||||
class CreateCustomerRequested extends CustomerEvent {
|
||||
final CustomerModel customer;
|
||||
const CreateCustomerRequested(this.customer);
|
||||
}
|
||||
|
||||
// Ricerca in tempo reale
|
||||
class SearchCustomersRequested extends CustomerEvent {
|
||||
final String companyId;
|
||||
final String query;
|
||||
const SearchCustomersRequested(this.companyId, this.query);
|
||||
}
|
||||
40
lib/features/customers/blocs/customer_state.dart
Normal file
40
lib/features/customers/blocs/customer_state.dart
Normal file
@@ -0,0 +1,40 @@
|
||||
part of 'customer_bloc.dart';
|
||||
|
||||
enum CustomerStatus { initial, loading, success, failure }
|
||||
|
||||
class CustomerState extends Equatable {
|
||||
final CustomerStatus status;
|
||||
final List<CustomerModel> customers; // Per la lista generale
|
||||
final CustomerModel?
|
||||
lastCreatedCustomer; // <--- Fondamentale per la Dialog "al volo"
|
||||
final String? errorMessage;
|
||||
|
||||
const CustomerState({
|
||||
this.status = CustomerStatus.initial,
|
||||
this.customers = const [],
|
||||
this.lastCreatedCustomer,
|
||||
this.errorMessage,
|
||||
});
|
||||
|
||||
CustomerState copyWith({
|
||||
CustomerStatus? status,
|
||||
List<CustomerModel>? customers,
|
||||
CustomerModel? lastCreatedCustomer,
|
||||
String? errorMessage,
|
||||
}) {
|
||||
return CustomerState(
|
||||
status: status ?? this.status,
|
||||
customers: customers ?? this.customers,
|
||||
lastCreatedCustomer: lastCreatedCustomer ?? this.lastCreatedCustomer,
|
||||
errorMessage: errorMessage ?? this.errorMessage,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
status,
|
||||
customers,
|
||||
lastCreatedCustomer,
|
||||
errorMessage,
|
||||
];
|
||||
}
|
||||
56
lib/features/customers/data/customer_repository.dart
Normal file
56
lib/features/customers/data/customer_repository.dart
Normal file
@@ -0,0 +1,56 @@
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import '../models/customer_model.dart';
|
||||
|
||||
class CustomerRepository {
|
||||
final SupabaseClient _client = GetIt.I<SupabaseClient>();
|
||||
|
||||
// Crea un nuovo cliente
|
||||
Future<CustomerModel> createCustomer(CustomerModel customer) async {
|
||||
try {
|
||||
final response = await _client
|
||||
.from('customer')
|
||||
.insert(customer.toJson())
|
||||
.select()
|
||||
.single();
|
||||
return CustomerModel.fromJson(response);
|
||||
} catch (e) {
|
||||
throw 'Errore durante la creazione del cliente: $e';
|
||||
}
|
||||
}
|
||||
|
||||
// Recupera tutti i clienti dell'azienda
|
||||
Future<List<CustomerModel>> getCustomers(String companyId) async {
|
||||
try {
|
||||
final response = await _client
|
||||
.from('customer')
|
||||
.select()
|
||||
.eq('company_id', companyId)
|
||||
.eq('is_active', true)
|
||||
.order('nome');
|
||||
|
||||
return (response as List).map((c) => CustomerModel.fromJson(c)).toList();
|
||||
} catch (e) {
|
||||
throw 'Errore nel recupero clienti';
|
||||
}
|
||||
}
|
||||
|
||||
// Ricerca clienti per nome o telefono (fondamentale per la UX)
|
||||
Future<List<CustomerModel>> searchCustomers(
|
||||
String companyId,
|
||||
String query,
|
||||
) async {
|
||||
try {
|
||||
final response = await _client
|
||||
.from('customer')
|
||||
.select()
|
||||
.eq('company_id', companyId)
|
||||
.or('nome.ilike.%$query%,telefono.ilike.%$query%')
|
||||
.limit(10);
|
||||
|
||||
return (response as List).map((c) => CustomerModel.fromJson(c)).toList();
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
101
lib/features/customers/models/customer_model.dart
Normal file
101
lib/features/customers/models/customer_model.dart
Normal file
@@ -0,0 +1,101 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class CustomerModel extends Equatable {
|
||||
final BigInt? id; // Bigint in SQL
|
||||
final DateTime? createdAt;
|
||||
final String nome;
|
||||
final String telefono;
|
||||
final String email;
|
||||
final String note;
|
||||
final DateTime? dataUltimoContatto;
|
||||
final bool nonDisturbare;
|
||||
final String companyId; // UUID
|
||||
final bool isActive;
|
||||
|
||||
const CustomerModel({
|
||||
this.id,
|
||||
this.createdAt,
|
||||
required this.nome,
|
||||
required this.telefono,
|
||||
required this.email,
|
||||
required this.note,
|
||||
this.dataUltimoContatto,
|
||||
this.nonDisturbare = false,
|
||||
required this.companyId,
|
||||
this.isActive = true,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
id,
|
||||
createdAt,
|
||||
nome,
|
||||
telefono,
|
||||
email,
|
||||
note,
|
||||
dataUltimoContatto,
|
||||
nonDisturbare,
|
||||
companyId,
|
||||
isActive,
|
||||
];
|
||||
|
||||
CustomerModel copyWith({
|
||||
BigInt? id,
|
||||
DateTime? createdAt,
|
||||
String? nome,
|
||||
String? telefono,
|
||||
String? email,
|
||||
String? note,
|
||||
DateTime? dataUltimoContatto,
|
||||
bool? nonDisturbare,
|
||||
String? companyId,
|
||||
bool? isActive,
|
||||
}) {
|
||||
return CustomerModel(
|
||||
id: id ?? this.id,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
nome: nome ?? this.nome,
|
||||
telefono: telefono ?? this.telefono,
|
||||
email: email ?? this.email,
|
||||
note: note ?? this.note,
|
||||
dataUltimoContatto: dataUltimoContatto ?? this.dataUltimoContatto,
|
||||
nonDisturbare: nonDisturbare ?? this.nonDisturbare,
|
||||
companyId: companyId ?? this.companyId,
|
||||
isActive: isActive ?? this.isActive,
|
||||
);
|
||||
}
|
||||
|
||||
factory CustomerModel.fromJson(Map<String, dynamic> json) {
|
||||
return CustomerModel(
|
||||
id: json['id'],
|
||||
createdAt: json['created_at'] != null
|
||||
? DateTime.parse(json['created_at'])
|
||||
: null,
|
||||
nome: json['nome'],
|
||||
telefono: json['telefono'],
|
||||
email: json['email'],
|
||||
note: json['note'] ?? '',
|
||||
dataUltimoContatto: json['data_ultimo_contatto'] != null
|
||||
? DateTime.parse(json['data_ultimo_contatto'])
|
||||
: null,
|
||||
nonDisturbare: json['non_disturbare'] ?? false,
|
||||
companyId: json['company_id'],
|
||||
isActive: json['is_active'] ?? true,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
if (id != null) 'id': id,
|
||||
'nome': nome,
|
||||
'telefono': telefono,
|
||||
'email': email,
|
||||
'note': note,
|
||||
if (dataUltimoContatto != null)
|
||||
'data_ultimo_contatto': dataUltimoContatto!.toIso8601String(),
|
||||
'non_disturbare': nonDisturbare,
|
||||
'company_id': companyId,
|
||||
'is_active': isActive,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user