Skip to content

Commit

Permalink
ref pr#1
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian smyda committed Aug 8, 2024
1 parent 43c5861 commit 2869aa7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Forte.Web.React/Configuration/ReactConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public class ReactConfiguration
public string NameOfObjectToSaveProps { get; set; } = "__reactProps";

/// <summary>
/// Name of the object used to save global context. Default value is "__globalContext".
/// <remarks>NameOfGlobalContextToSave is supported by <see cref="IReactService.RenderToStringAsync"/> method.</remarks>
/// Name of the object used to save global data object. Default value is "__globalData".
/// <remarks>NameOfGlobalDataToSave is supported by <see cref="IReactService.RenderToStringAsync"/> method.</remarks>
/// </summary>
public string NameOfGlobalContextToSave { get; set; } = "__globalContext";
public string NameOfGlobalDataToSave { get; set; } = "__globalData";

/// <summary>
/// Indicates whether caching is used. Default value is "true".
Expand Down
4 changes: 2 additions & 2 deletions Forte.Web.React/ForteWebReactExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void AddReact(this IServiceCollection services,

public static void UseReact(this IApplicationBuilder app, IEnumerable<string> scriptUrls, Version reactVersion,
bool disableServerSideRendering = false, string? nameOfObjectToSaveProps = null,
string? nameOfGlobalContextToSave = null, bool? useCache = null, bool? strictMode = null)
string? nameOfGlobalDataToSave = null, bool? useCache = null, bool? strictMode = null)
{
var config = app.ApplicationServices.GetService<ReactConfiguration>();

Expand All @@ -62,7 +62,7 @@ public static void UseReact(this IApplicationBuilder app, IEnumerable<string> sc
config.ScriptUrls = scriptUrls.ToList();
config.ReactVersion = reactVersion;
config.NameOfObjectToSaveProps = nameOfObjectToSaveProps ?? config.NameOfObjectToSaveProps;
config.NameOfGlobalContextToSave = nameOfGlobalContextToSave ?? config.NameOfGlobalContextToSave;
config.NameOfGlobalDataToSave = nameOfGlobalDataToSave ?? config.NameOfGlobalDataToSave;
config.UseCache = useCache ?? true;
config.StrictMode = strictMode ?? false;
}
Expand Down
24 changes: 12 additions & 12 deletions Forte.Web.React/HtmlHelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,33 @@ namespace Forte.Web.React;
public static class HtmlHelperExtensions
{
#if NET48
public static IHtmlString React<T>(this HtmlHelper _, string componentName, T props, object? globalContext = null)
public static IHtmlString React<T>(this HtmlHelper _, string componentName, T props, object? globalData = null)
{
var reactService = DependencyResolver.Current.GetService<IReactService>();
var renderedComponent = reactService.RenderToStringAsync(componentName, props, globalContext: globalContext)
var renderedComponent = reactService.RenderToStringAsync(componentName, props, globalData: globalData)
.GetAwaiter().GetResult();

return new HtmlString(renderedComponent);
}

public static IHtmlString React<TComponent>(this HtmlHelper _, TComponent component, object? globalContext = null)
public static IHtmlString React<TComponent>(this HtmlHelper _, TComponent component, object? globalData = null)
where TComponent : IReactComponent
{
var reactService = DependencyResolver.Current.GetService<IReactService>();
var renderedComponent = reactService
.RenderToStringAsync(component.Path, null, component.RenderingMode, globalContext)
.RenderToStringAsync(component.Path, null, component.RenderingMode, globalData)
.GetAwaiter().GetResult();

return new HtmlString(renderedComponent);
}

public static IHtmlString React<TComponent, TProps>(this HtmlHelper _, TComponent component,
object? globalContext = null)
object? globalData = null)
where TComponent : IReactComponent<TProps> where TProps : IReactComponentProps
{
var reactService = DependencyResolver.Current.GetService<IReactService>();
var renderedComponent = reactService
.RenderToStringAsync(component.Path, component.Props, component.RenderingMode, globalContext).GetAwaiter()
.RenderToStringAsync(component.Path, component.Props, component.RenderingMode, globalData).GetAwaiter()
.GetResult();

return new HtmlString(renderedComponent);
Expand All @@ -60,33 +60,33 @@ public static IHtmlString InitJavascript(this HtmlHelper _)

#if NET6_0_OR_GREATER
public static async Task<IHtmlContent> ReactAsync<T>(this IHtmlHelper htmlHelper, string componentName, T props,
object? globalContext = null)
object? globalData = null)
{
var reactService = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IReactService>();

return new HtmlString(
await reactService.RenderToStringAsync(componentName, props, globalContext: globalContext));
await reactService.RenderToStringAsync(componentName, props, globalData: globalData));
}

