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 }) {
|
async bulkUpdate(ids: number[], data: { categoryId?: number | null }) {
|
||||||
const updateData: Record<string, any> = {};
|
const updateData: Record<string, any> = {};
|
||||||
if ('categoryId' in data) {
|
if (data.categoryId !== undefined) {
|
||||||
updateData.categoryId = data.categoryId;
|
updateData.categoryId = data.categoryId;
|
||||||
}
|
}
|
||||||
if (Object.keys(updateData).length === 0) return { updated: 0 };
|
if (Object.keys(updateData).length === 0) return { updated: 0 };
|
||||||
await this.prisma.product.updateMany({ where: { id: { in: ids } }, data: updateData });
|
const result = await this.prisma.product.updateMany({
|
||||||
return { updated: ids.length };
|
where: { id: { in: ids } },
|
||||||
|
data: updateData,
|
||||||
|
});
|
||||||
|
return { updated: result.count };
|
||||||
}
|
}
|
||||||
|
|
||||||
async findUncategorized(): Promise<{ id: number; name: string; canonicalName: string | null }[]> {
|
async findUncategorized(): Promise<{ id: number; name: string; canonicalName: string | null }[]> {
|
||||||
|
|||||||
@@ -104,14 +104,21 @@ class _AdminProductsPanelState extends ConsumerState<AdminProductsPanel> {
|
|||||||
|
|
||||||
Future<void> _applyBulkCategory() async {
|
Future<void> _applyBulkCategory() async {
|
||||||
if (_selectedIds.isEmpty || _isApplying) return;
|
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);
|
setState(() => _isApplying = true);
|
||||||
try {
|
try {
|
||||||
await ref.read(adminRepositoryProvider).bulkSetCategory(
|
await ref.read(adminRepositoryProvider).bulkSetCategory(
|
||||||
_selectedIds.toList(),
|
_selectedIds.toList(),
|
||||||
categoryId: _bulkCategoryValue == null ||
|
categoryId: categoryId,
|
||||||
_bulkCategoryValue == '__remove__'
|
|
||||||
? null
|
|
||||||
: int.parse(_bulkCategoryValue!),
|
|
||||||
);
|
);
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
setState(() {
|
setState(() {
|
||||||
@@ -651,7 +658,9 @@ class _AdminProductsPanelState extends ConsumerState<AdminProductsPanel> {
|
|||||||
),
|
),
|
||||||
if (!_showDeletedOnly)
|
if (!_showDeletedOnly)
|
||||||
FilledButton(
|
FilledButton(
|
||||||
onPressed: _selectedIds.isEmpty || _isApplying
|
onPressed: _selectedIds.isEmpty ||
|
||||||
|
_isApplying ||
|
||||||
|
_bulkCategoryValue == null
|
||||||
? null
|
? null
|
||||||
: _applyBulkCategory,
|
: _applyBulkCategory,
|
||||||
child: _isApplying
|
child: _isApplying
|
||||||
|
|||||||
Reference in New Issue
Block a user