-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleDialog.razor
More file actions
174 lines (155 loc) · 8.14 KB
/
RuleDialog.razor
File metadata and controls
174 lines (155 loc) · 8.14 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
@using Tilework.LoadBalancing.Enums
@using Tilework.LoadBalancing.Models
@using Tilework.Core.Enums
@using System.Linq
@using System.Collections.Generic
@namespace Tilework.Ui.Components.Dialogs
<MudDialog>
<TitleContent>
<MudText Typo="Typo.h6">
<MudIcon Icon="@Icon" Class="mr-3 mb-n1" />
@Title
</MudText>
</TitleContent>
<DialogContent>
<MudForm @ref="form">
<MudGrid Spacing="3">
<MudItem xs="12">
<MudCard Outlined="true">
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.subtitle1"><strong>Details</strong></MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudNumericField T="int" @bind-Value="Rule.Priority" Label="Priority" Variant="Variant.Outlined"
HideSpinButtons="true" Required="true" Class="mb-4" />
</MudCardContent>
</MudCard>
</MudItem>
<MudItem xs="12">
<MudCard Outlined="true">
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.subtitle1"><strong>Conditions</strong></MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
@foreach (var condition in Rule.Conditions)
{
<ConditionForm Condition="condition"
OnDelete="@(() => RemoveCondition(condition))" />
<MudDivider/>
}
<MudStack AlignItems="AlignItems.Start" Row>
<MudMenu Disabled="@(!CanAddCondition)" AnchorOrigin="Origin.BottomCenter" Class="mt-4">
<ActivatorContent>
<MudTooltip Disabled="@(AllowedConditionTypes.Any())" Text="This protocol does not support rule conditions.">
<MudButton Disabled="@(!CanAddCondition)" Variant="Variant.Outlined" Color="Color.Primary" StartIcon="@Icons.Material.Filled.Add">
Add condition
</MudButton>
</MudTooltip>
</ActivatorContent>
<ChildContent>
@foreach (var type in AvailableConditionTypes)
{
<MudMenuItem OnClick="@(() => AddCondition(type))">@type.GetDescription()</MudMenuItem>
}
</ChildContent>
</MudMenu>
</MudStack>
</MudCardContent>
</MudCard>
</MudItem>
<MudItem xs="12">
<MudCard Outlined="true">
<MudCardHeader>
<CardHeaderContent>
<MudText Typo="Typo.subtitle1"><strong>Action</strong></MudText>
</CardHeaderContent>
</MudCardHeader>
<MudCardContent>
<MudSelect T="RuleActionType" @bind-Value="Rule.Action.Type" Label="Action" Variant="Variant.Outlined" AnchorOrigin="Origin.BottomCenter" Required="true" Class="mb-4">
@foreach (var type in AllowedActionTypes)
{
<MudSelectItem T="RuleActionType" Value="@type">@type.GetDescription()</MudSelectItem>
}
</MudSelect>
@if (Rule.Action?.Type == RuleActionType.Forward)
{
<MudSelect T="Guid?" @bind-Value="SelectedTargetGroup" Label="Target group" Variant="Variant.Outlined" AnchorOrigin="Origin.BottomCenter" Required="true">
@foreach (var group in TargetGroups)
{
<MudSelectItem T="Guid?" Value="@group.Id">@group.Name</MudSelectItem>
}
</MudSelect>
}
else if (Rule.Action?.Type == RuleActionType.Redirect)
{
<MudTextField T="string" @bind-Value="Rule.Action.RedirectUrl" Label="Redirect URL" Variant="Variant.Outlined" Required="true" Class="mb-4" />
<MudNumericField T="int?" @bind-Value="Rule.Action.RedirectStatusCode" Label="Redirect status code" Variant="Variant.Outlined" HideSpinButtons="true" Required="true" />
}
else if (Rule.Action?.Type == RuleActionType.FixedResponse)
{
<MudNumericField T="int?" @bind-Value="Rule.Action.FixedResponseStatusCode" Label="Response status code" Variant="Variant.Outlined" HideSpinButtons="true" Required="true" Class="mb-4" />
<MudTextField T="string" @bind-Value="Rule.Action.FixedResponseContentType" Label="Content type" Variant="Variant.Outlined" Class="mb-4" />
<MudTextField T="string" @bind-Value="Rule.Action.FixedResponseBody" Label="Response body" Variant="Variant.Outlined" Lines="3" />
}
</MudCardContent>
</MudCard>
</MudItem>
</MudGrid>
</MudForm>
</DialogContent>
<DialogActions>
<MudButton OnClick="Cancel">Cancel</MudButton>
<MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit">@ButtonText</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter] private IMudDialogInstance MudDialog { get; set; }
[Parameter] public RuleDTO Rule { get; set; } = new();
[Parameter] public List<TargetGroupDTO> TargetGroups { get; set; } = new();
[Parameter] public LoadBalancerProtocol Protocol { get; set; }
[Parameter] public LoadBalancerType Type { get; set; }
[Parameter] public string Title { get; set; } = "Add rule";
[Parameter] public string ButtonText { get; set; } = "Add";
[Parameter] public string Icon { get; set; } = Icons.Material.Filled.Add;
private MudForm form;
private Guid? SelectedTargetGroup { get; set; }
protected override async Task OnInitializedAsync()
{
SelectedTargetGroup = Rule.TargetGroup;
await base.OnInitializedAsync();
}
async Task Submit()
{
await form.Validate();
if (form.IsValid)
{
if (Rule.Action?.Type == RuleActionType.Forward)
{
Rule.TargetGroup = (Guid) SelectedTargetGroup!;
}
else
{
Rule.TargetGroup = null;
}
MudDialog.Close(DialogResult.Ok(true));
}
}
void Cancel() => MudDialog.Cancel();
IEnumerable<ConditionType> AllowedConditionTypes => LoadBalancerConditionRules.GetAllowedConditions(Protocol);
IEnumerable<RuleActionType> AllowedActionTypes => LoadBalancerActionRules.GetAllowedActions(Type);
IEnumerable<ConditionType> AvailableConditionTypes =>
AllowedConditionTypes.Except(Rule.Conditions.Select(c => c.Type));
bool CanAddCondition => AvailableConditionTypes.Any();
void AddCondition(ConditionType type)
{
Rule.Conditions.Add(new Condition { Type = type });
}
void RemoveCondition(Condition condition)
{
Rule.Conditions.Remove(condition);
}
}