@page "/patient/doctor_access" @attribute [Authorize(Roles = UserRoles.Patient)] @layout PatientLayout @inject IPatientManagementService PatientManagementService @inject IAuthenticationService AuthenticationService @inject NavigationManager NavigationManager

Manage Access to Medical History

@if (!string.IsNullOrEmpty(statusMessage)) { }
@foreach (var doctor in doctors) {
Status: @if (doctorHasAccess(doctor.Id)) { Granted } else { Revoked }
Doctor
Dr. @doctor.Name
}
@code { private string statusMessage; private bool isSuccess; private List doctors = new(); private readonly List grantedAccessDoctors = new(); private Guid medicalRecordId; protected override async Task OnInitializedAsync() { await LoadDoctorsAndAccessStatus(); } private async Task LoadDoctorsAndAccessStatus() { var userInfo = await AuthenticationService.GetUserInformation(); var medicalHistory = await PatientManagementService.GetMedicalHistoryAsync(userInfo.Id); if (medicalHistory != null) { medicalRecordId = medicalHistory.Id; doctors = await PatientManagementService.GetAllDoctorsAsync(); await UpdateDoctorAccessStatuses(userInfo.Id); } else { statusMessage = "Failed to load medical history."; isSuccess = false; } } private async Task UpdateDoctorAccessStatuses(Guid patientId) { if (medicalRecordId != Guid.Empty) { foreach (var doctor in doctors) { if (await PatientManagementService.CheckForAccessAsync(medicalRecordId, doctor.Id)) { grantedAccessDoctors.Add(doctor.Id); } } StateHasChanged(); } } private bool doctorHasAccess(Guid doctorId) { return grantedAccessDoctors.Contains(doctorId); } private async Task GrantAccess(Guid doctorId) { if (medicalRecordId != Guid.Empty) { var success = await PatientManagementService.GrantAccessAsync(medicalRecordId, doctorId); if (success) { grantedAccessDoctors.Add(doctorId); statusMessage = "Access granted successfully."; isSuccess = true; } else { statusMessage = "Failed to grant access."; isSuccess = false; } StateHasChanged(); } } private async Task RevokeAccess(Guid doctorId) { if (medicalRecordId != Guid.Empty) { var success = await PatientManagementService.RevokeAccessAsync(medicalRecordId, doctorId); if (success) { grantedAccessDoctors.Remove(doctorId); statusMessage = "Access revoked successfully."; isSuccess = true; } else { statusMessage = "Failed to revoke access."; isSuccess = false; } StateHasChanged(); } } }