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

Tweaks to swap style builder and related types #28

Merged
merged 4 commits into from
May 1, 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
8 changes: 7 additions & 1 deletion src/Htmxor/HtmxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Htmxor;
/// </summary>
public partial record class HtmxConfig
{
private SwapStyle? defaultSwapStyle;

/// <summary>
/// Defaults to <see langword="true" /> if this property is null. really only useful for testing
/// </summary>
Expand All @@ -31,7 +33,11 @@ public partial record class HtmxConfig
/// Defaults to <see cref="SwapStyle.InnerHTML"/> if this property is null.
/// </summary>
[JsonPropertyName("defaultSwapStyle")]
public SwapStyle? DefaultSwapStyle { get; set; }
public SwapStyle? DefaultSwapStyle
{
get => defaultSwapStyle;
set => defaultSwapStyle = value is SwapStyle.Default ? null : value;
}

/// <summary>
/// Defaults to <see langword="0"/> if this property is null.
Expand Down
39 changes: 19 additions & 20 deletions src/Htmxor/Http/HtmxResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
public HtmxResponse Reswap(string modifier)
{
ArgumentNullException.ThrowIfNullOrWhiteSpace(modifier);
headers[HtmxResponseHeaderNames.Reswap] = modifier;

return this;
}

Expand All @@ -159,37 +159,35 @@
{
AssertIsHtmxRequest();

var style = swapStyle switch
if (swapStyle is SwapStyle.Default)
{
SwapStyle.InnerHTML => "innerHTML",
SwapStyle.OuterHTML => "outerHTML",
SwapStyle.BeforeBegin => "beforebegin",
SwapStyle.AfterBegin => "afterbegin",
SwapStyle.BeforeEnd => "beforeend",
SwapStyle.AfterEnd => "afterend",
SwapStyle.Delete => "delete",
SwapStyle.None => "none",
_ => throw new SwitchExpressionException(swapStyle),
};
Reswap(modifier);

Check warning on line 164 in src/Htmxor/Http/HtmxResponse.cs

View workflow job for this annotation

GitHub Actions / infer-sharp

Possible null reference argument for parameter 'modifier' in 'HtmxResponse HtmxResponse.Reswap(string modifier)'.

Check warning on line 164 in src/Htmxor/Http/HtmxResponse.cs

View workflow job for this annotation

GitHub Actions / infer-sharp

Possible null reference argument for parameter 'modifier' in 'HtmxResponse HtmxResponse.Reswap(string modifier)'.

Check warning on line 164 in src/Htmxor/Http/HtmxResponse.cs

View workflow job for this annotation

GitHub Actions / create-nuget

Possible null reference argument for parameter 'modifier' in 'HtmxResponse HtmxResponse.Reswap(string modifier)'.

Check warning on line 164 in src/Htmxor/Http/HtmxResponse.cs

View workflow job for this annotation

GitHub Actions / run-stryker

Possible null reference argument for parameter 'modifier' in 'HtmxResponse HtmxResponse.Reswap(string modifier)'.

Check warning on line 164 in src/Htmxor/Http/HtmxResponse.cs

View workflow job for this annotation

GitHub Actions / run-stryker

Possible null reference argument for parameter 'modifier' in 'HtmxResponse HtmxResponse.Reswap(string modifier)'.

Check warning on line 164 in src/Htmxor/Http/HtmxResponse.cs

View workflow job for this annotation

GitHub Actions / run-test

Possible null reference argument for parameter 'modifier' in 'HtmxResponse HtmxResponse.Reswap(string modifier)'.

Check warning on line 164 in src/Htmxor/Http/HtmxResponse.cs

View workflow job for this annotation

GitHub Actions / run-test

Possible null reference argument for parameter 'modifier' in 'HtmxResponse HtmxResponse.Reswap(string modifier)'.
return this;
}

var value = modifier != null ? $"{style} {modifier}" : style;
var style = swapStyle.ToHtmxString();
var value = !string.IsNullOrWhiteSpace(modifier)
? $"{style} {modifier}"
: style;

headers[HtmxResponseHeaderNames.Reswap] = value;

return this;
}

/// <summary>
/// Allows you to specify how the response will be swapped.
/// Allows you to specify how the response will be swapped.
/// </summary>
/// <param></param>
/// <param name="swapStyle"></param>
/// <returns></returns>
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
public HtmxResponse Reswap(SwapStyleBuilder swapStyle)
{
var (style, modifier) = swapStyle.Build();
var (style, modifier) = swapStyle.Build();

return style is null ? Reswap(modifier) : Reswap((SwapStyle)style, modifier);
return style is SwapStyle.Default
? Reswap(modifier)
: Reswap(style, modifier);
}

/// <summary>
Expand Down Expand Up @@ -224,11 +222,12 @@
/// Sets response code to stop polling
/// </summary>
/// <returns></returns>
/// <returns>This <see cref="HtmxResponse"/> object instance.</returns>
public HtmxResponse StopPolling()
{
context.Response.StatusCode = HtmxStatusCodes.StopPolling;
context.Response.StatusCode = HtmxStatusCodes.StopPolling;

return this;
return this;
}

/// <summary>
Expand Down Expand Up @@ -314,7 +313,7 @@

private void AssertIsHtmxRequest()
{
if(!isHtmxRequest)
if (!isHtmxRequest)
{
throw new InvalidOperationException(
"The active request is not an htmx request. " +
Expand Down
Loading
Loading