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

@@ -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);
}