@@ -16,6 +16,9 @@ public partial class Category : ModuleBase
1616 [ Inject ]
1717 private Radzen . DialogService DialogService { get ; set ; } = default ! ;
1818
19+ [ Parameter ]
20+ public int ModuleId { get ; set ; }
21+
1922 private const string SuccessNotificationMessage = "Success" ;
2023 private const string ErrorNotificationMessage = "Error" ;
2124 private List < ListCategoryDto > _treeData = [ ] ;
@@ -41,7 +44,7 @@ protected override async Task OnInitializedAsync()
4144 ErrorMessage = null ;
4245 try
4346 {
44- Categories = await CategoryService . ListAsync ( ModuleState . ModuleId , pageNumber : 1 , pageSize : int . MaxValue ) ;
47+ Categories = await CategoryService . ListAsync ( ModuleId , pageNumber : 1 , pageSize : int . MaxValue ) ;
4548 CreateTreeStructure ( ) ;
4649 }
4750 catch ( Exception ex )
@@ -56,7 +59,10 @@ protected override async Task OnInitializedAsync()
5659
5760 private void ShowContextMenu ( MouseEventArgs args , ListCategoryDto ? category )
5861 {
59- if ( category == null ) return ;
62+ if ( category == null )
63+ {
64+ return ;
65+ }
6066
6167 SelectedCategory = category ;
6268
@@ -69,7 +75,7 @@ private void ShowContextMenu(MouseEventArgs args, ListCategoryDto? category)
6975 Text = "Add Category" ,
7076 Value = "add" ,
7177 Icon = "add" ,
72- }
78+ } ,
7379 } ;
7480
7581 ContextMenuService . Open ( args , rootMenuItems , OnContextMenuClick ) ;
@@ -117,8 +123,10 @@ private void ShowContextMenu(MouseEventArgs args, ListCategoryDto? category)
117123 ContextMenuService . Open ( args , menuItems , OnContextMenuClick ) ;
118124 }
119125
120- private void OnContextMenuClick ( Radzen . MenuItemEventArgs args )
126+ private async void OnContextMenuClick ( Radzen . MenuItemEventArgs args )
121127 {
128+ ContextMenuService . Close ( ) ;
129+
122130 var action = args . Value ? . ToString ( ) ;
123131
124132 switch ( action )
@@ -130,17 +138,15 @@ private void OnContextMenuClick(Radzen.MenuItemEventArgs args)
130138 EditCategoryInline ( ) ;
131139 break ;
132140 case "moveup" :
133- _ = MoveUp ( ) ;
141+ await MoveUp ( ) ;
134142 break ;
135143 case "movedown" :
136- _ = MoveDown ( ) ;
144+ await MoveDown ( ) ;
137145 break ;
138146 case "delete" :
139- PromptDeleteCategory ( ) ;
147+ await PromptDeleteCategory ( ) ;
140148 break ;
141149 }
142-
143- ContextMenuService . Close ( ) ;
144150 }
145151
146152 private bool CanMoveUp ( ListCategoryDto category )
@@ -283,7 +289,7 @@ private async Task SaveInlineEdit()
283289 ParentId = EditingNode . ParentId ,
284290 } ;
285291
286- var id = await CategoryService . CreateAsync ( ModuleState . ModuleId , createDto ) ;
292+ var id = await CategoryService . CreateAsync ( ModuleId , createDto ) ;
287293 await logger . LogInformation ( "Category Created {Id}" , id ) ;
288294
289295 // Update the temporary node in-place with the real ID and name
@@ -308,7 +314,7 @@ private async Task SaveInlineEdit()
308314 ParentId = EditingNode . ParentId ,
309315 } ;
310316
311- await CategoryService . UpdateAsync ( EditingNode . Id , ModuleState . ModuleId , updateDto ) ;
317+ await CategoryService . UpdateAsync ( EditingNode . Id , ModuleId , updateDto ) ;
312318 await logger . LogInformation ( "Category Updated {Id}" , EditingNode . Id ) ;
313319
314320 // Update the node name in-place
@@ -405,7 +411,7 @@ private async Task MoveUp()
405411 var previous = siblings [ currentIndex - 1 ] ;
406412
407413 // Update on server using dedicated move endpoint
408- await CategoryService . MoveUpAsync ( current . Id , ModuleState . ModuleId ) ;
414+ await CategoryService . MoveUpAsync ( current . Id , ModuleId ) ;
409415
410416 // Swap ViewOrder values locally
411417 ( current . ViewOrder , previous . ViewOrder ) = ( previous . ViewOrder , current . ViewOrder ) ;
@@ -460,7 +466,7 @@ private async Task MoveDown()
460466 var next = siblings [ currentIndex + 1 ] ;
461467
462468 // Update on server using dedicated move endpoint
463- await CategoryService . MoveDownAsync ( current . Id , ModuleState . ModuleId ) ;
469+ await CategoryService . MoveDownAsync ( current . Id , ModuleId ) ;
464470
465471 // Swap ViewOrder values locally
466472 ( current . ViewOrder , next . ViewOrder ) = ( next . ViewOrder , current . ViewOrder ) ;
@@ -534,7 +540,7 @@ private async Task DeleteCategory()
534540 {
535541 var categoryToDelete = SelectedCategory ;
536542
537- await CategoryService . DeleteAsync ( categoryToDelete . Id , ModuleState . ModuleId ) ;
543+ await CategoryService . DeleteAsync ( categoryToDelete . Id , ModuleId ) ;
538544 await logger . LogInformation ( "Category Deleted {Id}" , categoryToDelete . Id ) ;
539545
540546 if ( categoryToDelete . ParentId is null )
@@ -545,10 +551,7 @@ private async Task DeleteCategory()
545551 else
546552 {
547553 var parent = FindCategoryById ( [ _rootNode ] , categoryToDelete . ParentId . Value ) ;
548- if ( parent != null )
549- {
550- parent . Children . Remove ( categoryToDelete ) ;
551- }
554+ parent ? . Children . Remove ( categoryToDelete ) ;
552555 }
553556
554557 NotificationService . Notify ( new Radzen . NotificationMessage
@@ -577,7 +580,7 @@ private async Task DeleteCategory()
577580
578581 private void CreateTreeStructure ( )
579582 {
580- if ( Categories . Items is null || ! Categories . Items . Any ( ) )
583+ if ( Categories . Items ? . Any ( ) != true )
581584 {
582585 _treeData = [ ] ;
583586
@@ -589,7 +592,7 @@ private void CreateTreeStructure()
589592 ParentId = - 1 ,
590593 ViewOrder = 0 ,
591594 IsExpanded = true ,
592- Children = [ ]
595+ Children = [ ] ,
593596 } ;
594597 return ;
595598 }
@@ -625,23 +628,26 @@ private void CreateTreeStructure()
625628 ParentId = null ,
626629 ViewOrder = 0 ,
627630 IsExpanded = true ,
628- Children = _treeData
631+ Children = _treeData ,
629632 } ;
630633 }
631634
632635 private static void SortChildren ( List < ListCategoryDto > categories )
633636 {
634637 foreach ( var category in categories )
635638 {
636- if ( category . Children . Any ( ) )
639+ if ( category . Children . Count == 0 )
637640 {
638- category . Children = category . Children
639- . OrderBy ( c => c . ViewOrder )
640- . ThenBy ( c => c . Name , StringComparer . Ordinal )
641- . ToList ( ) ;
642-
643- SortChildren ( category . Children . ToList ( ) ) ;
641+ continue ;
644642 }
643+
644+ var sortedChildren = category . Children
645+ . OrderBy ( c => c . ViewOrder )
646+ . ThenBy ( c => c . Name , StringComparer . Ordinal )
647+ . ToList ( ) ;
648+
649+ category . Children = sortedChildren ;
650+ SortChildren ( sortedChildren ) ;
645651 }
646652 }
647653
0 commit comments