@using Indotalent.ConfigBackEnd.Extensions @using Indotalent.Features.Thirdparty.PatientContact @using Indotalent.Features.Thirdparty.PatientContact.Cqrs @using Microsoft.AspNetCore.Components.Web @using Microsoft.JSInterop @using MudBlazor @using Features.Root.Shared @using ClosedXML.Excel @using System.IO @inject PatientContactService PatientContactService @inject IDialogService DialogService @inject ISnackbar Snackbar @inject IJSRuntime JSRuntime
Patient Contact Manage individual contact persons for your patients.
/ Third Party / Patient Contact
Search
@if (_isExporting) { Processing... } else { Excel } @if (_isRefreshing) { Refreshing... } else { Refresh } @if (_selectedContact != null) { View Edit Remove } else { Add New }
Contact Name Patient Title Email / Phone
@context.Name @context.AutoNumber
@context.PatientName @context.Title
@context.EmailAddress @context.PhoneNumber
Rows per page: Showing @(GetFilteredData().Count() == 0 ? 0 : _skip + 1)-@Math.Min(_skip + _top, GetFilteredData().Count()) of @GetFilteredData().Count()
Prev @{ var totalPages = _totalPage == 0 ? 1 : _totalPage; var maxVisible = 5; var startPage = Math.Max(1, _currentPage - maxVisible / 2); var endPage = Math.Min(totalPages, startPage + maxVisible - 1); if (endPage - startPage < maxVisible - 1) { startPage = Math.Max(1, endPage - maxVisible + 1); } } @for (int i = startPage; i <= endPage; i++) { var pageNum = i; var isActive = pageNum == _currentPage; @pageNum } Next
@code { [Parameter] public EventCallback OnAdd { get; set; } [Parameter] public EventCallback OnEdit { get; set; } [Parameter] public EventCallback OnView { get; set; } private List _contacts = new(); private GetPatientContactListResponse? _selectedContact; private string _searchString = ""; private bool _isRefreshing = false; private bool _isExporting = false; private int _skip = 0; private int _top = 5; private int _totalPage => GetFilteredData().Count() == 0 ? 0 : (int)Math.Ceiling((double)GetFilteredData().Count() / (_top == 0 ? 1 : _top)); private int _currentPage => (_top >= GetFilteredData().Count() || _top == 0) ? 1 : (_skip / _top) + 1; protected override async Task OnInitializedAsync() => await LoadData(); private async Task LoadData() { _isRefreshing = true; _selectedContact = null; StateHasChanged(); try { var response = await PatientContactService.GetPatientContactListAsync(); await Task.Delay(500); if (response != null && response.IsSuccess) { _contacts = response.Value ?? new List(); } } finally { _isRefreshing = false; StateHasChanged(); } } private IEnumerable GetFilteredData() { if (string.IsNullOrWhiteSpace(_searchString)) return _contacts; return _contacts.Where(x => (x.Name?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) ?? false) || (x.EmailAddress?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) ?? false) || (x.PatientName?.Contains(_searchString, StringComparison.OrdinalIgnoreCase) ?? false) ); } private IEnumerable GetPagedData() => GetFilteredData().Skip(_skip).Take(_top); private void OnSearchClick() { _skip = 0; _selectedContact = null; StateHasChanged(); } private void HandleSearchKeyDown(KeyboardEventArgs e) { if (e.Key == "Enter") OnSearchClick(); } private async Task ExportToExcel() { _isExporting = true; try { using (var workbook = new XLWorkbook()) { var worksheet = workbook.Worksheets.Add("PatientContacts"); worksheet.Cell(1, 1).Value = "Contact Name"; worksheet.Cell(1, 2).Value = "Job Title"; worksheet.Cell(1, 3).Value = "Patient"; worksheet.Cell(1, 4).Value = "Email"; worksheet.Cell(1, 5).Value = "Phone"; worksheet.Range(1, 1, 1, 5).Style.Font.Bold = true; var currentRow = 1; foreach (var item in GetFilteredData()) { currentRow++; worksheet.Cell(currentRow, 1).Value = item.Name; worksheet.Cell(currentRow, 2).Value = item.Title; worksheet.Cell(currentRow, 3).Value = item.PatientName; worksheet.Cell(currentRow, 4).Value = item.EmailAddress; worksheet.Cell(currentRow, 5).Value = item.PhoneNumber; } worksheet.Columns().AdjustToContents(); using (var stream = new MemoryStream()) { workbook.SaveAs(stream); var content = Convert.ToBase64String(stream.ToArray()); await JSRuntime.InvokeVoidAsync("downloadFile", "PatientContact_List.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", content); } } } finally { _isExporting = false; StateHasChanged(); } } private void OnPageChanged(int page) { if (page >= 1 && page <= _totalPage) { _skip = (page - 1) * _top; _selectedContact = null; StateHasChanged(); } } private void OnPageSizeChanged(int size) { _top = size; _skip = 0; _selectedContact = null; StateHasChanged(); } private async Task InvokeEdit() { if (_selectedContact != null) { var req = await MapToUpdateRequest(_selectedContact.Id); if (req != null) await OnEdit.InvokeAsync(req); } } private async Task InvokeView() { if (_selectedContact != null) { var req = await MapToUpdateRequest(_selectedContact.Id); if (req != null) await OnView.InvokeAsync(req); } } private async Task MapToUpdateRequest(string id) { var response = await PatientContactService.GetPatientContactByIdAsync(id); if (response != null && response.IsSuccess && response.Value != null) { var d = response.Value; return new UpdatePatientContactRequest { Id = d.Id, Name = d.Name, Title = d.Title, PhoneNumber = d.PhoneNumber, EmailAddress = d.EmailAddress, Description = d.Description, PatientId = d.PatientId, CreatedAt = d.CreatedAt, CreatedBy = d.CreatedBy, UpdatedAt = d.UpdatedAt, UpdatedBy = d.UpdatedBy }; } return null; } private async Task OnDelete() { if (_selectedContact == null) return; var parameters = new DialogParameters<_DeleteConfirmation> { { x => x.ContentText, _selectedContact.Name } }; var dialog = await DialogService.ShowAsync<_DeleteConfirmation>("", parameters, new DialogOptions { MaxWidth = MaxWidth.ExtraSmall, FullWidth = true }); var result = await dialog.Result; if (result != null && !result.Canceled) { var success = await PatientContactService.DeletePatientContactByIdAsync(_selectedContact.Id); if (success) { _selectedContact = null; await LoadData(); Snackbar.Add("Contact deleted successfully", Severity.Success); } } } }