using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using ASTRAIN.Shared.Dtos;
using ASTRAIN.Shared.Requests;
namespace ASTRAIN.Client.Pages;
public partial class Exercises
{
///
/// Optional user id from route.
///
[Parameter]
public string? UserId { get; set; }
///
/// The list of exercises for the current user.
///
private List ExerciseList { get; set; } = new();
///
/// Whether the page is currently loading data.
///
private bool IsLoading { get; set; } = true;
///
/// Whether the create exercise UI is visible.
///
private bool ShowCreateExercise { get; set; }
///
/// Name for a new exercise being created.
///
private string NewExerciseName { get; set; } = string.Empty;
///
/// The currently edited exercise id, if any.
///
private int? EditingId { get; set; }
///
/// The current edited exercise name.
///
private string EditingName { get; set; } = string.Empty;
///
protected override async Task OnInitializedAsync()
{
var ensured = await Api.EnsureUserAsync(UserId);
if (string.IsNullOrWhiteSpace(ensured))
{
return;
}
UserContext.SetUserId(ensured);
if (UserId != ensured)
{
Navigation.NavigateTo($"/{ensured}/exercises", true);
return;
}
await LoadExercisesAsync();
}
///
/// Loads exercises from the API.
///
private async Task LoadExercisesAsync()
{
IsLoading = true;
ExerciseList = await Api.GetExercisesAsync(UserContext.UserId);
IsLoading = false;
}
///
/// Creates a new exercise with the provided name.
///
private async Task CreateExerciseAsync()
{
if (string.IsNullOrWhiteSpace(NewExerciseName))
{
return;
}
var result = await Api.CreateExerciseAsync(UserContext.UserId, new ExerciseUpsertRequest(NewExerciseName));
if (result is not null)
{
ExerciseList.Add(result);
ExerciseList = ExerciseList.OrderBy(e => e.Name).ToList();
NewExerciseName = string.Empty;
}
}
///
/// Toggles the create form visibility.
///
private void ToggleCreate()
{
ShowCreateExercise = !ShowCreateExercise;
}
///
/// Begin editing the supplied exercise.
///
private void StartEdit(ExerciseDto exercise)
{
EditingId = exercise.Id;
EditingName = exercise.Name;
}
///
/// Cancel the current edit.
///
private void CancelEdit()
{
EditingId = null;
EditingName = string.Empty;
}
///
/// Save the edited exercise name.
///
private async Task SaveEditAsync(int exerciseId)
{
if (string.IsNullOrWhiteSpace(EditingName))
{
return;
}
var result = await Api.UpdateExerciseAsync(UserContext.UserId, exerciseId, new ExerciseUpsertRequest(EditingName));
if (result is not null)
{
var index = ExerciseList.FindIndex(e => e.Id == exerciseId);
if (index >= 0)
{
ExerciseList[index] = result;
ExerciseList = ExerciseList.OrderBy(e => e.Name).ToList();
}
}
CancelEdit();
}
///
/// Deletes the exercise with the given id after confirmation.
///
private async Task DeleteExerciseAsync(int exerciseId)
{
var confirmed = await JS.InvokeAsync("confirm", "Are you sure you want to delete this exercise?");
if (!confirmed) return;
await Api.DeleteExerciseAsync(UserContext.UserId, exerciseId);
ExerciseList.RemoveAll(e => e.Id == exerciseId);
}
}