Skip to content

Commit b701184

Browse files
renemadsenclaude
andcommitted
fix(visual-editor): load FieldTypes from DB instead of hardcoded enum
The visual editor crashed with TypeError when looking up field types because the hardcoded EformFieldTypesEnum ordinals don't necessarily match the auto-incremented FieldTypes.Id values in the database. Backend: - New GET /api/template-columns/field-types endpoint returns all rows from the FieldTypes table (Id, Type, Description) - Added FieldTypeModel DTO, ITemplateColumnsService.GetFieldTypes(), and EFormColumnsService implementation Frontend: - Added EFormService.getFieldTypes() to call the new endpoint - VisualEditorFieldComponent now loads DB field types on init and uses them for the fieldTypeTranslation lookup (matching by DB Id instead of hardcoded enum value) - Removed dependency on getTranslatedTypes() in this component Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a158f4f commit b701184

File tree

6 files changed

+61
-5
lines changed

6 files changed

+61
-5
lines changed

eFormAPI/eFormAPI.Web/Abstractions/Eforms/ITemplateColumnsService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ public interface ITemplateColumnsService
3333
Task<OperationDataResult<List<TemplateColumnModel>>> GetAvailableColumns(int templateId);
3434
Task<OperationDataResult<DisplayTemplateColumnsModel>> GetCurrentColumns(int templateId);
3535
Task<OperationResult> UpdateColumns(UpdateTemplateColumnsModel model);
36+
Task<OperationDataResult<List<FieldTypeModel>>> GetFieldTypes();
3637
}

eFormAPI/eFormAPI.Web/Controllers/Eforms/EFormColumnsController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ public async Task<OperationDataResult<DisplayTemplateColumnsModel>> GetCurrentCo
5656
return await templateColumnsService.GetCurrentColumns(templateId);
5757
}
5858

59+
[HttpGet]
60+
[Route("field-types")]
61+
public async Task<OperationDataResult<List<FieldTypeModel>>> GetFieldTypes()
62+
{
63+
return await templateColumnsService.GetFieldTypes();
64+
}
65+
5966
[HttpPost]
6067
[Authorize(Policy = AuthConsts.EformPolicies.Eforms.UpdateColumns)]
6168
public async Task<IActionResult> UpdateColumns([FromBody] UpdateTemplateColumnsModel model)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace eFormAPI.Web.Infrastructure.Models.Templates;
2+
3+
public class FieldTypeModel
4+
{
5+
public int Id { get; set; }
6+
public string Type { get; set; }
7+
public string Description { get; set; }
8+
}

eFormAPI/eFormAPI.Web/Services/EFormColumnsService.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace eFormAPI.Web.Services;
3131
using System;
3232
using System.Collections.Generic;
3333
using System.Linq;
34+
using Microsoft.EntityFrameworkCore;
3435
using System.Threading.Tasks;
3536
using Abstractions;
3637
using Abstractions.Eforms;
@@ -165,4 +166,28 @@ public async Task<OperationResult> UpdateColumns(UpdateTemplateColumnsModel mode
165166
return new OperationResult(false, localizationService.GetString("ErrorWhileUpdatingColumns"));
166167
}
167168
}
169+
170+
public async Task<OperationDataResult<List<FieldTypeModel>>> GetFieldTypes()
171+
{
172+
try
173+
{
174+
var core = await coreHelper.GetCore();
175+
await using var dbContext = core.DbContextHelper.GetDbContext();
176+
var fieldTypes = await dbContext.FieldTypes
177+
.Select(ft => new FieldTypeModel
178+
{
179+
Id = ft.Id,
180+
Type = ft.Type,
181+
Description = ft.Description
182+
})
183+
.ToListAsync();
184+
return new OperationDataResult<List<FieldTypeModel>>(true, fieldTypes);
185+
}
186+
catch (Exception e)
187+
{
188+
SentrySdk.CaptureException(e);
189+
logger.LogError(e.Message);
190+
return new OperationDataResult<List<FieldTypeModel>>(false, e.Message);
191+
}
192+
}
168193
}

eform-client/src/app/common/services/eform/eform.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ export class EFormService {
9999
return this.apiBaseService.post(TemplateColumnMethods.GetColumns, model);
100100
}
101101

102+
getFieldTypes(): Observable<OperationDataResult<{id: number; type: string; description: string}[]>> {
103+
return this.apiBaseService.get(TemplateColumnMethods.GetColumns + '/field-types');
104+
}
105+
102106
downloadEformXML(templateId: number): Observable<any> {
103107
return this.apiBaseService.getBlobData(
104108
TemplateFilesMethods.DownloadXML + '/' + templateId

eform-client/src/app/modules/eforms/eform-visual-editor/components/eform-visual-editor-elements/field/visual-editor-field/visual-editor-field.component.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import {
99
} from 'src/app/common/models';
1010
import {
1111
eformVisualEditorElementColors,
12-
getTranslatedTypes
1312
} from '../../../../const';
14-
import { LocaleService } from 'src/app/common/services';
13+
import { LocaleService, EFormService } from 'src/app/common/services';
1514
import {TranslateService} from '@ngx-translate/core';
1615
import {getRandomInt} from 'src/app/common/helpers';
1716
import {selectCurrentUserLanguageId} from 'src/app/state/auth/auth.selector';
@@ -26,6 +25,7 @@ import {Store} from '@ngrx/store';
2625
export class VisualEditorFieldComponent implements OnInit, OnDestroy {
2726
private authStore = inject(Store);
2827
private translateService = inject(TranslateService);
28+
private eformService = inject(EFormService);
2929

3030
@Input() field: EformVisualEditorFieldModel;
3131
@Input() fieldIndex: number;
@@ -50,6 +50,8 @@ export class VisualEditorFieldComponent implements OnInit, OnDestroy {
5050
@Input() appLanguages: LanguagesModel = new LanguagesModel();
5151
private selectCurrentUserLanguageId$ = this.authStore.select(selectCurrentUserLanguageId);
5252

53+
private dbFieldTypes: {id: number; type: string}[] = [];
54+
5355
get fieldTypes() {
5456
return EformFieldTypesEnum;
5557
}
@@ -77,13 +79,22 @@ export class VisualEditorFieldComponent implements OnInit, OnDestroy {
7779
}
7880

7981
fieldTypeTranslation(fieldType: number): string {
80-
if(fieldType) {
81-
const types = [...getTranslatedTypes(this.translateService)];
82-
return types.find(x => x.id === fieldType).name;
82+
if (!fieldType) {
83+
return '';
84+
}
85+
const dbType = this.dbFieldTypes.find(x => x.id === fieldType);
86+
if (dbType) {
87+
return this.translateService.instant(dbType.type);
8388
}
89+
return '';
8490
}
8591

8692
ngOnInit() {
93+
this.eformService.getFieldTypes().subscribe(res => {
94+
if (res && res.success && res.model) {
95+
this.dbFieldTypes = res.model;
96+
}
97+
});
8798
}
8899

89100
onAddNewField() {

0 commit comments

Comments
 (0)