Skip to content

Commit

Permalink
refactor: Improve attribute handling and simplify variable assignment
Browse files Browse the repository at this point in the history
This update refines the process of retrieving custom attributes in HtmxorComponentEndpointHost, ensuring a more reliable order by introducing a priority system. This allows users to define a default HtmxLayout and override it on a component basis.

In addition, the code in HtmxorComponentResultExecutor has been simplified by directly assigning the 'isBadRequest' variable within the conditional statement, reducing redundancy and improving readability.
  • Loading branch information
tanczosm committed May 14, 2024
1 parent c72411c commit 325261a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/Htmxor/Endpoints/HtmxorComponentEndpointHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ public Task SetParametersAsync(ParameterView parameters)

private void BuildRenderTree(RenderTreeBuilder builder)
{
var pageLayoutType = ComponentType.GetCustomAttribute<LayoutAttribute>()?.LayoutType;
// Since GetCustomAttributes does not guarantee the order of attributes
// returned, having a priority allows users to define a default HtmxLayout
// e.g. in their _Imports.razor, and then override that on a component base,
// just as they can with the @layout directive in razor files.
var pageLayoutType = ComponentType
.GetCustomAttributes<HtmxLayoutAttribute>(true)
.MaxBy(x => x.Priority)?.LayoutType ??
ComponentType
.GetCustomAttribute<LayoutAttribute>()?.LayoutType;

builder.OpenComponent<LayoutView>(0);
builder.AddComponentParameter(1, nameof(LayoutView.Layout), pageLayoutType);
Expand Down
3 changes: 1 addition & 2 deletions src/Htmxor/Endpoints/Results/HtmxComponentResultExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ private static Task RenderComponentToResponse(
{
try
{
var isBadRequest = false;
quiesceTask = htmxContext.Request.EventHandlerId is not null
? endpointHtmlRenderer.DispatchHtmxorEventAsync(htmxContext, out isBadRequest)
? endpointHtmlRenderer.DispatchHtmxorEventAsync(htmxContext, out var isBadRequest)
: endpointHtmlRenderer.DispatchSubmitEventAsync(result.HandlerName, out isBadRequest);

if (isBadRequest)
Expand Down

0 comments on commit 325261a

Please sign in to comment.