@page "/patient/medical_history"
@attribute [Authorize(Roles = UserRoles.Patient)]
@layout PatientLayout
@inject IPatientManagementService PatientManagementService
@inject IAuthenticationService AuthenticationService
@inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime
Medical History
@if (!string.IsNullOrEmpty(statusMessage))
{
@statusMessage
}
@if (uploadedFile != null)
{
}
else
{
}
@code {
private MedicalHistory uploadedFile;
private string statusMessage;
private bool isSuccess;
protected override async Task OnInitializedAsync()
{
await CheckMedicalHistory();
}
private async Task CheckMedicalHistory()
{
var userInfo = await AuthenticationService.GetUserInformation();
uploadedFile = await PatientManagementService.GetMedicalHistoryAsync(userInfo.Id);
StateHasChanged();
}
private async Task HandleFileSelected(InputFileChangeEventArgs e)
{
var file = e.File;
if (file == null)
{
statusMessage = "No file selected.";
isSuccess = false;
StateHasChanged();
return;
}
// Check if the file MIME type is 'application/pdf'
if (!file.ContentType.Equals("application/pdf", StringComparison.OrdinalIgnoreCase))
{
statusMessage = "Only PDF files are allowed.";
isSuccess = false;
StateHasChanged();
return;
}
using var memoryStream = new MemoryStream();
await file.OpenReadStream().CopyToAsync(memoryStream);
var fileContent = memoryStream.ToArray();
var userInfo = await AuthenticationService.GetUserInformation();
var response = await PatientManagementService.UploadMedicalHistoryAsync(userInfo.Id, fileContent);
statusMessage = response.Message;
isSuccess = response.StatusCode == HttpStatusCodes.Created; // Assume HttpStatusCodes.Created represents successful upload
await CheckMedicalHistory();
StateHasChanged();
}
private void PromptFileUpdate()
{
JSRuntime.InvokeVoidAsync("document.getElementById", "hiddenFileInput");
}
private async Task DeleteFile()
{
if (uploadedFile != null)
{
var success = await PatientManagementService.DeleteMedicalHistoryAsync(uploadedFile.Id);
if (success)
{
uploadedFile = null;
statusMessage = "File deleted successfully.";
isSuccess = true;
StateHasChanged();
}
else
{
statusMessage = "Failed to delete file.";
isSuccess = false;
}
}
}
private async Task DownloadFile()
{
if (uploadedFile != null)
{
var fileContent = await PatientManagementService.DownloadMedicalHistoryAsync(uploadedFile.Id);
if (fileContent != null)
{
var base64 = Convert.ToBase64String(fileContent);
var href = $"data:application/octet-stream;base64,{base64}";
await JSRuntime.InvokeVoidAsync("downloadFile", "MedicalHistory.pdf", href);
}
}
}
}