Skip to content

Commit

Permalink
Support for DeleteBaseDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Jan 11, 2024
1 parent 5030b6f commit cbf415b
Showing 1 changed file with 131 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
@using EasyMicroservices.ServiceContracts
@using EasyMicroservices.UI.Cores
@using MudBlazor
@inject ISnackbar Snackbar

@typeparam TItem

<BaseDialog @ref="deleteDialog">
<TitleContent>
<MudText Typo="Typo.h6">
<MudIcon Icon="@Icons.Material.Filled.Edit" Class="mr-3" /> @BaseViewModel.GetTranslatedByKey("Delete_Title")
</MudText>
</TitleContent>
<DialogContent>
@BaseViewModel.GetTranslatedByKey("DeleteQuestion_Content")
</DialogContent>
<DialogActions>
@if (!BindViewModel.IsBusy)
{
<MudButton OnClick="() => deleteDialog.CloseDialog()">
@BindViewModel.GetInnerTranslatedByKey("Cancel")
</MudButton>
}
<MudButton Color="Color.Error" Variant="Variant.Filled" Disabled="BindViewModel.IsBusy" OnClick="()=>Delete()">
@if (BindViewModel.IsBusy)
{
<MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" />
<MudText Class="ms-2">@BindViewModel.GetInnerTranslatedByKey("Deleting")</MudText>
}
else
{
<MudText>@BindViewModel.GetInnerTranslatedByKey("Delete")</MudText>
}
</MudButton>
</DialogActions>
</BaseDialog>
@code {
BaseDialog deleteDialog;
MudDialog dialog;
TItem selectedItem;

[Parameter]
[Category("Behavior")]
public string SuccessMessage { get; set; }

[Parameter]
[Category("Behavior")]
public BaseViewModel BindViewModel { get; set; }

[Parameter]
[Category("Behavior")]
public Func<TItem, Task<MessageContract>> OnDeleteAsync { get; set; }

[Parameter]
[Category("Behavior")]
public Func<TItem, MessageContract> OnDelete { get; set; }

void Delete()
{
BindViewModel.IsBusy = true;
DoDelete();
}

async void DoDelete()
{
try
{
if (OnDelete != null)
{
var result = OnDelete.Invoke(selectedItem);
if (result)
ShowSucess();
else
{
ShowError(result.Error);
}
}

if (OnDeleteAsync != null)
{
var result = await OnDeleteAsync.Invoke(selectedItem);
if (result)
ShowSucess();
else
{
ShowError(result.Error);
}
}
}
catch (Exception ex)
{
ShowError(ex);
}
finally
{
BindViewModel.IsBusy = false;
StateHasChanged();
}
}

public void ShowDeleteDialog(TItem item, Func<TItem, MessageContract> onDelete)
{
OnDeleteAsync = null;
OnDelete = onDelete;
ShowDeleteDialog(item);
}

public void ShowDeleteDialogAsync(TItem item, Func<TItem, Task<MessageContract>> onDeleteAsync)
{
OnDelete = null;
OnDeleteAsync = onDeleteAsync;
ShowDeleteDialog(item);
}

void ShowDeleteDialog(TItem item)
{
selectedItem = item;
deleteDialog.ShowDialog();
}

void ShowSucess()
{
Snackbar.Add(SuccessMessage, Severity.Success, key: Guid.NewGuid().ToString());
deleteDialog.CloseDialog();
}

void ShowError(ErrorContract errorContract)
{
Snackbar.Add(errorContract.EndUserMessage ?? errorContract.Message, Severity.Error, key: Guid.NewGuid().ToString());
}
}

0 comments on commit cbf415b

Please sign in to comment.