| | | 1 | | using Microsoft.AspNetCore.Components; |
| | | 2 | | using Microsoft.JSInterop; |
| | | 3 | | using Pomodoro.Web.Services; |
| | | 4 | | using Pomodoro.Web.Services.Formatters; |
| | | 5 | | |
| | | 6 | | namespace Pomodoro.Web.Components.History; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Code-behind for WeeklyMiniChart component |
| | | 10 | | /// Displays a weekly bar chart using Chart.js |
| | | 11 | | /// </summary> |
| | | 12 | | public class WeeklyMiniChartBase : ComponentBase, IAsyncDisposable |
| | | 13 | | { |
| | | 14 | | #region Services (Dependency Injection) |
| | | 15 | | |
| | | 16 | | [Inject] |
| | 211 | 17 | | protected ChartService ChartService { get; set; } = default!; |
| | | 18 | | |
| | | 19 | | [Inject] |
| | 151 | 20 | | protected ChartDataFormatter ChartDataFormatter { get; set; } = default!; |
| | | 21 | | |
| | | 22 | | #endregion |
| | | 23 | | |
| | | 24 | | #region Parameters |
| | | 25 | | |
| | | 26 | | [Parameter] |
| | 137 | 27 | | public Dictionary<DateTime, int> DailyFocusMinutes { get; set; } = new(); |
| | | 28 | | |
| | | 29 | | [Parameter] |
| | 137 | 30 | | public Dictionary<DateTime, int> BreakDailyMinutes { get; set; } = new(); |
| | | 31 | | |
| | | 32 | | [Parameter] |
| | 91 | 33 | | public DateTime WeekStartDate { get; set; } |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Properties |
| | | 38 | | |
| | 218 | 39 | | protected string CanvasId { get; } = $"{Constants.Charts.WeeklyChartPrefix}{Guid.NewGuid().ToString(Constants.DateFo |
| | | 40 | | |
| | | 41 | | #endregion |
| | | 42 | | |
| | | 43 | | private bool _isRendered; |
| | | 44 | | |
| | | 45 | | #region Lifecycle Methods |
| | | 46 | | |
| | | 47 | | protected override async Task OnAfterRenderAsync(bool firstRender) |
| | 55 | 48 | | { |
| | 55 | 49 | | if (firstRender) |
| | 48 | 50 | | { |
| | 48 | 51 | | _isRendered = true; |
| | 48 | 52 | | await RenderChartAsync(); |
| | 48 | 53 | | } |
| | 55 | 54 | | } |
| | | 55 | | |
| | | 56 | | protected override async Task OnParametersSetAsync() |
| | 55 | 57 | | { |
| | 55 | 58 | | if (_isRendered) |
| | 7 | 59 | | { |
| | 7 | 60 | | await RenderChartAsync(); |
| | 7 | 61 | | } |
| | 55 | 62 | | } |
| | | 63 | | |
| | | 64 | | private async Task RenderChartAsync() |
| | 55 | 65 | | { |
| | 55 | 66 | | var (labels, focusData, breakData) = ChartDataFormatter.PrepareWeeklyChartData( |
| | 55 | 67 | | DailyFocusMinutes, BreakDailyMinutes, WeekStartDate); |
| | 55 | 68 | | await ChartService.CreateGroupedBarChartAsync(CanvasId, labels, focusData, breakData, null); |
| | 55 | 69 | | } |
| | | 70 | | |
| | | 71 | | #endregion |
| | | 72 | | |
| | | 73 | | #region IDisposable |
| | | 74 | | |
| | | 75 | | public async ValueTask DisposeAsync() |
| | 60 | 76 | | { |
| | | 77 | | try |
| | 60 | 78 | | { |
| | 60 | 79 | | await ChartService.DestroyChartAsync(CanvasId); |
| | 52 | 80 | | } |
| | 8 | 81 | | catch |
| | 8 | 82 | | { |
| | | 83 | | // Ignore disposal errors |
| | 8 | 84 | | } |
| | 60 | 85 | | } |
| | | 86 | | |
| | | 87 | | #endregion |
| | | 88 | | } |