@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using RobotNet.RobotShares.OpenACS
@inject IHttpClientFactory HttpClientFactory
@inject ISnackbar Snackbar
Publish Setting
@code {
[Parameter, EditorRequired]
public OpenACSPublishSettingDto SettingDto { get; set; } = new(false, "", [], 2000);
private OpenACSPublishSettingDto NewSettingDto { get; set; } = new(false, "", [], 2000);
private bool OverlayIsVisible;
public void UpdateSettings(OpenACSPublishSettingDto settings)
{
SettingDto = settings;
NewSettingDto = new()
{
IsPublishEnabled = SettingDto.IsPublishEnabled,
PublishInterval = SettingDto.PublishInterval,
PublishUrl = SettingDto.PublishUrl,
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.PublishUrl == SettingDto.PublishUrl && NewSettingDto.PublishInterval == SettingDto.PublishInterval)
{
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/publish", new OpenACSPublishSettingModel(NewSettingDto.PublishUrl, NewSettingDto.PublishInterval))).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()
{
IsPublishEnabled = SettingDto.IsPublishEnabled,
PublishInterval = SettingDto.PublishInterval,
PublishUrl = SettingDto.PublishUrl,
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/publish?enable={NewSettingDto.IsPublishEnabled}");
if (result is null) { Snackbar.Add("Lỗi giao tiếp với hệ thống", Severity.Error); return; }
else if (!result.IsSuccess) { Snackbar.Add(result.Message ?? "Cập nhật không thành công", Severity.Warning); return; }
SettingDto.IsPublishEnabled = NewSettingDto.IsPublishEnabled;
OverlayIsVisible = false;
StateHasChanged();
}
catch (AccessTokenNotAvailableException ex)
{
ex.Redirect();
}
}
}