using System.Net.Http.Json; using JdeScoping.Core.ViewModels; using Microsoft.AspNetCore.Components; using Radzen; namespace JdeScoping.Client.Components.DataSync; /// /// Dialog component for creating new manual sync requests. /// public partial class NewSyncRequestDialog : ComponentBase { /// /// Gets or sets the Radzen dialog service for closing the dialog. /// [Inject] private DialogService DialogService { get; set; } = default!; /// /// Gets or sets the HTTP client for API calls. /// [Inject] private HttpClient HttpClient { get; set; } = default!; /// /// Gets or sets the available pipelines to select from. /// [Parameter] public List Pipelines { get; set; } = new(); private string? _selectedPipeline; private string? _selectedSyncType; private List _availableSyncTypes = new(); private bool _isCreating; /// /// Determines if the Create button should be enabled. /// private bool CanCreate => !string.IsNullOrEmpty(_selectedPipeline) && !string.IsNullOrEmpty(_selectedSyncType) && !_isCreating; /// /// Handles pipeline selection change by filtering available sync types. /// private void OnPipelineChanged() { // Reset sync type when pipeline changes _selectedSyncType = null; if (string.IsNullOrEmpty(_selectedPipeline)) { _availableSyncTypes = new(); return; } // Find the selected pipeline and get its supported sync types var pipeline = Pipelines.FirstOrDefault(p => p.Name == _selectedPipeline); _availableSyncTypes = pipeline?.SupportedSyncTypes ?? new(); } /// /// Handles the Cancel button click by closing the dialog with null result. /// private void OnCancelAsync() { DialogService.Close(null); } /// /// Handles the Create button click by calling the API and closing with the result. /// private async Task OnCreateAsync() { if (!CanCreate) { return; } _isCreating = true; StateHasChanged(); try { var createDto = new CreateManualSyncRequestDto { PipelineName = _selectedPipeline!, SyncType = _selectedSyncType! }; var response = await HttpClient.PostAsJsonAsync("api/manual-sync", createDto); if (response.IsSuccessStatusCode) { var createdRequest = await response.Content.ReadFromJsonAsync(); DialogService.Close(createdRequest); } else { // On error, close with null (parent can show notification if needed) DialogService.Close(null); } } catch { // On exception, close with null DialogService.Close(null); } finally { _isCreating = false; } } }