@using Indotalent.ConfigBackEnd.Extensions @using Indotalent.Features.Utilities.Todo @using Indotalent.Features.Utilities.Todo.Cqrs @using Indotalent.Features.Utilities.Todo.Components @using Indotalent.Shared.Utils @using MudBlazor @inject TodoService TodoService @inject ISnackbar Snackbar
@(ReadOnly ? "Todo Details" : "Edit Todo") @(ReadOnly ? "Viewing todo specification." : "Modify existing todo information.")
Todo Name Start Date Start Time End Date End Time Description <_TodoItemDataTable Items="_items" TodoId="@(_model.Id ?? string.Empty)" ReadOnly="ReadOnly" OnChanged="RefreshItems" /> Audit History Created At @DateTimeExtensions.ToString(_model.CreatedAt) Created By @(!string.IsNullOrEmpty(_model.CreatedBy) ? _model.CreatedBy : "System") Last Updated At @DateTimeExtensions.ToString(_model.UpdatedAt) Last Updated By @(!string.IsNullOrEmpty(_model.UpdatedBy) ? _model.UpdatedBy : "System")
@(ReadOnly ? "Back to List" : "Cancel") @if (!ReadOnly) { @if (_processing) { Updating... } else { Save Changes } }
@code { [Parameter] public UpdateTodoRequest Data { get; set; } = new(); [Parameter] public bool ReadOnly { get; set; } = false; [Parameter] public EventCallback OnCancel { get; set; } [Parameter] public EventCallback OnSuccess { get; set; } private MudForm _form = default!; private UpdateTodoValidator _validator = new(); private UpdateTodoRequest _model = new(); private List _items = new(); private bool _processing = false; private DateTime? _startDate; private TimeSpan? _startTime; private DateTime? _endDate; private TimeSpan? _endTime; protected override async Task OnInitializedAsync() { _model = new UpdateTodoRequest { Id = Data.Id, Name = Data.Name, Description = Data.Description, StartTime = Data.StartTime, EndTime = Data.EndTime, IsCompleted = Data.IsCompleted, CreatedAt = Data.CreatedAt, CreatedBy = Data.CreatedBy, UpdatedAt = Data.UpdatedAt, UpdatedBy = Data.UpdatedBy }; _startDate = _model.StartTime?.Date; _startTime = _model.StartTime?.TimeOfDay; _endDate = _model.EndTime?.Date; _endTime = _model.EndTime?.TimeOfDay; await RefreshItems(); } private async Task RefreshItems() { var response = await TodoService.GetTodoByIdAsync(_model.Id!); if (response != null && response.IsSuccess) { _items = response.Value?.TodoItems ?? new(); } } private async Task Submit() { if (ReadOnly) return; if (_startDate.HasValue && _startTime.HasValue) _model.StartTime = _startDate.Value.Date.Add(_startTime.Value); if (_endDate.HasValue && _endTime.HasValue) _model.EndTime = _endDate.Value.Date.Add(_endTime.Value); await _form.Validate(); if (!_form.IsValid) return; _processing = true; try { var response = await TodoService.UpdateTodoAsync(_model); await Task.Delay(500); if (response != null && response.IsSuccess) { Snackbar.Add("Todo updated successfully", Severity.Success); await OnSuccess.InvokeAsync(); } } catch (Exception ex) { Snackbar.Add($"Error: {ex.Message}", Severity.Error); } finally { _processing = false; } } }