@using AyCode.Core.Consts @using System.Linq.Expressions @using System.ComponentModel.DataAnnotations @using AyCode.Services.Loggers @using System.Reflection @using AyCode.Blazor.Components.Components @using TIAM.Entities.Products @using TIAM.Entities.Transfers @using TIAM.Models.Dtos.Users @using TIAMSharedUI.Shared @using TIAMWebApp.Shared.Application.Interfaces @using TIAMWebApp.Shared.Application.Utility @inject IEnumerable LogWriters @inject ISessionService _sessionService @if (isEditing) {

@TitleString

@CreateEditFormFields()
} else {

Details

@* Create new *@
@CreateCardView()
}

@FormSubmitResult

@code { [Parameter] public object? Data { get; set; } [Parameter] public List IgnoreReflection { get; set; } [Parameter] public EventCallback OnSubmit { get; set; } [Parameter] public bool isEditing { get; set; } = false; [Parameter] public bool userAvailable { get; set; } [Parameter] public UserModelDtoDetail userModelDtodetail { get; set; } [Parameter] public Product? CurrentProduct { get; set; } = null; [Parameter] public string TitleString { get; set; } = "Edit your details"; [Parameter] public string ButtonTextString { get; set; } = "Submit"; string _formSubmitResult = ""; private string _spinnerClass = ""; string FormSubmitResult = ""; private LoggerClient _logger; string PhoneMask { get; set; } = AcRegExpression.PhoneNumberMask; string EmailMask { get; set; } = AcRegExpression.EmailMask; protected override void OnInitialized() { _logger = new LoggerClient(LogWriters.ToArray()); base.OnInitialized(); } protected override void OnParametersSet() { StateHasChanged(); base.OnParametersSet(); } // void HandleValidSubmit() // { // FormSubmitResult = "You have been registered successfully."; // isEditing = false; // Stop editing after successful submission // } async Task HandleValidSubmit() { //_spinnerClass = "spinner-border spinner-border-sm"; //await Task.Delay(500); var debugString = "Success: "; var myType = Data.GetType(); IList props = new List(myType.GetProperties()); foreach (var prop in props) { var propValue = prop.GetValue(Data, null); // Do something with propValue debugString += $"{prop.Name} = {propValue}\n"; } _formSubmitResult = debugString; _spinnerClass = ""; await OnSubmit.InvokeAsync(Data); isEditing = false; } void HandleInvalidSubmit() { FormSubmitResult = "Please correct all errors"; } void StartEditing() { isEditing = true; } public RenderFragment CreateCardView() => cardViewBuilder => { var mytype = Data.GetType(); var propertyList = mytype.GetProperties(); foreach (var property in propertyList) { if (IgnoreReflection.Contains(property.Name)) { continue; } var displayLabel = (DisplayAttribute)property.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault(); cardViewBuilder.OpenElement(0, "p"); cardViewBuilder.AddContent(1, $"{displayLabel?.Name ?? property.Name}: {property.GetValue(Data)}"); cardViewBuilder.CloseElement(); } }; public RenderFragment CreateEditFormFields() => formLayoutBuilder => { _logger.Debug($"Data type: {Data.GetType().FullName}"); var mytype = Data.GetType(); var propertyList = mytype.GetProperties(); _logger.Debug($"Data property list count: {propertyList.Length}"); formLayoutBuilder.OpenComponent(0); formLayoutBuilder.AddAttribute(1, "ChildContent", (RenderFragment)((layoutItemBuilder) => { int i = 0; foreach (var property in propertyList) { if (IgnoreReflection.Contains(property.Name)) { continue; } var attrList = (DataTypeAttribute)property.GetCustomAttributes(typeof(DataTypeAttribute), false).FirstOrDefault(); var displayLabel = (DisplayAttribute)property.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault(); layoutItemBuilder.OpenComponent(i++); layoutItemBuilder.AddAttribute(i++, "Caption", displayLabel?.Name ?? property.Name); layoutItemBuilder.AddAttribute(i++, "ColSpanMd", 12); var access = Expression.Property(Expression.Constant(Data), property.Name); var lambda = Expression.Lambda(typeof(Func<>).MakeGenericType(property.PropertyType), access); layoutItemBuilder.AddAttribute(i++, "Template", (RenderFragment)((context) => ((editor) => { var j = 0; switch (attrList?.DataType) { case DataType.Text: editor.OpenComponent(j++); editor.AddAttribute(j++, "Text", property.GetValue(Data)); editor.AddAttribute(j++, "TextExpression", lambda); editor.AddAttribute(j++, "CssClass", "form-field"); editor.AddAttribute(j++, "TextChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); })); editor.CloseComponent(); break; case DataType.Password: editor.OpenComponent(j++); editor.AddAttribute(j++, "Password", true); editor.AddAttribute(j++, "NullText", "Password"); editor.AddAttribute(j++, "Text", property.GetValue(Data)); editor.AddAttribute(j++, "CssClass", "form-field"); editor.AddAttribute(j++, "TextExpression", lambda); editor.AddAttribute(j++, "TextChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); })); editor.CloseComponent(); break; case DataType.PhoneNumber: editor.OpenComponent>(j++); editor.AddAttribute(j++, "Value", property.GetValue(Data)); editor.AddAttribute(j++, "MaskMode", MaskMode.RegEx); editor.AddAttribute(j++, "Mask", PhoneMask); editor.AddAttribute(j++, "BindValueMode", BindValueMode.OnInput); editor.AddAttribute(j++, "NullText", "+11234567890"); editor.AddAttribute(j++, "MaskAutoCompleteMode", MaskAutoCompleteMode.None); editor.AddAttribute(j++, "ValueExpression", lambda); editor.AddAttribute(j++, "CssClass", "form-field"); editor.AddAttribute(j++, "ValueChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); })); editor.CloseComponent(); break; case DataType.Date: editor.OpenComponent>(j); editor.AddAttribute(j++, "Date", property.GetValue(Data)); editor.AddAttribute(j++, "DateExpression", lambda); editor.AddAttribute(j++, "CssClass", "form-field"); editor.AddAttribute(j++, "TimeSectionVisible", true); editor.AddAttribute(j++, "TimeSectionScrollPickerFormat", "tt h m"); editor.AddAttribute(j++, "DateChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); })); editor.CloseComponent(); break; case DataType.Custom: if (property.PropertyType == typeof(double)) { editor.OpenComponent>(j); editor.AddAttribute(j++, "Value", property.GetValue(Data)); editor.AddAttribute(j++, "Mask", "n6"); editor.AddAttribute(j++, "BindValueMode", BindValueMode.OnInput); editor.AddAttribute(j++, "ValueExpression", lambda); editor.AddAttribute(j++, "CssClass", "form-field"); editor.AddAttribute(j++, "ValueChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); })); editor.CloseComponent(); break; } else if (property.PropertyType == typeof(int)) { editor.OpenComponent>(j); editor.AddAttribute(j++, "Value", property.GetValue(Data)); editor.AddAttribute(j++, "BindValueMode", BindValueMode.OnInput); editor.AddAttribute(j++, "Mask", NumericMask.WholeNumber); editor.AddAttribute(j++, "ValueExpression", lambda); editor.AddAttribute(j++, "CssClass", "form-field"); editor.AddAttribute(j++, "ValueChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); })); editor.CloseComponent(); break; } else if (property.PropertyType == typeof(IEnumerable) && property.Name == "Occupation") { editor.OpenComponent>(j); editor.AddAttribute(j++, "Data", AdditionalData.Occupations); editor.AddAttribute(j++, "Value", property.GetValue(Data)); editor.AddAttribute(j++, "ValueExpression", lambda); editor.AddAttribute(j++, "ValueChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); })); editor.CloseComponent(); break; } else if (property.PropertyType == typeof(string) && string.Compare(attrList.CustomDataType, "TransferDestination", true) == 0) { editor.OpenComponent(j); editor.AddAttribute(j++, "TextValue", property.GetValue(Data)); editor.AddAttribute(j++, "CssClass", "form-field"); if (CurrentProduct != null) { editor.AddAttribute(j++, "ProductId", CurrentProduct.Id); } // editor.AddAttribute(j++, "ValExpression", lambda); editor.AddAttribute(j++, "OnSliderChanged", EventCallback.Factory.Create(this, result => { _logger.Debug($"Slider changed to {result}"); property.SetValue(Data, result); _logger.DetailConditional($"bleh: {property.Name} = {property.GetValue(Data)}"); })); editor.CloseComponent(); } else if (property.PropertyType == typeof(string) && string.Compare(attrList.CustomDataType, "FullName", true) == 0) { editor.OpenComponent(j); editor.AddAttribute(j++, "NullText", ""); editor.AddAttribute(j++, "FirstNameChanged", EventCallback.Factory.Create(this, result => { _logger.DetailConditional($"FirstName changed to {result}"); //find property with name FirstName var firstNameProperty = propertyList.FirstOrDefault(p => p.Name == "FirstName"); firstNameProperty.SetValue(Data, result); //find property with name LastName var lastNameProperty = propertyList.FirstOrDefault(p => p.Name == "LastName"); //combine the two values, if they are not null if (firstNameProperty != null && lastNameProperty != null) { var firstName = result; var lastName = (string)lastNameProperty.GetValue(Data); var fullName = $"{firstName} {lastName}"; property.SetValue(Data, fullName); } })); editor.AddAttribute(j++, "LastNameChanged", EventCallback.Factory.Create(this, result => { _logger.DetailConditional($"LastName changed to {result}"); //find property with name FirstName var firstNameProperty = propertyList.FirstOrDefault(p => p.Name == "FirstName"); //find property with name LastName var lastNameProperty = propertyList.FirstOrDefault(p => p.Name == "LastName"); lastNameProperty.SetValue(Data, result); //combine the two values, if they are not null if (firstNameProperty != null && lastNameProperty != null) { var firstName = (string)firstNameProperty.GetValue(Data); var lastName = result; var fullName = $"{firstName} {lastName}"; property.SetValue(Data, fullName); } _logger.DetailConditional($"bleh: {property.Name} = {property.GetValue(Data)}"); StateHasChanged(); // Add this line to refresh the UI })); editor.CloseComponent(); editor.OpenComponent(j++); /*editor.AddAttribute(j++, "CssClass", "form-field");*/ editor.AddAttribute(j++, "NullText", "Please type in the above fields"); editor.AddAttribute(j++, "Enabled", false); editor.AddAttribute(j++, "Text", property.GetValue(Data)); editor.AddAttribute(j++, "TextExpression", lambda); editor.AddAttribute(j++, "TextChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); _logger.DetailConditional($"bleh: {property.Name} = {property.GetValue(Data)}"); })); editor.CloseComponent(); } break; case DataType.MultilineText: editor.OpenComponent(j); editor.AddAttribute(j++, "Text", property.GetValue(Data)); editor.AddAttribute(j++, "CssClass", "form-field"); editor.AddAttribute(j++, "TextExpression", lambda); editor.AddAttribute(j++, "TextChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); })); editor.CloseComponent(); break; default: editor.OpenComponent(j++); editor.AddAttribute(j++, "Text", property.GetValue(Data)); editor.AddAttribute(j++, "CssClass", "form-field"); editor.AddAttribute(j++, "TextExpression", lambda); editor.AddAttribute(j++, "TextChanged", EventCallback.Factory.Create(this, str => { property.SetValue(Data, str); })); editor.CloseComponent(); break; } }))); layoutItemBuilder.CloseComponent(); layoutItemBuilder.OpenElement(i++, "div"); layoutItemBuilder.AddAttribute(i++, "class", "text-danger"); layoutItemBuilder.OpenComponent(i++, typeof(ValidationMessage<>).MakeGenericType(property.PropertyType)); layoutItemBuilder.AddAttribute(i++, "For", lambda); layoutItemBuilder.CloseComponent(); layoutItemBuilder.CloseElement(); } layoutItemBuilder.OpenComponent(i++); layoutItemBuilder.AddAttribute(i++, "Template", (RenderFragment)((context) => ((editor) => { editor.OpenComponent(i++); editor.AddAttribute(i++, "SubmitFormOnClick", true); editor.AddAttribute(i++, "Text", ButtonTextString); editor.CloseComponent(); }))); layoutItemBuilder.CloseComponent(); })); formLayoutBuilder.CloseComponent(); }; }