RobotNet/RobotNet.WebApp/Scripts/Components/Dialogs/RenameFileOrFolderDialog.razor
2025-10-15 15:15:53 +07:00

108 lines
3.4 KiB
Plaintext

@inject ISnackbar Snackbar
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">
@Title
</MudText>
</TitleContent>
<DialogContent>
<div class="d-flex flex-column" style="width: 500px;">
<MudTextField @bind-Value="FileName" Label="@Label" Class="mb-3" ShrinkLabel Variant="Variant.Outlined"
DebounceInterval="500" OnDebounceIntervalElapsed="ValidateName" OnBlur="ValidateName"
Error="@NameError" ErrorText="@NameErrorText" Adornment="@AdornmentVisible" AdornmentText=".cs" />
</div>
</DialogContent>
<DialogActions>
<MudButton OnClick="OnCancel">Cancel</MudButton>
<MudButton Color="Color.Primary" OnClick="OnRename" Disabled="@RenameDisabled">Rename</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter]
private IMudDialogInstance Dialog { get; set; } = null!;
[Parameter]
public ScriptWorkspace Workspace { get; set; } = null!;
private bool IsFolder;
private string Label = "";
private string Title => IsFolder ? "Rename folder" : "Rename file";
private string NameErrorText = "";
private bool NameError = false;
private string FileName = "";
private string OldName = "";
private bool RenameDisabled => FileName == OldName;
private Adornment AdornmentVisible => IsFolder ? Adornment.None : Adornment.End;
protected override void OnInitialized()
{
base.OnInitialized();
if (Workspace.SelectedFile is not null)
{
IsFolder = false;
Label = Workspace.SelectedFile.Parrent?.Name ?? "/";
FileName = Workspace.SelectedFile.Name;
if (FileName.EndsWith(".cs"))
{
FileName = FileName.Substring(0, FileName.Length - 3);
}
OldName = FileName;
}
else if (Workspace.SelectedFolder is not null)
{
IsFolder = true;
Label = Workspace.SelectedFolder.Parrent?.Name ?? "/";
FileName = Workspace.SelectedFolder.Name;
OldName = FileName;
}
}
private void ValidateName()
{
NameErrorText = string.IsNullOrEmpty(FileName) ? "Tên không được để trống" : "";
NameError = !string.IsNullOrEmpty(NameErrorText);
}
private void OnCancel() => Dialog.Cancel();
private async Task OnRename()
{
ValidateName();
if (NameError)
{
Snackbar.Add("Tên chưa đúng!", Severity.Error);
}
else
{
if (Workspace.SelectedFile is not null)
{
var result = await Workspace.Rename(Workspace.SelectedFile, FileName.EndsWith(".cs") ? FileName : FileName + ".cs");
if (result.IsSuccess)
{
Dialog.Close();
}
else
{
Snackbar.Add(result.Message, Severity.Error);
}
}
else if (Workspace.SelectedFolder is not null)
{
var result = await Workspace.Rename(Workspace.SelectedFolder, FileName);
if (result.IsSuccess)
{
Dialog.Close();
}
else
{
Snackbar.Add(result.Message, Severity.Error);
}
}
}
}
}