-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardDialogBase.cs
More file actions
137 lines (120 loc) · 4.37 KB
/
CardDialogBase.cs
File metadata and controls
137 lines (120 loc) · 4.37 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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;
using System.Net;
namespace JMayer.Example.WebAssemblyBlazor.Client.Components.Base;
/// <summary>
/// The class manages user interaction for an add/edit dialog associated with a card.
/// </summary>
/// <typeparam name="T">Must be a UserEditableDataObject.</typeparam>
/// <typeparam name="U">Must be a IUserEditableDataLayer.</typeparam>
public class CardDialogBase<T, U> : ComponentBase
where T : SubUserEditableDataObject, new()
where U : ISubUserEditableDataLayer<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 add/edit.
/// </summary>
[Parameter]
public 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 if the data object is a new record.
/// </summary>
[Parameter]
public bool IsNewRecord { get; set; }
/// <summary>
/// The property gets/sets a reference to the mud dialog.
/// </summary>
[CascadingParameter]
protected IMudDialogInstance MudDialog { get; set; } = default!;
/// <summary>
/// The property gets/sets the id of who owns the created sub data object.
/// </summary>
/// <remarks>
/// This is only used when creating a new sub data object.
/// </remarks>
[Parameter]
public long OwnerId { get; set; }
/// <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 OnParametersSet()
{
//For new records, set the owner to the value set when the dialog is opened.
if (DataObject.OwnerInteger64ID == 0)
{
DataObject.OwnerInteger64ID = OwnerId;
}
EditContext = new(DataObject);
base.OnParametersSet();
}
/// <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;
if (IsNewRecord)
{
operationResult = await DataLayer.CreateAsync(DataObject);
}
else
{
operationResult = await DataLayer.UpdateAsync(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 if (operationResult.StatusCode == HttpStatusCode.Conflict)
{
await DialogService.ShowEditConflictMessageAsync();
}
else
{
await DialogService.ShowErrorMessageAsync($"Failed to {(IsNewRecord ? "create" : "update")} the object because of an error on the server.");
}
}
catch
{
await DialogService.ShowErrorMessageAsync("Failed to communicate with the server.");
}
}
}