125 lines
4.9 KiB
Plaintext
125 lines
4.9 KiB
Plaintext
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
|
|
@using RobotNet.RobotShares.OpenACS
|
|
|
|
@inject IHttpClientFactory HttpClientFactory
|
|
@inject ISnackbar Snackbar
|
|
|
|
<MudCard Class="w-100 me-2 position-relative mt-2" Elevation="0" Outlined Style="height: 280px">
|
|
<MudCardHeader>
|
|
<MudText Typo="Typo.h5">Traffic Setting</MudText>
|
|
</MudCardHeader>
|
|
<MudCardContent>
|
|
<div class="d-flex flex-column">
|
|
<MudSwitch @bind-Value="NewSettingDto.IsTrafficEnabled" Color="Color.Primary" Label="Enable" @bind-Value:after="EnableChanged" />
|
|
<MudAutocomplete T="string" Variant="Variant.Outlined" Label="Url" ShrinkLabel Disabled="@NewSettingDto.IsTrafficEnabled" Modal SearchFunc="UrlSearch"
|
|
CoerceText="false" CoerceValue="true" MaxItems="NewSettingDto.PublishUrlsUsed.Length" ShowProgressIndicator="true" @bind-Value="NewSettingDto.TrafficUrl">
|
|
</MudAutocomplete>
|
|
<div class="d-flex flex-row-reverse mt-4">
|
|
<div>
|
|
<MudButton Class="d-flex flex-grow-1 mt-2 mb-1 ms-2" Variant="Variant.Filled" Color="Color.Info" Disabled="@NewSettingDto.IsTrafficEnabled" OnClick="Update">Update</MudButton>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</MudCardContent>
|
|
|
|
<MudOverlay Visible="OverlayIsVisible" DarkBackground="true" Absolute="true">
|
|
<MudProgressCircular Color="Color.Secondary" Indeterminate="true" />
|
|
</MudOverlay>
|
|
</MudCard>
|
|
|
|
@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<IEnumerable<string>> 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<MessageResult<OpenACSTrafficSettingDto>>();
|
|
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<MessageResult>($"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();
|
|
}
|
|
}
|
|
}
|