-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathIndex.razor
180 lines (154 loc) · 5.77 KB
/
Index.razor
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
175
176
177
178
179
180
@page "/"
@using FormValidation.Models
@inject NavigationManager NavigationManager
@* used to generate the upload controller paths *@
@inject IJSRuntime JsInterop
@* used only for the "reset" button, for brevity *@
<div class="col-4">
@if (string.IsNullOrEmpty(SuccessMessage))
{
<EditForm EditContext="@MyEditContext" OnValidSubmit="@HandleValidSubmit" OnInvalidSubmit="@HandleInvalidSubmit">
<DataAnnotationsValidator />
<div class="form-group row">
<label for="name" class="col-sm-4 col-form-label">Name:</label>
<div class="col-sm-8">
<TelerikTextBox @bind-Value="@currentForm.Name" Id="name" width="100%" />
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-4 col-form-label">Email address:</label>
<div class="col-sm-8">
<TelerikTextBox @bind-Value="@currentForm.Email" Id="email" width="100%" />
</div>
</div>
<div class="form-group row">
<label class="col-sm-4 col-form-label">CV:</label>
<div class="col-sm-8">
<TelerikUpload SaveUrl="@SaveUrl"
RemoveUrl="@RemoveUrl"
AllowedExtensions="@( new List<string>() { ".pdf", ".docx" } )"
OnSelect="@OnSelectHandler"
OnRemove="@OnRemoveHandler"
OnSuccess="@OnSuccessHandler"
OnError="@OnErrorHandler"
OnCancel="@OnCancelHandler">
</TelerikUpload>
<div class="demo-hint pt-1 text-right">Accepted files: <strong>PDF and DOCX</strong></div>
</div>
</div>
<div class="mt-4">
<ValidationSummary />
</div>
<div class="text-right">
<TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary">Submit Application</TelerikButton>
</div>
</EditForm>
}
else
{
<div class="alert alert-success" role="alert">
@SuccessMessage
<br />
<TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary" OnClick="@BackToForm">Go back</TelerikButton>
</div>
}
</div>
@code {
// setup upload endpoints
public string SaveUrl => ToAbsoluteUrl("api/upload/save");
public string RemoveUrl => ToAbsoluteUrl("api/upload/remove");
public string ToAbsoluteUrl(string url)
{
return $"{NavigationManager.BaseUri}{url}";
}
// setup validation, this is a workaround because file validation does not exist in the framework
// see more here https://github.com/dotnet/aspnetcore/issues/18821
JobApplicationForm currentForm { get; set; }
protected EditContext MyEditContext { get; set; }
Dictionary<string, bool> FilesValidationInfo { get; set; } = new Dictionary<string, bool>();
protected override void OnInitialized()
{
currentForm = new JobApplicationForm();
MyEditContext = new EditContext(currentForm);
}
void OnSelectHandler(UploadSelectEventArgs e)
{
foreach (var item in e.Files)
{
if (!FilesValidationInfo.Keys.Contains(item.Id))
{
// nothing is assumed to be valid until the server returns an OK
FilesValidationInfo.Add(item.Id, false);
}
}
UpdateValidationModel();
}
void OnSuccessHandler(UploadSuccessEventArgs e)
{
if (e.Operation == UploadOperationType.Upload)
{
if (FilesValidationInfo.Keys.Contains(e.Files[0].Id))
{
// only when the server got the file, saved it and confirmed it is OK do we update client validation
FilesValidationInfo[e.Files[0].Id] = true;
}
}
else
{
RemoveFailedFilesFromList(e.Files);
}
UpdateValidationModel();
}
void OnRemoveHandler(UploadEventArgs e)
{
RemoveFailedFilesFromList(e.Files);
UpdateValidationModel();
}
void OnErrorHandler(UploadErrorEventArgs e)
{
RemoveFailedFilesFromList(e.Files);
UpdateValidationModel();
}
void OnCancelHandler(UploadCancelableEventArgs e)
{
RemoveFailedFilesFromList(e.Files);
UpdateValidationModel();
}
void RemoveFailedFilesFromList(List<Telerik.Blazor.Components.Upload.UploadFileInfo> files)
{
foreach (var file in files)
{
if (FilesValidationInfo.Keys.Contains(file.Id))
{
FilesValidationInfo.Remove(file.Id);
}
}
}
void UpdateValidationModel()
{
bool areAllUploadedFilesValid = false;
if (FilesValidationInfo.Keys.Count > 0 &&
!FilesValidationInfo.Values.Contains(false))
{
areAllUploadedFilesValid = true;
}
currentForm.IsResumeValid = areAllUploadedFilesValid;
// we update the validation state out of the standard form cycle and events
// so we need an EditContext that we can call upon to re-evaluate the validation
MyEditContext.Validate();
}
// UI for the demo to showcase changes to the form validation and sucess
string SuccessMessage = string.Empty;
void HandleValidSubmit()
{
SuccessMessage = "Form Submitted Successfully! We will get back to you.";
}
void HandleInvalidSubmit()
{
SuccessMessage = "";
}
async Task BackToForm()
{
await JsInterop.InvokeVoidAsync("window.location.reload");
}
}