@using Microsoft.AspNetCore.Components.WebAssembly.Authentication @using RobotNet.RobotShares.OpenACS @inject IHttpClientFactory HttpClientFactory @inject ISnackbar Snackbar Traffic Setting
Update
@code { [Parameter, EditorRequired] public OpenACSTrafficSettingDto SettingDto { get; set; } = new(false, "", []); private OpenACSTrafficSettingDto NewSettingDto { get; set; } = new(false, "", []); private bool OverlayIsVisible; public void UpdateSettings(OpenACSTrafficSettingDto settings) { SettingDto = settings; NewSettingDto = new() { IsTrafficEnabled = SettingDto.IsTrafficEnabled, TrafficUrl = SettingDto.TrafficUrl, PublishUrlsUsed = SettingDto.PublishUrlsUsed, }; StateHasChanged(); } private async Task> UrlSearch(string value, CancellationToken token) { await Task.Delay(5, token); if (string.IsNullOrEmpty(value)) { return SettingDto.PublishUrlsUsed; } return SettingDto.PublishUrlsUsed.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase)); } private async Task Update() { try { if (NewSettingDto.TrafficUrl == SettingDto.TrafficUrl) { Snackbar.Add("Không có thay đổi nào để cập nhật", Severity.Warning); return; } OverlayIsVisible = true; StateHasChanged(); using var Http = HttpClientFactory.CreateClient("RobotManagerAPI"); var result = await (await Http.PostAsJsonAsync($"api/OpenACSSettings/traffic", new OpenACSTrafficSettingModel(NewSettingDto.TrafficUrl))).Content.ReadFromJsonAsync>(); if (result is null) { Snackbar.Add("Lỗi giao tiếp với hệ thống", Severity.Warning); return; } else if (!result.IsSuccess) { Snackbar.Add(result.Message ?? "Cập nhật không thành công", Severity.Warning); return; } else if (result.Data == null) { Snackbar.Add("Lỗi dữ liệu trả về", Severity.Warning); return; } SettingDto = result.Data; NewSettingDto = new() { IsTrafficEnabled = SettingDto.IsTrafficEnabled, TrafficUrl = SettingDto.TrafficUrl, PublishUrlsUsed = SettingDto.PublishUrlsUsed, }; OverlayIsVisible = false; Snackbar.Add("Cập nhật thành công", Severity.Success); StateHasChanged(); } catch (AccessTokenNotAvailableException ex) { ex.Redirect(); } } private async Task EnableChanged() { try { OverlayIsVisible = true; StateHasChanged(); using var Http = HttpClientFactory.CreateClient("RobotManagerAPI"); var result = await Http.GetFromJsonAsync($"api/OpenACSSettings/traffic?enable={NewSettingDto.IsTrafficEnabled}"); if (result is null) { Snackbar.Add("Lỗi giao tiếp với hệ thống", Severity.Warning); return; } else if (!result.IsSuccess) { Snackbar.Add(result.Message ?? "Cập nhật không thành công", Severity.Warning); return; } SettingDto.IsTrafficEnabled = NewSettingDto.IsTrafficEnabled; OverlayIsVisible = false; StateHasChanged(); } catch (AccessTokenNotAvailableException ex) { ex.Redirect(); } } }