Skip to content

Commit 04950cf

Browse files
committed
Improve Inertia shared data integration
1 parent 8d84eec commit 04950cf

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
lines changed

InertiaCore/Inertia.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ public static class Inertia
1919
public static string? GetVersion() => _factory.GetVersion();
2020

2121
public static LocationResult Location(string url) => _factory.Location(url);
22+
23+
public static void Share(string key, object? value) => _factory.Share(key, value);
24+
25+
public static void Share(IDictionary<string, object?> data) => _factory.Share(data);
2226
}

InertiaCore/InertiaSharedData.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
namespace InertiaCore;
22

3-
public class InertiaSharedData
3+
internal class InertiaSharedData
44
{
5-
public IDictionary<string, object?>? Data { get; set; }
5+
private IDictionary<string, object?>? Data { get; set; }
66

7-
public Dictionary<string, object?> Merge(IDictionary<string, object?> with)
7+
public Dictionary<string, object?> GetMerged(IDictionary<string, object?> with)
88
{
99
var result = new Dictionary<string, object?>();
1010

@@ -16,4 +16,12 @@ public class InertiaSharedData
1616

1717
return result;
1818
}
19+
20+
public void Merge(IDictionary<string, object?> with) => Data = GetMerged(with);
21+
22+
public void Set(string key, object? value)
23+
{
24+
Data ??= new Dictionary<string, object?>();
25+
Data[key] = value;
26+
}
1927
}

InertiaCore/Response.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task ExecuteResultAsync(ActionContext context)
5050

5151
var shared = context.HttpContext.Features.Get<InertiaSharedData>();
5252
if (shared != null)
53-
page.Props = shared.Merge(page.Props);
53+
page.Props = shared.GetMerged(page.Props);
5454

5555
var errors = GetErrors();
5656
if (errors != null)

InertiaCore/ResponseFactory.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Text.Json.Serialization;
33
using System.Web;
44
using Microsoft.AspNetCore.Html;
5+
using Microsoft.AspNetCore.Http;
56

67
namespace InertiaCore;
78

@@ -13,14 +14,19 @@ internal interface IResponseFactory
1314
public void Version(object? version);
1415
public string? GetVersion();
1516
public LocationResult Location(string url);
17+
public void Share(string key, object? value);
18+
public void Share(IDictionary<string, object?> data);
1619
}
1720

1821
internal class ResponseFactory : IResponseFactory
1922
{
20-
private string _rootView = "~/Views/App.cshtml";
23+
private readonly IHttpContextAccessor _contextAccessor;
2124

25+
private string _rootView = "~/Views/App.cshtml";
2226
private object? _version;
2327

28+
public ResponseFactory(IHttpContextAccessor contextAccessor) => _contextAccessor = contextAccessor;
29+
2430
public Response Render(string component, object? props = null)
2531
{
2632
props ??= new { };
@@ -54,4 +60,26 @@ public IHtmlContent Html(dynamic model)
5460
};
5561

5662
public LocationResult Location(string url) => new(url);
63+
64+
public void Share(string key, object? value)
65+
{
66+
var context = _contextAccessor.HttpContext!;
67+
68+
var sharedData = context.Features.Get<InertiaSharedData>();
69+
sharedData ??= new InertiaSharedData();
70+
sharedData.Set(key, value);
71+
72+
context.Features.Set(sharedData);
73+
}
74+
75+
public void Share(IDictionary<string, object?> data)
76+
{
77+
var context = _contextAccessor.HttpContext!;
78+
79+
var sharedData = context.Features.Get<InertiaSharedData>();
80+
sharedData ??= new InertiaSharedData();
81+
sharedData.Merge(data);
82+
83+
context.Features.Set(sharedData);
84+
}
5785
}

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,19 @@ app.Use(async (context, next) =>
126126
{
127127
var userId = context.Session.GetInt32("userId");
128128

129-
var sharedData = new InertiaSharedData
129+
Inertia.Share("auth", new
130130
{
131-
Data = new Dictionary<string, object?>
131+
UserId = userId
132+
});
133+
134+
// Or
135+
136+
Inertia.Share(new Dictionary<string, object?>
137+
{
138+
["auth"] => new
132139
{
133-
["userId"] = userId
140+
UserId = userId
134141
}
135-
};
136-
context.Features.Set(sharedData);
142+
});
137143
});
138144
```

0 commit comments

Comments
 (0)