feat: Improve bulk category update functionality with validation and clearer logic
Test Suite / test (24.15.0) (push) Has been cancelled
Test Suite / test (24.15.0) (push) Has been cancelled
This commit is contained in:
@@ -389,12 +389,15 @@ export class ProductsService {
|
||||
|
||||
async bulkUpdate(ids: number[], data: { categoryId?: number | null }) {
|
||||
const updateData: Record<string, any> = {};
|
||||
if ('categoryId' in data) {
|
||||
if (data.categoryId !== undefined) {
|
||||
updateData.categoryId = data.categoryId;
|
||||
}
|
||||
if (Object.keys(updateData).length === 0) return { updated: 0 };
|
||||
await this.prisma.product.updateMany({ where: { id: { in: ids } }, data: updateData });
|
||||
return { updated: ids.length };
|
||||
const result = await this.prisma.product.updateMany({
|
||||
where: { id: { in: ids } },
|
||||
data: updateData,
|
||||
});
|
||||
return { updated: result.count };
|
||||
}
|
||||
|
||||
async findUncategorized(): Promise<{ id: number; name: string; canonicalName: string | null }[]> {
|
||||
|
||||
@@ -104,14 +104,21 @@ class _AdminProductsPanelState extends ConsumerState<AdminProductsPanel> {
|
||||
|
||||
Future<void> _applyBulkCategory() async {
|
||||
if (_selectedIds.isEmpty || _isApplying) return;
|
||||
if (_bulkCategoryValue == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Välj en kategori innan du uppdaterar valda produkter.')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final categoryId =
|
||||
_bulkCategoryValue == '__remove__' ? null : int.parse(_bulkCategoryValue!);
|
||||
|
||||
setState(() => _isApplying = true);
|
||||
try {
|
||||
await ref.read(adminRepositoryProvider).bulkSetCategory(
|
||||
_selectedIds.toList(),
|
||||
categoryId: _bulkCategoryValue == null ||
|
||||
_bulkCategoryValue == '__remove__'
|
||||
? null
|
||||
: int.parse(_bulkCategoryValue!),
|
||||
categoryId: categoryId,
|
||||
);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
@@ -651,7 +658,9 @@ class _AdminProductsPanelState extends ConsumerState<AdminProductsPanel> {
|
||||
),
|
||||
if (!_showDeletedOnly)
|
||||
FilledButton(
|
||||
onPressed: _selectedIds.isEmpty || _isApplying
|
||||
onPressed: _selectedIds.isEmpty ||
|
||||
_isApplying ||
|
||||
_bulkCategoryValue == null
|
||||
? null
|
||||
: _applyBulkCategory,
|
||||
child: _isApplying
|
||||
|
||||
Reference in New Issue
Block a user