Skip to content

Commit

Permalink
Don't save filter cookies if the original path was a deep linked griddly
Browse files Browse the repository at this point in the history
  • Loading branch information
programcsharp committed Feb 10, 2019
1 parent 2838683 commit 7e37561
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
1 change: 1 addition & 0 deletions Griddly.Mvc/GriddlyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class GriddlyContext
{
public string Name { get; set; }
public bool IsDefaultSkipped { get; set; }
public bool IsDeepLink { get; set; }

public GriddlyFilterCookieData CookieData { get; set; }

Expand Down
82 changes: 47 additions & 35 deletions Griddly.Mvc/GriddlyParameterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)

// if a param came in on querystring, skip the defaults. cookie already does its own skip.
if (!context.IsDefaultSkipped && filterContext.ActionParameters.Any(x => x.Value != null && parentKeys.Contains(x.Key)))
{
context.IsDefaultSkipped = true;
context.IsDeepLink = true;
}

foreach (var param in filterContext.ActionParameters.ToList())
{
Expand All @@ -49,6 +52,11 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
}
}
}
else if (filterContext.ActionParameters.TryGetValue("_isDeepLink", out object value))
{
if (Convert.ToBoolean(value) == true)
context.IsDeepLink = true;
}
}

base.OnActionExecuting(filterContext);
Expand All @@ -59,58 +67,62 @@ public override void OnActionExecuted(ActionExecutedContext filterContext)
if (filterContext.Result is GriddlyResult result)
{
var context = filterContext.Controller.GetOrCreateGriddlyContext();
var request = filterContext.HttpContext.Request;

Uri parentPath = filterContext.IsChildAction ? request.Url : request.UrlReferrer;
string parentPathString = parentPath?.PathAndQuery.Split('?')[0]; // TODO: less allocations than split

if (parentPathString?.Length > 0)
if (!context.IsDeepLink)
{
HttpCookie cookie = new HttpCookie("gf_" + context.Name)
{
Path = parentPathString
};
var request = filterContext.HttpContext.Request;

Uri parentPath = filterContext.IsChildAction ? request.Url : request.UrlReferrer;
string parentPathString = parentPath?.PathAndQuery.Split('?')[0]; // TODO: less allocations than split

GriddlyFilterCookieData data = new GriddlyFilterCookieData()
if (parentPathString?.Length > 0)
{
Values = new Dictionary<string, string[]>()
};
HttpCookie cookie = new HttpCookie("gf_" + context.Name)
{
Path = parentPathString
};

if (context.SortFields?.Length > 0)
data.SortFields = context.SortFields;
GriddlyFilterCookieData data = new GriddlyFilterCookieData()
{
Values = new Dictionary<string, string[]>()
};

// now, we could use the context.Parameters... but the raw string values seems more like what we want here...
foreach (var param in filterContext.ActionDescriptor.GetParameters())
{
var valueResult = filterContext.Controller.ValueProvider.GetValue(param.ParameterName);
if (context.SortFields?.Length > 0)
data.SortFields = context.SortFields;

if (valueResult?.RawValue != null)
// now, we could use the context.Parameters... but the raw string values seems more like what we want here...
foreach (var param in filterContext.ActionDescriptor.GetParameters())
{
if (valueResult.RawValue is string[] array)
data.Values[param.ParameterName] = array;
else
data.Values[param.ParameterName] = new[] { valueResult.RawValue.ToString() };
var valueResult = filterContext.Controller.ValueProvider.GetValue(param.ParameterName);

if (valueResult?.RawValue != null)
{
if (valueResult.RawValue is string[] array)
data.Values[param.ParameterName] = array;
else
data.Values[param.ParameterName] = new[] { valueResult.RawValue.ToString() };
}
}
}

// ... but if it is a defaults situation, we actually need to grab those
if (filterContext.IsChildAction && !context.IsDefaultSkipped && context.Defaults.Count > 0)
{
foreach (var param in context.Defaults)
// ... but if it is a defaults situation, we actually need to grab those
if (filterContext.IsChildAction && !context.IsDefaultSkipped && context.Defaults.Count > 0)
{
if (param.Value != null)
foreach (var param in context.Defaults)
{
var value = GriddlyExtensions.GetFormattedValueByType(param.Value);
if (param.Value != null)
{
var value = GriddlyExtensions.GetFormattedValueByType(param.Value);

if (value != null)
data.Values[param.Key] = value;
if (value != null)
data.Values[param.Key] = value;
}
}
}
}

cookie.Value = JsonConvert.SerializeObject(data);
cookie.Value = JsonConvert.SerializeObject(data);

filterContext.HttpContext.Response.Cookies.Add(cookie);
filterContext.HttpContext.Response.Cookies.Add(cookie);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions Griddly/Scripts/griddly.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,13 @@
var filterDefaults = this.$element.data("griddly-filter-defaults");
var currencySymbol = this.$element.data("griddly-currency-symbol");
var removeIconCssClass = this.$element.data("griddly-remove-icon-css-class");
var isDeepLink = this.$element.data("griddly-isdeeplink");

this.additionalRequestValues = {};

if (isDeepLink)
this.additionalRequestValues._isDeepLink = true;

this.options.url = url;
this.options.defaultRowIds = defaultRowIds;
this.options.count = parseInt(count);
Expand Down
2 changes: 2 additions & 0 deletions Griddly/Views/Shared/Griddly/Griddly.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@{
GriddlySettings settings = (GriddlySettings)ViewBag.settings;
var context = ViewContext.Controller.GetOrCreateGriddlyContext();

settings.Columns.RemoveAll(x => !x.RenderMode.HasFlag(ColumnRenderMode.View));

Expand Down Expand Up @@ -158,6 +159,7 @@
@: @Html.AttributeNullable("data-griddly-url", !simple ? Url.Current() : null)
@: @Html.AttributeNullable("data-griddly-urltarget", !simple ? settings.RowClickTarget : null)
@: data-griddly-count="@Model.Total"
@: @Html.AttributeIf("data-griddly-isdeeplink", context.IsDeepLink, true)
@: @Html.AttributeNullable("data-griddly-filtermode", settings.InitialFilterMode != FilterMode.None ? settings.InitialFilterMode.ToString() : null)
@: @Html.AttributeNullable("data-griddly-allowedfiltermodes", settings.AllowedFilterModes != FilterMode.None ? Json.Encode(Enum.GetValues(typeof(FilterMode)).Cast<FilterMode>().Where(x => settings.AllowedFilterModes.Value.HasFlag(x) && x != FilterMode.Both && x != FilterMode.None).Select(x => x.ToString())) : null)
@: data-griddly-isfilterforminline="@settings.IsFilterFormInline.ToString().ToLower()"
Expand Down

0 comments on commit 7e37561

Please sign in to comment.