Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Edit Form #1488

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Nox.Generator.Tasks/Ui/DataGrid/EntityDataGrid.scriban-cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public partial class {{className}} : ComponentBase
private List<{{entity.Name}}Model> {{entity.PluralName}} = new();
private bool IsLoading = true;

[Parameter]
public EventCallback<{{entity.Name}}Model?> OnSelectionChanged { get; set; }

protected override async Task OnInitializedAsync()
{
await LoadData();
Expand All @@ -32,4 +35,12 @@ public partial class {{className}} : ComponentBase
await LoadData();
StateHasChanged();
}

public async Task SelectedOnClick({{entity.Name}}Model? currentSelection)
{
if (currentSelection != null)
{
await OnSelectionChanged.InvokeAsync(currentSelection);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
Style="border-style:solid;border-width:2px;border-color:lightgray;"
Dense="true">
<HeaderContent>
{{- if entity.Persistence.Update.IsEnabled }}
<MudTh>Select</MudTh>
{{- end }}
{{- for attribute in entity.Attributes }}
{{- if attributesToShowInSearch | !array.contains attribute.Name }}
{{- continue; }}
Expand All @@ -25,6 +28,11 @@
{{- end }}
</HeaderContent>
<RowTemplate>
{{- if entity.Persistence.Update.IsEnabled }}
<MudTd>
<MudIconButton Icon="@Icons.Material.Filled.Edit" Color="Color.Info" OnClick="@(e=>SelectedOnClick(context))" />
</MudTd>
{{- end }}
{{- for attribute in entity.Attributes }}
{{- if attributesToShowInSearch | !array.contains attribute.Name }}
{{- continue; }}
Expand Down
78 changes: 78 additions & 0 deletions src/Nox.Generator.Tasks/Ui/Forms/EditEntityForm.scriban-cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Microsoft.AspNetCore.Components;
using MudBlazor;

using {{codeGenConventions.UiNameSpace}}.Models;

namespace {{codeGenConventions.UiNameSpace}}.Forms.Edit;

public partial class {{className}} : ComponentBase
{
private bool IsLoading = false;
private bool HasError = false;

[Parameter]
public {{entity.Name}}Model {{entity.Name}} { get; set; } = new();

[Parameter]
public EventCallback OnSubmit { get; set; }

[Parameter]
public EventCallback OnCancel { get; set; }

[Parameter]
public bool IsVisible { get; set; }

[Parameter]
public EventCallback<bool> IsVisibleChanged { get; set; }

[Parameter]
public DialogOptions EditDialogOptions { get; set; } = new()
{
FullWidth = true,
ClassBackground = "custom-dialog",
DisableBackdropClick = true,
Position = DialogPosition.TopCenter,
MaxWidth = MaxWidth.Large
};

private async Task OnSubmitClicked()
{
try
{
IsLoading = true;

if ({{entity.Name}} != null)
{
var result = await CountriesService.UpdateAsync({{entity.Name}});

if (result is null)
HasError = true;
else
{
ResetForm();
await OnSubmit.InvokeAsync();
}
}
}
catch
{
HasError = true;
}
finally
{
IsLoading = false;
}
}

private async Task OnCancelClicked()
{
ResetForm();
await OnCancel.InvokeAsync();
}

private void ResetForm()
{
HasError = false;
IsLoading = false;
}
}
37 changes: 37 additions & 0 deletions src/Nox.Generator.Tasks/Ui/Forms/EditEntityFormGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Nox.Generator.Common;
using Nox.Solution;
using System.Linq;

namespace Nox.Generator.Tasks.Ui.Forms;

internal class EditEntityFormGenerator : INoxFileGenerator
{
public NoxGeneratorKind GeneratorKind => NoxGeneratorKind.Ui;

public void Generate(
NoxCodeGenConventions codeGeneratorState,
GeneratorConfig config,
System.Action<string> log,
string absoluteOutputPath
)
{
if (codeGeneratorState.Solution.Domain is null)
{
return;
}

var templateName = @"Tasks.Ui.Forms.EditEntityForm";
var entities = codeGeneratorState.Solution.Domain.Entities
.Where(e => !e.IsOwnedEntity && e.Persistence.Create.IsEnabled);

foreach (var entity in entities)
{
new TaskTemplateFileBuilder(codeGeneratorState, absoluteOutputPath)
.WithFileExtension("razor.cs")
.WithClassName($"Edit{entity.Name}Form")
.WithFileNamePrefix($"Ui.Forms.Edit")
.WithObject("entity", entity)
.GenerateSourceCodeFromResource(templateName);
}
}
}
72 changes: 72 additions & 0 deletions src/Nox.Generator.Tasks/Ui/Forms/EditEntityFormRazor.scriban-cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{{- func showOnCreate(attribute)
ret attribute.UserInterface?.ShowOnCreateForm == true
end -}}
{{- func attributePrimitiveType(attribute)
ret componentsInfo[attribute.Name].ComponentType | string.remove 'System.'
end -}}

@using NoxResources = Nox.Ui.Blazor.Lib.Resources.Resources
@using Nox.Ui.Blazor.Lib.Components.Generic
@using Nox.Ui.Blazor.Lib.Components.NoxTypes
@using {{codeGenConventions.DtoNameSpace}}

@using {{codeGenConventions.UiNameSpace}}.Services

@namespace {{codeGenConventions.UiNameSpace}}.Forms.Edit

@inject I{{entity.PluralName}}Service {{entity.PluralName}}Service

<MudDialog @bind-IsVisible="@IsVisible" Options="@EditDialogOptions">
<TitleContent>
<MudText Typo="Typo.h6">@string.Format(NoxResources.AddEntity, "{{entity.Name}}")</MudText>
</TitleContent>
<DialogContent>
<MudForm Model="@{{entity.Name}}">
<MudGrid Style="margin-top:15px;">
{{- for key in entity.Keys }}
{{- if !IsNoxTypeCreatable key.Type || !(showOnCreate key) }}
{{- continue; }}
{{- end }}
<MudItem xs="12">
<Edit{{key.Type}} @bind-{{key.Type}}="{{entity.Name}}.{{key.Name}}" Title="{{key.Name}}" />
</MudItem>
{{- end }}
{{- for attribute in entity.Attributes }}
{{- if !IsNoxTypeCreatable attribute.Type || !(showOnCreate attribute) }}
{{- continue; }}
{{- end }}
<MudItem xs="12">
<Edit{{attribute.Type}} @bind-{{attribute.Type}}="{{entity.Name}}.{{attribute.Name}}"
Title="{{attribute.Name}}"
{{- if attribute.Type == "Number" }} T="{{attributePrimitiveType attribute}}"{{ end }}
{{- if attributesWithTypeOptions | array.contains attribute.Name }} TypeOptions="@{{entity.Name}}Metadata.{{attribute.Name}}TypeOptions" {{ end -}}
/>
</MudItem>
{{- end }}
</MudGrid>
</MudForm>
@if (HasError) {
<ErrorSummary />
<MudText Color="Color.Error">ERROR</MudText>
}
</DialogContent>
<DialogActions>
<MudButton OnClick="@OnCancelClicked"
Variant="Variant.Outlined"
Color="Color.Info"
Style="Margin:15px;">
@NoxResources.Cancel.ToUpper()
</MudButton>
<MudButton OnClick="@OnSubmitClicked"
Variant="Variant.Filled"
Color="Color.Info"
Style="Margin:15px;"
Disabled="@IsLoading">
<MudText>@NoxResources.Submit.ToUpper()</MudText>
@if (IsLoading)
{
<MudProgressCircular Color="Color.Default" Indeterminate="true" Size="Size.Small" />
}
</MudButton>
</DialogActions>
</MudDialog>
58 changes: 58 additions & 0 deletions src/Nox.Generator.Tasks/Ui/Forms/EditEntityFormRazorGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Nox.Generator.Common;
using Nox.Solution;
using Nox.Types;
using Nox.Types.Extensions;
using System.Linq;

namespace Nox.Generator.Tasks.Ui.Forms;

internal class EditEntityFormRazorGenerator : INoxFileGenerator
{
public NoxGeneratorKind GeneratorKind => NoxGeneratorKind.Ui;

public void Generate(
NoxCodeGenConventions codeGeneratorState,
GeneratorConfig config,
System.Action<string> log,
string absoluteOutputPath
)
{
if (codeGeneratorState.Solution.Domain is null)
{
return;
}

var templateName = @"Tasks.Ui.Forms.EditEntityFormRazor";
var entities = codeGeneratorState.Solution.Domain.Entities
.Where(e => !e.IsOwnedEntity && e.Persistence.Create.IsEnabled);

foreach (var entity in entities)
{
var componentsInfo = entity.Attributes
.ToDictionary(r => r.Name, key => new {
ComponentType = key.Type.GetComponents(key).FirstOrDefault().Value
});

var attributesWithTypeOptions = entity.Attributes.Where(a => HasTypeOptions(a)).Select(a => a.Name);

new TaskTemplateFileBuilder(codeGeneratorState, absoluteOutputPath)
.WithFileExtension("razor")
.WithClassName($"Edit{entity.Name}Form")
.WithFileNamePrefix($"Ui.Forms.Edit")
.WithObject("entity", entity)
.WithObject("componentsInfo", componentsInfo)
.WithObject("attributesWithTypeOptions", attributesWithTypeOptions)
.GenerateSourceCodeFromResource(templateName);
}
}

private bool HasTypeOptions(NoxSimpleTypeDefinition attribute)
{
var typeOptionsName = $"{attribute.Type}TypeOptions";
var propertyInfo = typeof(NoxSimpleTypeDefinition).GetProperty(typeOptionsName);
if (propertyInfo is null) return false;

object propertyValue = propertyInfo.GetValue(attribute);
return propertyValue is not null;
}
}
4 changes: 4 additions & 0 deletions src/Nox.Generator.Tasks/Ui/Models/EntityModel.scriban-cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ public abstract class {{className}}Base: IEntityModel
/// </summary>
public virtual {{attributeType attribute}}? {{attribute.Name}} { get; set; }
{{- end }}

[JsonPropertyName("@odata.etag")]
public System.Guid Etag { get; set; }

}
43 changes: 39 additions & 4 deletions src/Nox.Generator.Tasks/Ui/Pages/EntityPage.scriban-cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@using Nox.Ui.Blazor.Lib.Components.Generic
@using Nox.Ui.Blazor.Lib.Services
@using Nox.Ui.Blazor.Lib.Resources
@using {{codeGenConventions.UiNameSpace}}.Forms.Add
@using {{codeGenConventions.UiNameSpace}}.Forms.Edit
@using {{codeGenConventions.UiNameSpace}}.DataGrid
@using {{codeGenConventions.UiNameSpace}}.Models

@namespace {{codeGenConventions.UiNameSpace}}.Pages

Expand All @@ -19,23 +22,28 @@
<MudText>Search section</MudText>
</MudItem>
{{- if entity.Persistence.Create.IsEnabled }}
<MudItem xs="12">
<Actions OnClick="ShowAddDialog" />
<MudItem xs="12" Style="margin:15px 0px 15px 0px;text-align:right;">
<Actions OnClick="ShowAddDialog" Title="@Resources.Add.ToUpper()" StartIcon="@Icons.Material.Filled.Add" />
</MudItem>
<Add{{entity.Name}}Form @bind-IsVisible="showAddDialog" OnSubmit="Add{{entity.Name}}Submit" OnCancel="HideAddDialog" />
{{- end }}
{{- if entity.Persistence.Update.IsEnabled }}
<Edit{{entity.Name}}Form @bind-IsVisible="showEditDialog" OnSubmit="Edit{{entity.Name}}Submit" OnCancel="HideEditDialog" {{entity.Name}}="selectedEntity" />
{{- end }}
{{- if entity.Persistence.Read.IsEnabled }}
<MudItem xs="12">
<{{entity.PluralName}}DataGrid @ref="referencedDataGrid" />
<{{entity.PluralName}}DataGrid @ref="referencedDataGrid" OnSelectionChanged="@(e=>ShowEditDialog(e))" />
</MudItem>
{{- end }}
</MudGrid>

@code{
{{- if entity.Persistence.Create.IsEnabled }}

private {{entity.PluralName}}DataGrid? referencedDataGrid;

#region ADD
private bool showAddDialog = false;
private {{entity.PluralName}}DataGrid? referencedDataGrid;

private void ShowAddDialog()
{
Expand All @@ -54,4 +62,31 @@
}
#endregion ADD
{{- end }}

{{- if entity.Persistence.Update.IsEnabled }}
#region EDIT
private bool showEditDialog = false;

[Parameter]
public {{entity.Name}}Model? selectedEntity { get; set; }

private void ShowEditDialog({{entity.Name}}Model? currentSelected)
{
selectedEntity = currentSelected;
showEditDialog = true;
}

private void HideEditDialog()
{
showEditDialog = false;
}

private void Edit{{entity.Name}}Submit()
{
showEditDialog = false;
selectedEntity = null;
referencedDataGrid?.RefreshDataGrid();
}
#endregion ADD
{{- end }}
}
Loading
Loading