288 lines
11 KiB
Plaintext
288 lines
11 KiB
Plaintext
@page "/sites"
|
|
@attribute [Authorize]
|
|
@layout MainLayout
|
|
@inject HeimdallApiClient Api
|
|
@inject ISnackbar Snackbar
|
|
|
|
<MudText Typo="Typo.h4" Class="mb-6">
|
|
<i class="fas fa-video mr-2"></i>
|
|
Sites
|
|
</MudText>
|
|
|
|
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(() => _showCreate = true)" Class="mb-4">
|
|
<MudIcon Icon="@Icons.Material.Filled.Add" Class="mr-1" /> Nouveau site
|
|
</MudButton>
|
|
|
|
@if (_loading)
|
|
{
|
|
<MudProgressCircular Indeterminate="true" />
|
|
}
|
|
else if (_error)
|
|
{
|
|
<MudAlert Severity="Severity.Error" Class="mb-4" Variant="Variant.Filled">
|
|
<MudText>Erreur lors du chargement des sites.</MudText>
|
|
<MudButton Variant="Variant.Text" Color="Color.Inherit" OnClick="@LoadSites" Class="mt-2">
|
|
<MudIcon Icon="@Icons.Material.Filled.Refresh" Class="mr-1" Size="Size.Small" /> Réessayer
|
|
</MudButton>
|
|
</MudAlert>
|
|
}
|
|
else
|
|
{
|
|
<MudTable Items="@_sites" Hover="true" Striped="true" Elevation="2">
|
|
<HeaderContent>
|
|
<MudTh>Nom</MudTh>
|
|
<MudTh>Adresse</MudTh>
|
|
<MudTh>Fuseau</MudTh>
|
|
<MudTh>Statut</MudTh>
|
|
<MudTh>Actions</MudTh>
|
|
</HeaderContent>
|
|
<RowTemplate>
|
|
<MudTd DataLabel="Nom"><MudLink Href="@($"/sites/{context.Id}")">@context.Name</MudLink></MudTd>
|
|
<MudTd DataLabel="Adresse">@context.Address</MudTd>
|
|
<MudTd DataLabel="Fuseau">@context.Timezone</MudTd>
|
|
<MudTd DataLabel="Statut">
|
|
<MudText Color="@(context.Enabled ? Color.Success : Color.Default)" Typo="Typo.body2">
|
|
@(context.Enabled ? "● Actif" : "○ Inactif")
|
|
</MudText>
|
|
</MudTd>
|
|
<MudTd DataLabel="Actions">
|
|
<MudTooltip Text="Générer un token d'enregistrement pour ce site">
|
|
<MudIconButton Icon="@Icons.Material.Filled.ContentCopy" Size="Size.Small"
|
|
OnClick="@(() => CopyToken(context.Id))" />
|
|
</MudTooltip>
|
|
<MudTooltip Text="Supprimer ce site">
|
|
<MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Small" Color="Color.Error"
|
|
OnClick="@(() => ConfirmDeleteSite(context.Id, context.Name))" />
|
|
</MudTooltip>
|
|
</MudTd>
|
|
</RowTemplate>
|
|
<NoRecordsContent>
|
|
<MudText Align="Align.Center" Class="py-4">Aucun site.</MudText>
|
|
</NoRecordsContent>
|
|
</MudTable>
|
|
}
|
|
|
|
@if (_showCreate)
|
|
{
|
|
<MudOverlay Visible="@_showCreate" OnClick="@(() => _showCreate = false)" />
|
|
<MudDialog @bind-Visible="_showCreate">
|
|
<TitleContent><MudText Typo="Typo.h6">Nouveau site</MudText></TitleContent>
|
|
<DialogContent>
|
|
<MudTextField @bind-Value="_newName" Label="Nom du site *" Variant="Variant.Outlined"
|
|
Class="mb-4" Required="true" Immediate="true" />
|
|
<MudTextField @bind-Value="_newAddress" Label="Adresse" Variant="Variant.Outlined" Class="mb-4" />
|
|
<MudTextField @bind-Value="_newTimezone" Label="Fuseau horaire" Variant="Variant.Outlined"
|
|
Class="mb-4" HelperText="Ex: Europe/Paris (défaut: UTC)" />
|
|
@if (!string.IsNullOrEmpty(_creationError))
|
|
{
|
|
<MudAlert Severity="Severity.Error" Class="mb-4">@_creationError</MudAlert>
|
|
}
|
|
@if (!string.IsNullOrEmpty(_createdToken))
|
|
{
|
|
<MudAlert Severity="Severity.Success" Class="mb-4">
|
|
<strong>Token d'enregistrement :</strong><br />
|
|
<code class="d-block mt-2 p-2" style="background:#f5f5f5;word-break:break-all;font-size:0.9rem;">
|
|
@_createdToken
|
|
</code>
|
|
<MudText Typo="Typo.body2" Color="Color.Warning" Class="mt-2">
|
|
⚠ Copiez ce token maintenant. Il ne sera plus affiché.
|
|
</MudText>
|
|
</MudAlert>
|
|
}
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<MudButton Variant="Variant.Text" OnClick="@CancelCreate">Annuler</MudButton>
|
|
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@ConfirmCreate"
|
|
Disabled="@IsCreateDisabled">
|
|
@if (_saving) { <MudProgressCircular Size="Size.Small" Class="mr-1" /> }
|
|
@(!string.IsNullOrEmpty(_createdToken) ? "Fermer" : "Créer")
|
|
</MudButton>
|
|
</DialogActions>
|
|
</MudDialog>
|
|
}
|
|
|
|
@* ─── Confirm delete dialog ───────────────────────────────── *@
|
|
@if (_confirmDeleteShow)
|
|
{
|
|
<MudOverlay Visible="@_confirmDeleteShow" OnClick="@(() => _confirmDeleteShow = false)" />
|
|
<MudDialog @bind-Visible="_confirmDeleteShow">
|
|
<TitleContent><MudText Typo="Typo.h6">Confirmer la suppression</MudText></TitleContent>
|
|
<DialogContent>
|
|
<MudText>Supprimer le site "@_confirmDeleteName" ? Cette action est irréversible.</MudText>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<MudButton Variant="Variant.Text" OnClick="@(() => _confirmDeleteShow = false)">Annuler</MudButton>
|
|
<MudButton Variant="Variant.Filled" Color="Color.Error" OnClick="@ExecuteDelete">Supprimer</MudButton>
|
|
</DialogActions>
|
|
</MudDialog>
|
|
}
|
|
|
|
@* ─── Token dialog ──────────────────────────────────────────── *@
|
|
@if (_showTokenDialog)
|
|
{
|
|
<MudOverlay Visible="@_showTokenDialog" OnClick="@(() => _showTokenDialog = false)" />
|
|
<MudDialog @bind-Visible="_showTokenDialog">
|
|
<TitleContent>
|
|
<MudText Typo="Typo.h6">Token d'enregistrement</MudText>
|
|
</TitleContent>
|
|
<DialogContent>
|
|
@if (_tokenLoading)
|
|
{
|
|
<MudProgressCircular Indeterminate="true" Class="mb-4" />
|
|
<MudText>Génération du token...</MudText>
|
|
}
|
|
else if (!string.IsNullOrEmpty(_tokenError))
|
|
{
|
|
<MudAlert Severity="Severity.Error">@_tokenError</MudAlert>
|
|
}
|
|
else if (!string.IsNullOrEmpty(_generatedToken))
|
|
{
|
|
<MudAlert Severity="Severity.Info">
|
|
<MudText Typo="Typo.body2" Class="mb-2">
|
|
Utilisez ce token pour enregistrer un agent sur ce site :
|
|
</MudText>
|
|
<code class="d-block mt-2 p-2" style="background:#f5f5f5;word-break:break-all;font-size:0.9rem;">
|
|
@_generatedToken
|
|
</code>
|
|
<MudText Typo="Typo.body2" Color="Color.Warning" Class="mt-2">
|
|
⚠ Ce token expire dans 24h. Copiez-le maintenant.
|
|
</MudText>
|
|
</MudAlert>
|
|
}
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<MudButton Variant="Variant.Filled" Color="Color.Primary"
|
|
OnClick="@(() => _showTokenDialog = false)">
|
|
Fermer
|
|
</MudButton>
|
|
</DialogActions>
|
|
</MudDialog>
|
|
}
|
|
|
|
@code {
|
|
private IReadOnlyList<SiteItem>? _sites;
|
|
private bool _loading = true;
|
|
private bool _error;
|
|
private bool _showCreate;
|
|
private bool _saving;
|
|
private string _newName = "";
|
|
private string? _newAddress;
|
|
private string? _newTimezone;
|
|
private string _creationError = "";
|
|
private string _createdToken = "";
|
|
private bool IsCreateDisabled => _saving || !string.IsNullOrEmpty(_createdToken);
|
|
|
|
// Token generation dialog
|
|
private bool _showTokenDialog;
|
|
private bool _tokenLoading;
|
|
private string _generatedToken = "";
|
|
private string _tokenError = "";
|
|
private Guid _tokenSiteId;
|
|
|
|
protected override async Task OnInitializedAsync() => await LoadSites();
|
|
|
|
private async Task LoadSites()
|
|
{
|
|
_loading = true;
|
|
_error = false;
|
|
try
|
|
{
|
|
var result = await Api.GetSitesAsync();
|
|
_sites = result?.Items;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_error = true;
|
|
Snackbar.Add($"Erreur : {ex.Message}", Severity.Error);
|
|
}
|
|
finally { _loading = false; }
|
|
}
|
|
|
|
private bool _confirmDeleteShow;
|
|
private Guid _confirmDeleteId;
|
|
private string _confirmDeleteName = "";
|
|
|
|
private void ConfirmDeleteSite(Guid id, string name)
|
|
{
|
|
_confirmDeleteId = id;
|
|
_confirmDeleteName = name;
|
|
_confirmDeleteShow = true;
|
|
}
|
|
|
|
private async Task ExecuteDelete()
|
|
{
|
|
_confirmDeleteShow = false;
|
|
await DeleteSite(_confirmDeleteId, _confirmDeleteName);
|
|
}
|
|
|
|
private void CancelCreate()
|
|
{
|
|
_showCreate = false;
|
|
_creationError = "";
|
|
_createdToken = "";
|
|
_newName = "";
|
|
_newAddress = null;
|
|
_newTimezone = null;
|
|
}
|
|
|
|
private async Task ConfirmCreate()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(_newName)) { _creationError = "Le nom est requis"; return; }
|
|
_saving = true;
|
|
_creationError = "";
|
|
try
|
|
{
|
|
var result = await Api.CreateSiteAsync(_newName, _newAddress, _newTimezone);
|
|
if (result is null) { _creationError = "Erreur lors de la création"; return; }
|
|
_createdToken = result.RegistrationToken ?? "Token non disponible";
|
|
Snackbar.Add("Site créé avec succès", Severity.Success);
|
|
await LoadSites();
|
|
}
|
|
catch (Exception ex) { _creationError = $"Erreur : {ex.Message}"; }
|
|
finally { _saving = false; }
|
|
}
|
|
|
|
private async Task CopyToken(Guid siteId)
|
|
{
|
|
_tokenSiteId = siteId;
|
|
_tokenLoading = true;
|
|
_tokenError = "";
|
|
_generatedToken = "";
|
|
_showTokenDialog = true;
|
|
StateHasChanged();
|
|
|
|
try
|
|
{
|
|
var result = await Api.GenerateSiteTokenAsync(siteId);
|
|
if (result is null)
|
|
{
|
|
_tokenError = "Erreur lors de la génération du token";
|
|
}
|
|
else
|
|
{
|
|
_generatedToken = result.Token;
|
|
Snackbar.Add("Token généré avec succès", Severity.Success);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_tokenError = $"Erreur : {ex.Message}";
|
|
}
|
|
finally
|
|
{
|
|
_tokenLoading = false;
|
|
}
|
|
}
|
|
|
|
private async Task DeleteSite(Guid id, string name)
|
|
{
|
|
if (await Api.DeleteSiteAsync(id))
|
|
{
|
|
Snackbar.Add($"Site \"{name}\" supprimé", Severity.Success);
|
|
await LoadSites();
|
|
}
|
|
else
|
|
Snackbar.Add("Erreur lors de la suppression", Severity.Error);
|
|
}
|
|
}
|