@page "/addressbook"
@using System.Linq.Dynamic.Core
@using SeaHavenIndustries.Data.IRepository;
@using System.Text.Json
@using global::Data.SeaHavenIndustries
@inject IAccountRepository _service;
@inject NorthwindService service
@inject IJSRuntime JSRuntime
@using GoogleMapsComponents.Maps.Places
@using Microsoft.AspNetCore.Components.Forms
@code {
RadzenDataGrid grid;
int count;
int eid;
string submitbtnatrr = "";
bool issubmitting = false;
private ElementReference addressBox;
private RadzenTextBox? addressBoxRadzenTextBox;
private Autocomplete? autocomplete;
private string? placeData;
private string? formattedAddress;
IEnumerable locations;
bool isLoading = false;
private Locations model = new Locations();
List titles = new List { "Sales Representative", "Vice President, Sales", "Sales Manager", "Inside Sales Coordinator" };
IEnumerable selectedTitles;
async Task OnSelectedTitlesChange(object value)
{
if (selectedTitles != null && !selectedTitles.Any())
{
selectedTitles = null;
}
await grid.FirstPage();
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JSRuntime.InvokeVoidAsync("initScripts");
addressBox = addressBoxRadzenTextBox.Element;
await SetupGoogleAddressAutoComplete();
}
}
async Task Reset()
{
grid.Reset(true);
await grid.FirstPage(true);
}
protected async Task Edit(int id)
{
if (id != 0)
{
eid = id;
model = await _service.GetLocationById(id);
formattedAddress = model.Name;
}
else
{
model = new Locations();
formattedAddress = null;
}
}
protected async Task savesheetdata()
{
//model.Name = formattedAddress;
issubmitting = true;
submitbtnatrr = "disabled";
var res = await _service.ImportLocation(file);
if (res == "success")
{
await ShowSweetAlert("Sucessfully Saved Data", "success");
// modelList = (await _service.GetLocations())
// .OrderBy(location => location.Title)
// .ToList();
locations = await _service.GetLocationsiqueryable();
//await LoadData(null);
await JSRuntime.InvokeVoidAsync("location.reload");
submitbtnatrr = "";
issubmitting = false;
}
else
{
await ShowSweetAlert(res, "error");
submitbtnatrr = "";
issubmitting = false;
}
}
protected async Task savedata()
{
//model.Name = formattedAddress;
var res = await _service.AddLocation(model);
if (res == "success")
{
await ShowSweetAlert("Sucessfully Saved Data", "success");
locations = await _service.GetLocationsiqueryable();
grid.Reload();
StateHasChanged();
}
else
{
await ShowSweetAlert(res, "error");
}
}
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 GoogleAddressAutoComplete()
{
System.Threading.Thread.Sleep(500); // this is a hack. with no wait place was null first time
var place = await this.autocomplete.GetPlace();
var placeDataTest = JsonSerializer.Serialize(place);
if (place?.AddressComponents == null)
{
// this.message = "No results available for " + place?.Name;
}
else
{
model.Name = place.FormattedAddress;
var placeId = place.PlaceId;
var streetNumber = place.AddressComponents.FirstOrDefault(a => a.Types != null && a.Types.Contains("street_number"))?.LongName;
var street = place.AddressComponents.FirstOrDefault(a => a.Types != null && (a.Types.Contains("route") || a.Types.Contains("street_address")))?.LongName;
var city = place.AddressComponents.FirstOrDefault(a => a.Types != null && (a.Types.Contains("locality") || a.Types.Contains("neighborhood")))?.LongName;
var state = place.AddressComponents.FirstOrDefault(a => a.Types != null && a.Types.Contains("administrative_area_level_1"))?.LongName;
var country = place.AddressComponents.FirstOrDefault(a => a.Types != null && a.Types.Contains("country"))?.LongName;
var postcode = place.AddressComponents.FirstOrDefault(a => a.Types != null && a.Types.Contains("postal_code"))?.LongName;
formattedAddress = place.FormattedAddress;
// model.Title = place.AddressComponents.FirstOrDefault()?.LongName;
placeData = JsonSerializer.Serialize(place);
}
}
private async Task SetupGoogleAddressAutoComplete()
{
this.autocomplete = await Autocomplete.CreateAsync(JSRuntime, addressBox, new AutocompleteOptions
{
StrictBounds = false,
});
await this.autocomplete.SetFields(new[] { "address_components", "place_id", "formatted_address" });
}
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 _service.DeleteLocation(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"
});
locations = await _service.GetLocationsiqueryable();
await JSRuntime.InvokeVoidAsync("location.reload");
// 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 _service.GetLocationsiqueryable();
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.
locations = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
isLoading = false;
}
public void Export(string type)
{
service.Export("locations", 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))
});
}
private IBrowserFile file;
private async Task SheetUpload(InputFileChangeEventArgs e)
{
// Use IFormFile as needed
file = e.File;
}
}