shoc-backend/SeaHavenIndustries/Components/Pages/Contact/Index.razor
Adam Moussa dbb31ca7d7 Fix Contacts.Name references throughout Blazor project
- Replace all Contacts.Name with (FirstName + " " + LastName) pattern
- Fix ContactService, Index.razor, QuotesPDF, QuotesDetails, CreateQuotes
- Fix WorkOrderPDF, Viewworkorder, UserViewWordOrder, Addworkorders
- Fix ViewTemplate, AddTemplate, Templates settings pages
- Blazor project now builds successfully
2026-04-16 16:54:28 -04:00

289 lines
11 KiB
Text

@page "/contacts"
<Header pagename="Contacts" icon='<i class="material-icons-round opacity-10">contact_page</i>'></Header>
<div class="row mt-3">
<div class="col-12">
<div class="card cardbg">
<!-- Card header -->
<div class="card-header pb-0 cardbg">
<div class="d-lg-flex">
<div>
<h5 class="mb-0 ps-3">Contacts</h5>
</div>
<div class="ms-auto my-auto d-flex flex-wrap justify-content-end text-end">
@* <RadzenButton Text="Export XLS" Icon="grid_on" Click="@(args => Export("excel"))" class="mb-0 me-2" /><RadzenButton Text="Export CSV" Icon="wrap_text" Click="@(args => Export("csv"))" class="mb-0 me-2" />
*@
<button class="btn bg-gradient-info btn-sm mb-0" @onclick="()=>Edit(0)" data-bs-toggle="modal" data-bs-target="#exampleModal">+&nbsp; New Contact</button>
</div>
</div>
</div>
<div class="card-body">
<RadzenDataGrid style="height: auto" @ref="grid" IsLoading=@isLoading PagerAlwaysVisible="true" ShowPagingSummary="true" Count="@count" Data="@contactList" LoadData="@LoadData" AllowSorting="true" AllowFiltering="true" AllowPaging="true" PageSize="10" PagerHorizontalAlign="HorizontalAlign.Right" TItem="Contacts" ColumnWidth="200px">
<Columns>
<RadzenDataGridColumn TItem="Contacts" Property="Name" Title="Name" Width="120px" />
<RadzenDataGridColumn TItem="Contacts" Property="PhoneNumber" Title="Phone Number" Width="120px" />
<RadzenDataGridColumn TItem="Contacts" Property="Email" Title="Email" Width="120px" />
<RadzenDataGridColumn TItem="Contacts" Title="Action" Context="employee" Filterable="false" Sortable="false" TextAlign="TextAlign.Center" Width="156px">
<Template Context="contactList">
<RadzenButton Icon="edit" data-bs-target="#exampleModal" data-bs-toggle="modal" ButtonStyle="ButtonStyle.Success" Variant="Variant.Flat" Size="ButtonSize.Medium"
Click="@(args => Edit(contactList.Id))" @onclick:stopPropagation="true">
</RadzenButton>
<RadzenButton Icon="delete" ButtonStyle="ButtonStyle.Danger" Variant="Variant.Flat" Size="ButtonSize.Medium" Click="@(args => DeleteSweetAlert(contactList.Id))" @onclick:stopPropagation="true">
</RadzenButton>
</Template>
</RadzenDataGridColumn>
</Columns>
</RadzenDataGrid>
</div>
</div>
</div>
</div>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered " role="document">
<div class="modal-content cardbg">
<div class="modal-header">
@if (contacts.Id == 0)
{
<h5 class="modal-title font-weight-normal" id="exampleModalLabel">Add New Contact</h5>
}
else
{
<h5 class="modal-title font-weight-normal" id="exampleModalLabel">Edit Contact</h5>
}
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"> X
@* <span aria-hidden="true">&times;</span> *@
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-12">
<label for="exampleFormControlInput1" class="form-label">First Name:</label>
<div class="input-group input-group-outline mt-1 mb-2">
<input type="text" class="form-control" @bind="contacts.FirstName" />
</div>
</div>
<div class="col-12">
<label for="exampleFormControlInput1" class="form-label">Last Name:</label>
<div class="input-group input-group-outline mt-1 mb-2">
<input type="text" class="form-control" @bind="contacts.LastName" />
</div>
</div>
<div class="col-12">
<label for="exampleFormControlInput1" class="form-label">Phone Number:</label>
<div class="input-group input-group-outline mt-1 mb-2">
<input type="text" class="form-control" @oninput="FormatPhoneNumber" @bind="contacts.PhoneNumber" />
</div>
</div>
<div class="col-12">
<label for="exampleFormControlInput1" class="form-label">Email:</label>
<div class="input-group input-group-outline mt-1 mb-2">
<input type="email" class="form-control" @bind="contacts.Email" />
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn bg-gradient-info" @onclick="SaveContacts">Add</button>
</div>
</div>
</div>
</div>
@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<Contacts> grid;
int count;
int eid;
IEnumerable<Contacts> 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<JsonElement>("Swal.fire", new
{
html = "<div style='text-align: center;'><img src='./assets/img/delete icon.png' style='width: 100px;' /></div>" +
"<h4>Are you sure you want to delete</h4>" +
"<p style='font-size: 18px;'>\"Nicolas Alvarado\"</p>",
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))
});
}
}