@page "/contacts"
@using Microsoft.JSInterop
@inject IJSRuntime JSRuntime
@inject NavigationManager Navigation;
@using SeaHavenIndustries.Data.IRepository;
@inject IContactRepository _services;
@using System.Text.Json
@using global::Data.SeaHavenIndustries;
@inject NorthwindService service
@using System.Linq.Dynamic.Core
@code {
RadzenDataGrid grid;
int count;
int eid;
IEnumerable contactList;
bool isLoading = false;
private Contacts contacts = new Contacts();
async Task Reset()
{
grid.Reset(true);
await grid.FirstPage(true);
}
private void FormatPhoneNumber(ChangeEventArgs e)
{
string input = e.Value?.ToString();
string numericOnly = new string(input?.Where(c => char.IsDigit(c)).ToArray());
if (numericOnly.Length >= 3)
{
string formattedNumber = $"({numericOnly.Substring(0, 3)}";
if (numericOnly.Length >= 6)
{
formattedNumber += $") {numericOnly.Substring(3, 3)}";
if (numericOnly.Length >= 10)
{
formattedNumber += $"-{numericOnly.Substring(6, 4)}";
}
}
contacts.PhoneNumber = formattedNumber;
}
else
{
contacts.PhoneNumber = numericOnly;
}
}
protected async Task SaveContacts()
{
var response = await _services.AddContact(contacts);
if (response == "success")
{
await ShowSweetAlert("Sucessfully Saved Data", "success");
contactList = await _services.GetContacts();
await Reset();
}
else
{
await ShowSweetAlert(response, "error");
}
}
protected async Task Edit(int id)
{
if (id != 0)
{
eid = id;
contacts = await _services.GetContactById(id);
}
else
{
contacts = new Contacts();
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("initScripts");
}
}
private async Task SelectAllContacts()
{
await JSRuntime.InvokeVoidAsync("SelectContacts");
}
private async Task ShowSweetAlert(string title, string icon)
{
// Display a SweetAlert dialog
await JSRuntime.InvokeVoidAsync("Swal.fire", new
{
toast = true,
showConfirmButton = false,
timer = 3000,
title = title,
icon = icon,
position = "top-end"
});
await JSRuntime.InvokeVoidAsync("closemodal");
}
private async Task DeleteSweetAlert(int val)
{
var result = await JSRuntime.InvokeAsync("Swal.fire", new
{
html = "" +
"Are you sure you want to delete
" +
"\"Nicolas Alvarado\"
",
type = "warning",
showCancelButton = true,
customClass = new
{
confirmButton = "btn btn-success m-1",
cancelButton = "btn btn-danger m-1"
},
buttonsStyling = false,
confirmButtonText = "Yes",
cancelButtonText = "No"
});
if (result.TryGetProperty("isConfirmed", out var isConfirmedProperty) && isConfirmedProperty.GetBoolean())
{
// Call your delete API here
var response = await _services.DeleteContact(val);
if (response == "success")
{
await JSRuntime.InvokeVoidAsync("Swal.fire", new
{
icon = "success",
title = "Deleted!",
text = "Data has been deleted.",
type = "primary",
showCancelButton = false,
customClass = new
{
confirmButton = "btn btn-success",
cancelButton = "btn btn-danger"
},
buttonsStyling = false,
confirmButtonText = "Ok"
});
contactList = await _services.GetContacts();
await Reset();
// await JSRuntime.InvokeVoidAsync("location.reload");
}
else
{
await JSRuntime.InvokeVoidAsync("Swal.fire", new
{
title = "Not Deleted!",
text = "Unsuccessful! Cannot delete this.",
type = "danger"
});
}
}
}
async Task LoadData(LoadDataArgs args)
{
isLoading = true;
await Task.Yield();
// This demo is using https://dynamic-linq.net
var query = await _services.GetContactsiqueryable();
if (!string.IsNullOrEmpty(args.Filter))
{
// Filter via the Where method
query = query.Where(args.Filter);
}
if (!string.IsNullOrEmpty(args.OrderBy))
{
// Sort via the OrderBy method
query = query.OrderBy(args.OrderBy);
}
// Important!!! Make sure the Count property of RadzenDataGrid is set.
count = query.Count();
// Perform paging via Skip and Take.
contactList = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
isLoading = false;
}
public void Export(string type)
{
service.Export("Contacts", type, new Query()
{
OrderBy = grid.Query.OrderBy,
Filter = grid.Query.Filter,
Select = string.Join(",", grid.ColumnsCollection.Where(c => c.GetVisible() && !string.IsNullOrEmpty(c.Property))
.Select(c => c.Property.Contains(".") ? $"{c.Property} as {c.Property.Replace(".", "_")}" : c.Property))
});
}
}