-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewDialogBase.cs
More file actions
101 lines (88 loc) · 3.27 KB
/
NewDialogBase.cs
File metadata and controls
101 lines (88 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using JMayer.Data.Data;
using JMayer.Data.HTTP.DataLayer;
using JMayer.Example.WebAssemblyBlazor.Client.Extensions;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using MudBlazor;
namespace JMayer.Example.WebAssemblyBlazor.Client.Components.Base;
/// <summary>
/// The class manages user interaction for a new dialog.
/// </summary>
/// <typeparam name="T">Must be a UserEditableDataObject.</typeparam>
/// <typeparam name="U">Must be a IUserEditableDataLayer.</typeparam>
public class NewDialogBase<T, U> : ComponentBase
where T : UserEditableDataObject, new()
where U : IUserEditableDataLayer<T>
{
/// <summary>
/// The property gets/sets the data layer to be used by the dialog.
/// </summary>
[Inject]
protected U DataLayer { get; set; } = default!;
/// <summary>
/// The property gets/sets the data object to create.
/// </summary>
protected T DataObject { get; set; } = new();
/// <summary>
/// The property gets/sets the dialog service used for managing MudDialogs.
/// </summary>
[Inject]
protected IDialogService DialogService { get; set; } = default!;
/// <summary>
/// The property gets/sets the edit context associated with the edit form.
/// </summary>
protected EditContext EditContext { get; set; } = default!;
/// <summary>
/// The property gets/sets a reference to the mud dialog.
/// </summary>
[CascadingParameter]
protected IMudDialogInstance MudDialog { get; set; } = default!;
/// <summary>
/// The property gets/sets a reference to the server side validation.
/// </summary>
protected ServerSideValidation ServerSideValidation { get; set; } = default!;
/// <summary>
/// The method initializes the component.
/// </summary>
protected override void OnInitialized()
{
EditContext = new(DataObject);
base.OnInitialized();
}
/// <summary>
/// The method closes the dialog with a cancel result.
/// </summary>
protected virtual void OnCancelButtonClick() => MudDialog.Cancel();
/// <summary>
/// The method attempts to create a new data object on the server.
/// </summary>
/// <returns>A Task object for the async.</returns>
protected virtual async Task OnSubmitEditFormAsync()
{
try
{
OperationResult operationResult = await DataLayer.CreateAsync(DataObject);
if (operationResult.IsSuccessStatusCode)
{
MudDialog.Close();
}
else if (operationResult.ServerSideValidationResult?.Errors.Count > 0)
{
Dictionary<string, List<string>> errors = [];
foreach (ServerSideValidationError error in operationResult.ServerSideValidationResult.Errors)
{
errors.Add(error.PropertyName, [error.ErrorMessage]);
}
ServerSideValidation.DisplayErrors(errors);
}
else
{
await DialogService.ShowErrorMessageAsync("Failed to create the object because of an error on the server.");
}
}
catch
{
await DialogService.ShowErrorMessageAsync("Failed to communicate with the server.");
}
}
}