Files
2024-05-30 15:40:13 +03:00

143 lines
4.2 KiB
Plaintext

@page "/patient/medical_history"
@attribute [Authorize(Roles = UserRoles.Patient)]
@layout PatientLayout
@inject IPatientManagementService PatientManagementService
@inject IAuthenticationService AuthenticationService
@inject NavigationManager NavigationManager
@inject IJSRuntime JSRuntime
<script src="fileHelper.js"></script>
<script src="downloadFile.js"></script>
<h3>Medical History</h3>
@if (!string.IsNullOrEmpty(statusMessage))
{
<div class="alert @(isSuccess ? "alert-success" : "alert-danger")" role="alert">
@statusMessage
</div>
}
@if (uploadedFile != null)
{
<div class="card mt-4">
<div class="card-header">
Medical History
</div>
<div class="card-body">
<img src="pngtree-pdf-file-icon-png-png-image_4899509.png" style="width:100px" alt="Medical Document" id="medicalDocument"/>
<div class="mt-4">
<button class="btn btn-primary" @onclick="PromptFileUpdate">Update</button>
<button class="btn btn-danger" @onclick="DeleteFile">Delete</button>
<button class="btn btn-secondary" @onclick="DownloadFile">Download</button>
</div>
</div>
</div>
}
else
{
<div class="card mt-4">
<div class="card-header">
Upload Medical History
</div>
<div class="card-body">
<InputFile OnChange="HandleFileSelected"/>
</div>
</div>
}
@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);
}
}
}
}