Example #1
0
        /// <summary>
        /// Delete user function.
        /// </summary>
        /// <param name="User">User to delete.</param>
        async Task DeleteUser(User User)
        {
            // Show loading panal.
            LoadingPanal.Show();

            // Send the request and wait for result.
            string result = await Controller.DeleteUser(User.Id);

            // Hide loading panal.
            LoadingPanal.Hide();

            if (!string.IsNullOrEmpty(result))
            {
                // If the result is not empty the somthing went wrong in the request, So display the error.
                NotificationPanal.Show(NotificationType.Error, result, true, $"Can't delete '{User.Username}'!");
            }
            else
            {
                // If everything goes right, Delete the user from local list.
                UsersList.Remove(User);

                //Decrease total users count.
                UsersList.TotalCount--;

                // If current user is the last user in the list and there is other pages, request other page from the server.
                if (UsersList.Count == 0 && UsersList.PagesCount > 1)
                {
                    // New page number will be same as current page number except if the current page is last page then we should take previous one.
                    int pageNumber = UsersList.CurrentPage == UsersList.PagesCount ? UsersList.PagesCount - 1 : UsersList.CurrentPage;

                    // Keep other properties as they are.
                    int    sortBy       = UsersList.SortedBy;
                    string filterstring = UsersList.FilterString;

                    // Assign UsersList to null, this will make the layout displaying lazy content component.
                    UsersList = null;

                    // Force layout to refreshing component again.
                    StateHasChanged();

                    // Get result from server.
                    UsersList = await Controller.GetUsers(pageNumber, AppSettings.ItemsPerPage, sortBy, filterstring);
                }
            }

            // Force layout to refreshing component again.
            StateHasChanged();
        }