feat: scope filterable fields to agent type and update active filters on selection change

This commit is contained in:
pradeepkumar
2026-04-16 22:32:31 +05:30
parent 436f8fac42
commit 8dafe73425
2 changed files with 32 additions and 3 deletions

View File

@@ -89,10 +89,16 @@ class AgentsRepository {
}
/// Get filterable profile fields: GET /profile-fields/filterable
/// When `agentTypeId` is provided, backend scopes filters to that agent type + globals.
/// Response: { success, data: [...] }
Future<List<FilterableField>> getFilterableFields() async {
Future<List<FilterableField>> getFilterableFields({String? agentTypeId}) async {
try {
final response = await _dio.get(ApiConstants.filterableFields);
final response = await _dio.get(
ApiConstants.filterableFields,
queryParameters: agentTypeId != null && agentTypeId.isNotEmpty
? {'agentTypeId': agentTypeId}
: null,
);
final data = response.data['data'] as List<dynamic>;
return data
.map((e) => FilterableField.fromJson(e as Map<String, dynamic>))

View File

@@ -78,7 +78,7 @@ class SearchAgentsNotifier extends StateNotifier<SearchAgentsState> {
try {
final results = await Future.wait([
_repository.getAgentTypes(),
_repository.getFilterableFields(),
_repository.getFilterableFields(agentTypeId: state.selectedAgentTypeId),
]);
if (!mounted) return;
state = state.copyWith(
@@ -92,6 +92,28 @@ class SearchAgentsNotifier extends StateNotifier<SearchAgentsState> {
}
}
/// Re-fetch filterable fields scoped to the currently selected agent type.
/// Called whenever the user switches agent types so irrelevant filters disappear.
Future<void> _refreshFilterFields(String? agentTypeId) async {
try {
final fields =
await _repository.getFilterableFields(agentTypeId: agentTypeId);
if (!mounted) return;
// Drop any active filter slugs that no longer exist in the new list.
final validSlugs = fields.map((f) => f.slug).toSet();
final culled = <String, List<String>>{};
state.activeFilters.forEach((k, v) {
if (validSlugs.contains(k)) culled[k] = v;
});
state = state.copyWith(
filterFields: fields,
activeFilters: culled,
);
} catch (_) {
// Keep existing fields if the refresh fails; user can retry.
}
}
Future<void> _fetchAgents({required int page, bool append = false}) async {
try {
final response = await _repository.searchAgents(
@@ -135,6 +157,7 @@ class SearchAgentsNotifier extends StateNotifier<SearchAgentsState> {
isLoading: true,
currentPage: 1,
);
_refreshFilterFields(agentTypeId);
_fetchAgents(page: 1);
}