Skip to content

Commit

Permalink
Add new events for Grand.Web - ChangeCurrency/ChangeLanguage/ChangeSt…
Browse files Browse the repository at this point in the history
…ore/ChangeTaxType/ChangeTheme
  • Loading branch information
KrzysztofPajak committed Oct 26, 2021
1 parent 597cb0c commit 51a7e8c
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 20 deletions.
58 changes: 38 additions & 20 deletions src/Web/Grand.Web/Controllers/CommonController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ public virtual IActionResult ExternalAuthenticationError(IEnumerable<string> err

await _workContext.SetWorkingLanguage(language);

//notification
await _mediator.Publish(new ChangeLanguageEvent(_workContext.CurrentCustomer, language));

return Redirect(returnUrl);
}

Expand Down Expand Up @@ -222,8 +225,11 @@ public virtual IActionResult InternalRedirect(string url, bool permanentRedirect
//clear gift card
await userFieldService.SaveField(_workContext.CurrentCustomer, SystemCustomerFieldNames.GiftVoucherCoupons, "");

//notification
await _mediator.Publish(new ChangeCurrencyEvent(_workContext.CurrentCustomer, currency));

//home page
if (String.IsNullOrEmpty(returnUrl))
if (string.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");

//prevent open redirection attack
Expand All @@ -248,7 +254,12 @@ public virtual IActionResult InternalRedirect(string url, bool permanentRedirect
{
var selectedstore = await storeService.GetStoreById(store);
if (selectedstore != null)
{
await _storeHelper.SetStoreCookie(store);

//notification
await _mediator.Publish(new ChangeStoreEvent(_workContext.CurrentCustomer, selectedstore));
}
}
}
var prevStore = await storeService.GetStoreById(currentstoreid);
Expand All @@ -263,7 +274,7 @@ public virtual IActionResult InternalRedirect(string url, bool permanentRedirect
}

//home page
if (String.IsNullOrEmpty(returnUrl))
if (string.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");

//prevent open redirection attack
Expand All @@ -280,8 +291,11 @@ public virtual async Task<IActionResult> SetTaxType(int customerTaxType, string
var taxDisplayType = (TaxDisplayType)Enum.ToObject(typeof(TaxDisplayType), customerTaxType);
await _workContext.SetTaxDisplayType(taxDisplayType);

//notification
await _mediator.Publish(new ChangeTaxTypeEvent(_workContext.CurrentCustomer, taxDisplayType));

//home page
if (String.IsNullOrEmpty(returnUrl))
if (string.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");

//prevent open redirection attack
Expand All @@ -291,6 +305,27 @@ public virtual async Task<IActionResult> SetTaxType(int customerTaxType, string
return Redirect(returnUrl);
}

public virtual async Task<IActionResult> SetStoreTheme(
[FromServices] IThemeContext themeContext, string themeName, string returnUrl = "")
{
await themeContext.SetWorkingTheme(themeName);

//notification
await _mediator.Publish(new ChangeThemeEvent(_workContext.CurrentCustomer, themeName));

//home page
if (string.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");

//prevent open redirection attack
if (!Url.IsLocalUrl(returnUrl))
returnUrl = Url.RouteUrl("HomePage");

return Redirect(returnUrl);
}



//contact us page
//available even when a store is closed
[ClosedStore(true)]
Expand Down Expand Up @@ -390,23 +425,6 @@ public virtual async Task<IActionResult> Sitemap([FromServices] CommonSettings c
return View(model);
}

public virtual async Task<IActionResult> SetStoreTheme(
[FromServices] IThemeContext themeContext, string themeName, string returnUrl = "")
{
await themeContext.SetWorkingTheme(themeName);

//home page
if (string.IsNullOrEmpty(returnUrl))
returnUrl = Url.RouteUrl("HomePage");

//prevent open redirection attack
if (!Url.IsLocalUrl(returnUrl))
returnUrl = Url.RouteUrl("HomePage");

return Redirect(returnUrl);
}


[HttpPost]
[ClosedStore(true)]
[PublicStore(true)]
Expand Down
18 changes: 18 additions & 0 deletions src/Web/Grand.Web/Events/ChangeCurrencyEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Grand.Domain.Customers;
using Grand.Domain.Directory;
using MediatR;

namespace Grand.Web.Events
{
public class ChangeCurrencyEvent : INotification
{
public Customer Customer { get; private set; }
public Currency Currency { get; private set; }

public ChangeCurrencyEvent(Customer customer, Currency currency)
{
Customer = customer;
Currency = currency;
}
}
}
18 changes: 18 additions & 0 deletions src/Web/Grand.Web/Events/ChangeLanguageEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Grand.Domain.Customers;
using Grand.Domain.Localization;
using MediatR;

namespace Grand.Web.Events
{
public class ChangeLanguageEvent : INotification
{
public Customer Customer { get; private set; }
public Language Language { get; private set; }

public ChangeLanguageEvent(Customer customer, Language language)
{
Customer = customer;
Language = language;
}
}
}
18 changes: 18 additions & 0 deletions src/Web/Grand.Web/Events/ChangeStoreEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Grand.Domain.Customers;
using Grand.Domain.Stores;
using MediatR;

namespace Grand.Web.Events
{
public class ChangeStoreEvent : INotification
{
public Customer Customer { get; private set; }
public Store Store { get; private set; }

public ChangeStoreEvent(Customer customer, Store store)
{
Customer = customer;
Store = store;
}
}
}
18 changes: 18 additions & 0 deletions src/Web/Grand.Web/Events/ChangeTaxTypeEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Grand.Domain.Customers;
using Grand.Domain.Tax;
using MediatR;

namespace Grand.Web.Events
{
public class ChangeTaxTypeEvent : INotification
{
public Customer Customer { get; private set; }
public TaxDisplayType TaxDisplayType { get; private set; }

public ChangeTaxTypeEvent(Customer customer, TaxDisplayType taxDisplayType)
{
Customer = customer;
TaxDisplayType = taxDisplayType;
}
}
}
17 changes: 17 additions & 0 deletions src/Web/Grand.Web/Events/ChangeThemeEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Grand.Domain.Customers;
using MediatR;

namespace Grand.Web.Events
{
public class ChangeThemeEvent : INotification
{
public Customer Customer { get; private set; }
public string ThemeName { get; private set; }

public ChangeThemeEvent(Customer customer, string themeName)
{
Customer = customer;
ThemeName = themeName;
}
}
}

0 comments on commit 51a7e8c

Please sign in to comment.