feat: Enhance apple categorization logic and improve bulk category update feedback
Test Suite / test (24.15.0) (push) Has been cancelled

This commit is contained in:
Nils-Johan Gynther
2026-05-11 10:31:57 +02:00
parent 06056c6182
commit 56050a896b
4 changed files with 81 additions and 7 deletions
@@ -173,9 +173,6 @@ class AdminRepository {
// ── Produkter ──────────────────────────────────────────────────────────────
Future<List<AdminProduct>> listProducts() =>
_getList(ProductApiPaths.mine, AdminProduct.fromJson);
Future<List<AdminProduct>> listGlobalProducts() =>
_getList(ProductApiPaths.list, AdminProduct.fromJson, requiresAuth: false);
Future<List<PendingProduct>> listPrivateProducts() =>
@@ -244,8 +241,15 @@ class AdminRepository {
parse: (d) => d as Map<String, dynamic>,
);
Future<void> bulkSetCategory(List<int> ids, {required int? categoryId}) =>
_postVoid(ProductApiPaths.bulkUpdate, {'ids': ids, 'categoryId': categoryId});
Future<int> bulkSetCategory(List<int> ids, {required int? categoryId}) =>
_post<int>(
ProductApiPaths.bulkUpdate,
body: {'ids': ids, 'categoryId': categoryId},
parse: (d) {
final map = Map<String, dynamic>.from(d as Map);
return (map['updated'] as num?)?.toInt() ?? 0;
},
);
Future<void> mergeProducts({
required int sourceProductId,
@@ -116,10 +116,15 @@ class _AdminProductsPanelState extends ConsumerState<AdminProductsPanel> {
setState(() => _isApplying = true);
try {
await ref.read(adminRepositoryProvider).bulkSetCategory(
final updated = await ref.read(adminRepositoryProvider).bulkSetCategory(
_selectedIds.toList(),
categoryId: categoryId,
);
if (updated == 0) {
if (!mounted) return;
_showError('Inga produkter uppdaterades. Kontrollera att valda produkter fortfarande finns och försök igen.');
return;
}
if (!mounted) return;
setState(() {
_selectedIds.clear();
@@ -164,11 +169,18 @@ class _AdminProductsPanelState extends ConsumerState<AdminProductsPanel> {
if (!selectedProductIds.contains(row.productId)) continue;
grouped.putIfAbsent(row.categoryId, () => <int>[]).add(row.productId);
}
var totalUpdated = 0;
for (final entry in grouped.entries) {
await ref.read(adminRepositoryProvider).bulkSetCategory(
final updated = await ref.read(adminRepositoryProvider).bulkSetCategory(
entry.value,
categoryId: entry.key,
);
totalUpdated += updated;
}
if (totalUpdated == 0) {
if (!mounted) return;
_showError('AI-förslag kunde inte sparas. Prova att uppdatera listan och kör igen.');
return;
}
if (!mounted) return;
setState(() => _selectedIds.clear());