-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpsertCustomFieldModal.cshtml.cs
More file actions
177 lines (154 loc) · 5.74 KB
/
UpsertCustomFieldModal.cshtml.cs
File metadata and controls
177 lines (154 loc) · 5.74 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
175
176
177
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using Unity.Flex.Web.Views.Shared.Components.CustomFieldDefinitionWidget;
using Unity.Flex.Worksheets;
using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form;
namespace Unity.Flex.Web.Pages.WorksheetConfiguration;
public class UpsertCustomFieldModalModel(ICustomFieldAppService customFieldAppService,
IWorksheetSectionAppService worksheetSectionAppService,
IWorksheetListAppService worksheetListAppService) : FlexPageModel
{
[BindProperty]
public Guid WorksheetId { get; set; }
[BindProperty]
public Guid SectionId { get; set; }
[BindProperty]
public Guid? FieldId { get; set; }
[DisplayName("Name (Key)")]
[BindProperty]
[MinLength(1)]
[MaxLength(25)]
[RegularExpression("^([a-zA-Z0-9]*)$", ErrorMessage = "Illegal character found in name. Please enter a valid name")]
[Required]
public string? Key { get; set; }
[BindProperty]
[MinLength(1)]
[MaxLength(50)]
[Required]
public string? Label { get; set; }
[BindProperty]
public uint Order { get; set; }
[BindProperty]
public string? Definition { get; set; }
[BindProperty]
public bool Published { get; set; }
[BindProperty]
public WorksheetUpsertAction UpsertAction { get; set; }
[BindProperty]
public bool IsDelete { get; set; }
[SelectItems(nameof(FieldTypes))]
[Required]
[BindProperty]
public string? FieldType { get; set; }
public List<SelectListItem>? FieldTypes { get; set; }
public async Task OnGetAsync(Guid worksheetId, Guid sectionId, Guid fieldId, string actionType)
{
WorksheetId = worksheetId;
SectionId = sectionId;
FieldTypes = GetAvailableFieldTypes();
UpsertAction = Enum.Parse<WorksheetUpsertAction>(actionType);
FieldType = "Text";
if (UpsertAction == WorksheetUpsertAction.Update)
{
CustomFieldDto customField = await customFieldAppService.GetAsync(fieldId);
var worksheet = await worksheetListAppService.GetAsync(worksheetId);
Key = customField.Key;
Label = customField.Label;
FieldId = fieldId;
Published = worksheet.Published;
FieldType = customField.Type.ToString();
Definition = customField.Definition;
}
}
public async Task<IActionResult> OnPostAsync()
{
var delete = Request.Form["deleteCustomFieldBtn"];
var save = Request.Form["saveCustomFieldBtn"];
if (delete == "delete" || IsDelete)
{
await customFieldAppService.DeleteAsync(FieldId!.Value);
return new OkObjectResult(new ModalResponse()
{
CustomFieldId = FieldId!.Value,
WorksheetId = WorksheetId,
Action = "Delete"
});
}
else if (save == "save")
{
switch (UpsertAction)
{
case WorksheetUpsertAction.Insert:
return MapModalResponse(await InsertCustomField());
case WorksheetUpsertAction.Update:
return MapModalResponse(await UpdateCustomField());
default:
break;
}
}
return NoContent();
}
private async Task<CustomFieldDto> InsertCustomField()
{
return await worksheetSectionAppService.CreateCustomFieldAsync(SectionId, new CreateCustomFieldDto()
{
Definition = ExtractDefinition(),
Label = Label!,
Key = Key!,
Type = Enum.Parse<CustomFieldType>(FieldType!)
});
}
private async Task<CustomFieldDto> UpdateCustomField()
{
return await customFieldAppService.EditAsync(FieldId!.Value, new EditCustomFieldDto()
{
Definition = ExtractDefinition(),
Label = Label!,
Key = Key!,
Type = Enum.Parse<CustomFieldType>(FieldType!)
});
}
private object? ExtractDefinition()
{
var fieldType = Enum.TryParse(FieldType, out CustomFieldType type);
if (!fieldType) return null;
return CustomFieldDefinitionWidget.ParseFormValues(type, Request.Form);
}
private OkObjectResult MapModalResponse(CustomFieldDto customFieldDto)
{
return new OkObjectResult(new ModalResponse()
{
CustomFieldId = customFieldDto.Id,
WorksheetId = WorksheetId
});
}
private static List<SelectListItem> GetAvailableFieldTypes()
{
// Tailored list in specific order of the available fields enum
return [new SelectListItem("Text", "Text"),
new SelectListItem("Text Area", "TextArea"),
new SelectListItem("Number", "Numeric"),
new SelectListItem("Currency", "Currency"),
new SelectListItem("Date", "Date"),
new SelectListItem("Email", "Email"),
new SelectListItem("Phone", "Phone"),
new SelectListItem("Checkbox", "Checkbox"),
new SelectListItem("Checkbox Group", "CheckboxGroup"),
new SelectListItem("Select List", "SelectList"),
new SelectListItem("Radio", "Radio"),
new SelectListItem("Yes/No Select", "YesNo"),
new SelectListItem("Data Grid (Experimental)", "DataGrid"),
new SelectListItem("BC Address (Experimental)", "BCAddress")];
}
public class ModalResponse : CustomFieldDto
{
public Guid WorksheetId { get; set; }
public Guid CustomFieldId { get; set; }
public string Action { get; set; } = string.Empty;
}
}