Files
FACULTATE-HEALTHCARE_MANAGER/frontend/UI/Pages/Patient/PatientDashboard.razor
T
2024-05-30 15:40:13 +03:00

122 lines
3.4 KiB
Plaintext

@page "/patient/dashboard"
@attribute [Authorize(Roles = UserRoles.Patient)]
@layout PatientLayout
@inject IAuthenticationService AuthenticationService
@inject IPatientManagementService PatientManagementService
@inject NavigationManager NavigationManager
<h3>Dashboard</h3>
@if (errorMessage != string.Empty)
{
<div class="alert alert-danger" role="alert">
@errorMessage
</div>
}
@if (username != null)
{
<p>Welcome, @username!</p>
<p>Here you have listed your active appointments.</p>
}
@if (appointments != null && appointments.Count > 0)
{
<table class="table">
<thead>
<tr>
<th>Doctor</th>
<th>Time</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var appointment in appointments)
{
foreach (var appointmentTime in appointment.AppointmentsList)
{
<tr>
<td>Dr. @doctors[Guid.Parse(appointment.DoctorId)]</td>
<td>@appointmentTime.ToString("g")</td>
<td>
<button class="btn btn-danger" @onclick="() => DeleteAppointment(appointment, appointmentTime)">Delete</button>
</td>
</tr>
}
}
</tbody>
</table>
}
else if (errorMessage == string.Empty)
{
<p>No appointments exist.</p>
}
@code {
private string username;
private List<Appointment> appointments = new();
private Dictionary<Guid, string> doctors = new();
private string errorMessage = string.Empty;
protected override async Task OnInitializedAsync()
{
try
{
var userInfo = await AuthenticationService.GetUserInformation();
username = userInfo.Name;
appointments = await PatientManagementService.GetAppointmentsAsync(userInfo.Id);
var allDoctors = await PatientManagementService.GetAllDoctorsAsync();
doctors = allDoctors.ToDictionary(doc => doc.Id, doc => doc.Name);
if (!appointments.Any())
{
errorMessage = "No appointments exist.";
}
}
catch (Exception ex)
{
errorMessage = $"Failed to load data: {ex.Message}";
}
}
private async Task DeleteAppointment(Appointment appointment, DateTime appointmentTime)
{
try
{
var success = await PatientManagementService.CancelAppointmentAsync(
Guid.Parse(appointment.DoctorId),
Guid.Parse(appointment.PatientId),
appointmentTime
);
if (success)
{
var appList = appointments.FirstOrDefault(a => a.Id == appointment.Id)?.AppointmentsList;
if (appList != null)
{
appList.Remove(appointmentTime);
if (!appList.Any())
{
appointments.Remove(appointment);
}
}
if (!appointments.Any())
{
errorMessage = "No appointments exist.";
}
}
else
{
errorMessage = "Failed to delete the appointment.";
}
StateHasChanged();
}
catch (Exception ex)
{
errorMessage = $"Error deleting appointment: {ex.Message}";
}
}
}