@page "/patient/appointments"
@attribute [Authorize(Roles = UserRoles.Patient)]
@layout PatientLayout
@inject IPatientManagementService PatientManagementService;
@inject IAuthenticationService AuthenticationService;
Doctors
@if (!string.IsNullOrEmpty(_statusMessage))
{
@_statusMessage
}
@if (_doctors == null || !_doctors.Any())
{
No doctors available.
}
else
{
@foreach (var doctor in _doctors)
{
Dr. @doctor.Name
@doctor.Email
}
}
@if (_showAppointmentOverlay)
{
Book Appointment with Dr. @_selectedDoctor.Name
}
@code {
private List _doctors;
private Doctor _selectedDoctor;
private bool _showAppointmentOverlay = false;
private NewAppointmentModel _newAppointmentModel = new();
private DateAndTimeAppointment _dateAndTimeAppointment = new();
private string _statusMessage = string.Empty;
private bool _isError = false;
protected override async Task OnInitializedAsync()
{
await LoadDoctors();
}
private async Task LoadDoctors()
{
_doctors = await PatientManagementService.GetAllDoctorsAsync();
}
private void ShowAppointmentOverlay(Doctor doctor)
{
_selectedDoctor = doctor;
_newAppointmentModel = new NewAppointmentModel
{
DoctorId = doctor.Id
};
_showAppointmentOverlay = true;
}
private void HideAppointmentOverlay()
{
_showAppointmentOverlay = false;
}
private async Task BookAppointment()
{
var userInfo = await AuthenticationService.GetUserInformation();
_newAppointmentModel.PatientId = userInfo.Id;
_newAppointmentModel.Appointment = _dateAndTimeAppointment.GetAppointment();
var appointment = await PatientManagementService.BookAppointmentAsync(_newAppointmentModel);
_statusMessage = appointment.Message;
_isError = appointment.StatusCode != 201;
if (!_isError)
{
_showAppointmentOverlay = false;
}
}
class DateAndTimeAppointment
{
public DateTime AppointmentDate { get; set; } = DateTime.Now.Date;
public string FormattedAppointmentTime = DateTime.Now.ToString("HH:mm");
public DateTime GetAppointment()
{
// Parse FormattedAppointmentTime to a TimeSpan
if (TimeSpan.TryParse(FormattedAppointmentTime, out var appointmentTime))
{
// Combine AppointmentDate with appointmentTime
return AppointmentDate.Add(appointmentTime);
}
throw new FormatException("FormattedAppointmentTime is not in the correct format.");
}
}
}