public static async Task<IHtmlContent> ReactAsync<TComponent>(this IHtmlHelper htmlHelper, TComponent component,
object? globalContext = null)
object? globalData = null)
where TComponent : IReactComponent
{
var reactService = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IReactService>();

return new HtmlString(
await reactService.RenderToStringAsync(component.Path, null, component.RenderingMode, globalContext));
await reactService.RenderToStringAsync(component.Path, null, component.RenderingMode, globalData));
}

public static async Task<IHtmlContent> ReactAsync<TComponent, TProps>(this IHtmlHelper htmlHelper,
TComponent component, object? globalContext =
TComponent component, object? globalData =
null) where TComponent : IReactComponent<TProps> where TProps : IReactComponentProps
{
var reactService = htmlHelper.ViewContext.HttpContext.RequestServices.GetRequiredService<IReactService>();

return new HtmlString(
await reactService.RenderToStringAsync(component.Path, component.Props, component.RenderingMode,
globalContext));
globalData));
}

public static IHtmlContent InitJavascript(this IHtmlHelper htmlHelper)
Expand Down
12 changes: 6 additions & 6 deletions Forte.Web.React/React/ReactService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IReactService
Task RenderAsync(TextWriter writer, string componentName, object? props = null, RenderOptions? options = null);

Task<string> RenderToStringAsync(string componentName, object? props = null,
RenderingMode renderingMode = RenderingMode.ClientAndServer, object? globalContext = null);
RenderingMode renderingMode = RenderingMode.ClientAndServer, object? globalData = null);
}

public class ReactService : IReactService
Expand Down Expand Up @@ -67,7 +67,7 @@ public ReactService(INodeJSService nodeJsService, IJsonSerializationService json
}
#endif

private async Task<T> InvokeRenderTo<T>(Component component, object? props = null, object? globalContext = null, params object[] args)
private async Task<T> InvokeRenderTo<T>(Component component, object? props = null, object? globalData = null, params object[] args)
{
var allArgs = new List<object>()
{
Expand All @@ -76,8 +76,8 @@ private async Task<T> InvokeRenderTo<T>(Component component, object? props = nul
props,

Check warning on line 76 in Forte.Web.React/React/ReactService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<object>.Add(object item)'.

Check warning on line 76 in Forte.Web.React/React/ReactService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<object>.Add(object item)'.

Check warning on line 76 in Forte.Web.React/React/ReactService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<object>.Add(object item)'.

Check warning on line 76 in Forte.Web.React/React/ReactService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<object>.Add(object item)'.
_config.ScriptUrls,
_config.NameOfObjectToSaveProps,
_config.NameOfGlobalContextToSave,
globalContext
_config.NameOfGlobalDataToSave,
globalData

Check warning on line 80 in Forte.Web.React/React/ReactService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<object>.Add(object item)'.

Check warning on line 80 in Forte.Web.React/React/ReactService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<object>.Add(object item)'.

Check warning on line 80 in Forte.Web.React/React/ReactService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<object>.Add(object item)'.

Check warning on line 80 in Forte.Web.React/React/ReactService.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'void List<object>.Add(object item)'.
};
allArgs.AddRange(args);

Expand Down Expand Up @@ -108,7 +108,7 @@ private async Task<T> InvokeRenderTo<T>(Component component, object? props = nul


public async Task<string> RenderToStringAsync(string componentName, object? props = null,
RenderingMode renderingMode = RenderingMode.ClientAndServer, object? globalContext = null)
RenderingMode renderingMode = RenderingMode.ClientAndServer, object? globalData = null)
{
var component = new Component(componentName, props, renderingMode);
Components.Add(component);
Expand All @@ -118,7 +118,7 @@ public async Task<string> RenderToStringAsync(string componentName, object? prop
return WrapRenderedStringComponent(string.Empty, component);
}

var result = await InvokeRenderTo<string>(component, props, globalContext).ConfigureAwait(false);
var result = await InvokeRenderTo<string>(component, props, globalData).ConfigureAwait(false);

return WrapRenderedStringComponent(result, component);
}
Expand Down
6 changes: 3 additions & 3 deletions Forte.Web.React/Scripts/renderToString.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
props = {},
scriptFiles,
nameOfObjectToSaveProps,
nameOfGlobalContextToSave,
context = {}
nameOfGlobalDataToSave,
globalData = {}
) => {
scriptFiles.forEach((scriptFile) => {
require(scriptFile);
Expand All @@ -15,7 +15,7 @@
const ReactDOMServer = global["ReactDOMServer"];
const React = global["React"];
const componentRepository = global["__react"] || {};
global[nameOfGlobalContextToSave] = context;
global[nameOfGlobalDataToSave] = globalData;

const path = componentName.split(".");
let component = componentRepository[path[0]];
Expand Down

0 comments on commit 2869aa7

Please sign in to comment.