-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5030b6f
commit cbf415b
Showing
1 changed file
with
131 additions
and
0 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
src/CSharp/BlazorComponents/EasyMicroservices.UI.BlazorComponents/DeleteBaseDialog.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |