Skip to content

Commit

Permalink
Refactor code by renaming and moving classes
Browse files Browse the repository at this point in the history
Renamed 'VehiculeActivity', 'VehiculeUIHandler' to 'VehicleActivity', 'VehicleUIHandler' and moved them to their separate files. A debouncer was added to InputsTab in the WorkflowDefinitionEditor to limit the rate of firing refresh requests. Optimized codes by reducing unnecessary lines and improved consistency in the 'Program.cs' file.
  • Loading branch information
sfmskywalker committed Dec 29, 2023
1 parent cfb3d03 commit d635226
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 112 deletions.
64 changes: 13 additions & 51 deletions src/samples/aspnet/Elsa.Samples.AspNet.CustomUIHandler/Program.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
using Elsa;
using Elsa.EntityFrameworkCore.Extensions;
using Elsa.EntityFrameworkCore.Modules.Management;
using Elsa.EntityFrameworkCore.Modules.Runtime;
using Elsa.Extensions;
using Elsa.Http.Contracts;
using Elsa.Http.Models;
using Elsa.Samples.AspNet.CustomUIHandler;
using Microsoft.AspNetCore.Authentication;

var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration;
EndpointSecurityOptions.SecurityIsEnabled = false;
// Add services to the container.

builder.Services.AddElsa(elsa =>
{
// Configure management feature to use EF Core.
elsa.UseWorkflowManagement(management => { management.UseEntityFrameworkCore(ef => ef.UseSqlite()); });
// Configure runtime feature to use EF Core.
elsa.UseWorkflowRuntime(runtime =>
{
runtime.UseEntityFrameworkCore();
});
// Expose API endpoints.
elsa.UseWorkflowsApi(api => api.AddFastEndpointsAssembly<Program>());
// Add services for HTTP activities and workflow middleware.
elsa.UseHttp(configure =>
{
var s = configure.Services;
//s.AddSingleton<test>();
//configure.HttpEndpointAuthorizationHandler = (sp) => { return sp.GetRequiredService<test>(); };
});
// Use JavaScript and Liquid.
elsa.UseWorkflowManagement(management => management.UseEntityFrameworkCore());
elsa.UseWorkflowRuntime(runtime => runtime.UseEntityFrameworkCore());
elsa.UseWorkflowsApi();
elsa.UseHttp();
elsa.UseJavaScript();
elsa.UseLiquid();
// Configure identity so that we can create a default admin user.
elsa.UseIdentity(identity =>
{
identity.UseAdminUserProvider();
Expand All @@ -48,35 +24,21 @@
options.AccessTokenLifetime = TimeSpan.FromDays(1);
};
});
// Use default authentication (JWT).
elsa.UseDefaultAuthentication();
elsa.AddActivity<VehiculeActivity>();
elsa.AddActivity<VehicleActivity>();
});

builder.Services.AddSingleton<VehiculeUIHandler>();
//builder.Services.AddSingleton<VehiculeOptionsProvider>();

builder.Services.AddSingleton<VehicleUIHandler>();
builder.Services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()));

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.UseCors();
//app.UseAuthentication();
//app.UseAuthorization();
app.UseAuthentication();
app.UseAuthorization();
app.UseWorkflowsApi();
app.Map("/test",c=> c.UseWorkflows());
app.Run();

public class test : IHttpEndpointAuthorizationHandler
{
public ValueTask<bool> AuthorizeAsync(AuthorizeHttpEndpointContext context)
{
context.HttpContext.AuthenticateAsync();
throw new NotImplementedException();
}
}
app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Reflection;
using Elsa.Workflows.Contracts;

namespace Elsa.Samples.AspNet.CustomUIHandler;

/// <summary>
/// Configures the specified property to refresh the UI when the property value changes.
/// </summary>
public class RefreshUIHandler : IPropertyUIHandler
{
public ValueTask<IDictionary<string, object>> GetUIPropertiesAsync(PropertyInfo propertyInfo, object? context, CancellationToken cancellationToken = default)
{
IDictionary<string, object> result = new Dictionary<string, object>
{
{ "Refresh", true }
};
return ValueTask.FromResult(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Elsa.Workflows;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.Models;
using Elsa.Workflows.UIHints;

namespace Elsa.Samples.AspNet.CustomUIHandler;

/// <summary>
/// A sample activity that let's you select a car brand.
/// </summary>
public class VehicleActivity : Activity<string>
{
[Input(
Description = "The content type to use when sending the request.",
UIHint = InputUIHints.DropDown,
UIHandlers = [typeof(VehicleUIHandler), typeof(RefreshUIHandler)]
)]
public Input<string> Brand { get; set; } = default!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Reflection;
using Elsa.Workflows.UIHints.Dropdown;

namespace Elsa.Samples.AspNet.CustomUIHandler;

/// <summary>
/// A custom dropdown options provider to provide vehicle options for the Brand property of <see cref="VehicleActivity"/>.
/// </summary>
public class VehicleUIHandler : DropDownOptionsProviderBase
{
private readonly Random _random = new();

protected override ValueTask<ICollection<SelectListItem>> GetItemsAsync(PropertyInfo propertyInfo, object? context, CancellationToken cancellationToken)
{
var items = new List<SelectListItem>
{
new("BMW", "1"),
new("Tesla", "2"),
new("Peugeot", "3"),
new(_random.Next(100).ToString(), "4")
};

return new(items);
}
}

This file was deleted.

0 comments on commit d635226

Please sign in to comment.