Skip to content

Commit

Permalink
Added support for AJAX requests for XMLHTTPRequests and partly for fe…
Browse files Browse the repository at this point in the history
…tch requests (#23)

* working on front end side of things. regular development

* regular dev

* regular dev. major roadblock

* Added ajax support for XMLHTTPRequests and partly for fetch requests
  • Loading branch information
nabinked authored Feb 3, 2018
1 parent b56d964 commit 7cc37d4
Show file tree
Hide file tree
Showing 35 changed files with 6,824 additions and 130 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using NToastNotify.Web.FeatureFolders.Models;

Expand All @@ -25,20 +26,53 @@ public IActionResult Index()
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";

_toastNotification.AddToastMessage("Warning About Title", "My About Warning Message", Enums.ToastType.Warning, new ToastOption()
{
PositionClass = ToastPositions.BottomFullWidth
});
return View();
}

public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";

return View();
_toastNotification.AddToastMessage("Redirected...", "Dont get confused. <br /> <strong>You were redirected from Contact Page. <strong/>", Enums.ToastType.Info, new ToastOption()
{
PositionClass = ToastPositions.TopCenter
});
return RedirectToAction("About");
}

public IActionResult Error()
{
_toastNotification.AddErrorToastMessage("ERROR", "There was something wrong with this request.");

return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

public IActionResult Ajax()
{
//Testing Default Methods

//Success
_toastNotification.AddSuccessToastMessage("Same for success message", "Success title specified in controller action");
// Success with default options (taking into account the overwritten defaults when initializing in Startup.cs)
_toastNotification.AddSuccessToastMessage();

//Info
_toastNotification.AddInfoToastMessage();

//Warning
_toastNotification.AddWarningToastMessage();

//Error
_toastNotification.AddErrorToastMessage();

_toastNotification.AddToastMessage("Custom Title", "My Custom Message", Enums.ToastType.Success, new ToastOption()
{
PositionClass = ToastPositions.BottomLeft
});
return Content("AJAX CALL");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
<PackageReference Include="NToastNotify" Version="3.0.3" />
<!--<PackageReference Include="NToastNotify" Version="3.0.3" />-->
<PackageReference Include="OdeToCode.AddFeatureFolders" Version="1.0.8" />
</ItemGroup>

<ItemGroup>
<!--<ProjectReference Include="..\..\src\NToastNotify.csproj" />-->
<ProjectReference Include="..\..\src\NToastNotify.csproj" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion sample/NToastNotify.Web.FeatureFolders/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
}

app.UseStaticFiles();

app.UseNToastNotify();
app.UseMvcWithDefaultRoute();

}
}
}
4 changes: 2 additions & 2 deletions sample/NToastNotify.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public IActionResult Index()
PositionClass = ToastPositions.BottomLeft
});

var messages = _toastNotification.PeekToastMessages();
return View();
}

Expand All @@ -53,7 +52,7 @@ public IActionResult Contact()
{
PositionClass = ToastPositions.TopCenter
});
return RedirectToAction("About"); ;
return RedirectToAction("About");
}

public IActionResult Error()
Expand All @@ -64,6 +63,7 @@ public IActionResult Error()

public IActionResult Empty()
{

return View();
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/Components/ToastrViewComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Newtonsoft.Json;
using NToastNotify.Helpers;

namespace NToastNotify.Components
{
[ViewComponent(Name = "NToastNotify.Toastr")]
public class ToastrViewComponent : ViewComponent
{
private readonly IToastNotification _toastNotification;
private readonly ToastOption _globalOption; // This is filled with the provided default values on NToastNotify service config./initialization in startup.cs
private readonly ITempDataWrapper _tempDataWrapper;
private readonly NToastNotifyOption _nToastNotifyOption;

public ToastrViewComponent(ITempDataWrapper tempDataWrapper, ToastOption globalOption)
public ToastrViewComponent(IToastNotification toastNotification, ToastOption globalOption, NToastNotifyOption nToastNotifyOption)
{
_tempDataWrapper = tempDataWrapper;
_toastNotification = toastNotification;
_globalOption = globalOption;
_nToastNotifyOption = nToastNotifyOption;
}

public IViewComponentResult Invoke()
{
var model = new ToastNotificationViewModel()
{
ToastMessages = _tempDataWrapper.Get<IEnumerable<ToastMessage>>(Constants.TempDataKey),
GlobalOptionJson = _globalOption.MergeWith(ToastOption.Defaults).Json
ToastMessagesJson = _toastNotification.ReadAllMessages().ToJson(),
GlobalOptionJson = _globalOption.MergeWith(ToastOption.Defaults).Json,
RequestHeaderKey = Constants.RequestHeaderKey,
LibraryName = _nToastNotifyOption.Library.ToString().ToLower()
};
return View("ToastrView", model);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
internal class Constants
{
public static readonly string TempDataKey = "NToastNotify.Messages.TempDataKey";
public static readonly string ResponseHeaderKey = "NToastNotify-Messages";
public static readonly string RequestHeaderKey = "NToastNotify-Request-Type";
}
}
7 changes: 6 additions & 1 deletion src/ToastEnums.cs → src/Enums.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace NToastNotify
{
public class ToastEnums
public class Enums
{
public enum ToastType
{
Expand All @@ -9,5 +9,10 @@ public enum ToastType
Info,
Error
}

public enum SupportedLibrary
{
Toastr
}
}
}
22 changes: 22 additions & 0 deletions src/Helpers/JsonSerialization.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace NToastNotify.Helpers
{
public static class JsonSerialization
{
public static JsonSerializerSettings JsonSerializerSettings => new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
};

public static string ToJson(this object obj)
{
return JsonConvert.SerializeObject(obj, JsonSerializerSettings);
}
}
}
29 changes: 29 additions & 0 deletions src/Helpers/RequestHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Http;

namespace NToastNotify.Helpers
{
public static class RequestHelpers
{
public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}

if (!string.IsNullOrWhiteSpace(request.Headers[Constants.RequestHeaderKey]))
{
return true;
};
if (!string.IsNullOrWhiteSpace(request.Headers[Constants.RequestHeaderKey.ToLower()]))
{
return true;
}

return false;
}
}
}
14 changes: 14 additions & 0 deletions src/IMessageContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NToastNotify
{
public interface IMessageContainer
{
void Add(ToastMessage message);
void RemoveAll();
IList<ToastMessage> GetAll();
IList<ToastMessage> ReadAll();
}
}
11 changes: 11 additions & 0 deletions src/IMessageContainerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace NToastNotify
{
public interface IMessageContainerFactory
{
IMessageContainer Create();
}
}
6 changes: 6 additions & 0 deletions src/ITempDataWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,11 @@ public interface ITempDataWrapper
T Get<T>(string key);
T Peek<T>(string key);
void Add(string key, object value);
/// <summary>
/// Remove value with a given key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
bool Remove(string key);
}
}
29 changes: 20 additions & 9 deletions src/IToastNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@ public interface IToastNotification
/// </summary>
/// <param name="title">Title of the message</param>
/// <param name="message">The actual message body</param>
/// <param name="notificationType">Type of message <seealso cref="ToastEnums.ToastType"/></param>
void AddToastMessage(string title, string message, ToastEnums.ToastType notificationType);
/// <param name="notificationType">Type of message <seealso cref="Enums.ToastType"/></param>
void AddToastMessage(string title, string message, Enums.ToastType notificationType);

/// <summary>
/// Adds a toast message to render to the view.
/// </summary>
/// <param name="title">Title of the message</param>
/// <param name="message">The actual message body</param>
/// <param name="notificationType">Type of message <seealso cref="ToastEnums.ToastType"/></param>
/// <param name="notificationType">Type of message <seealso cref="Enums.ToastType"/></param>
/// <param name="toastOptions">Custom option for the message being added. <see cref="ToastOption"/></param>
void AddToastMessage(string title, string message, ToastEnums.ToastType notificationType,
void AddToastMessage(string title, string message, Enums.ToastType notificationType,
ToastOption toastOptions);

/// <summary>
/// Adds a toast message of type <see cref="ToastEnums.ToastType.Success"/>
/// Adds a toast message of type <see cref="Enums.ToastType.success"/>
/// </summary>
/// <param name="message">Messsage body</param>
/// <param name="title">Title of the message</param>
/// <param name="toastOptions">Custom option for the message being added. <see cref="ToastOption"/></param>
void AddSuccessToastMessage(string message = null, string title = null, ToastOption toastOptions = null);

/// <summary>
/// Adds a toast message of type <see cref="ToastEnums.ToastType.Info"/>
/// Adds a toast message of type <see cref="Enums.ToastType.Info"/>
/// </summary>
/// <param name="message">Messsage body</param>
/// <param name="title">Title of the message</param>
/// <param name="toastOptions">Custom option for the message being added. <see cref="ToastOption"/></param>
void AddInfoToastMessage(string message = null, string title = null, ToastOption toastOptions = null);

/// <summary>
/// Adds a toast message of type <see cref="ToastEnums.ToastType.Warning"/>
/// Adds a toast message of type <see cref="Enums.ToastType.Warning"/>
/// </summary>
/// <param name="message">Messsage body</param>
/// <param name="title">Title of the message</param>
/// <param name="toastOptions">Custom option for the message being added. <see cref="ToastOption"/></param>
void AddWarningToastMessage(string message = null, string title = null, ToastOption toastOptions = null);

/// <summary>
/// Adds a toast message of type <see cref="ToastEnums.ToastType.Error"/>
/// Adds a toast message of type <see cref="Enums.ToastType.Error"/>
/// </summary>
/// <param name="message">Messsage body</param>
/// <param name="title">Title of the message</param>
Expand All @@ -58,6 +58,17 @@ void AddToastMessage(string title, string message, ToastEnums.ToastType notifica
/// Gets the list of <see cref="ToastMessage"/> added so far.
/// </summary>
/// <returns></returns>
IEnumerable<ToastMessage> PeekToastMessages();
IEnumerable<ToastMessage> GetToastMessages();

/// <summary>
/// Returns a list of <see cref="ToastMessage"/> and removes them from the list of Toast Messages.
/// </summary>
/// <returns></returns>
IEnumerable<ToastMessage> ReadAllMessages();

/// <summary>
/// Remove all toast notifications
/// </summary>
void RemoveAll();
}
}
36 changes: 36 additions & 0 deletions src/InMemoryMessageContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Linq;

namespace NToastNotify
{
public class InMemoryMessageContainer : IMessageContainer
{
private IList<ToastMessage> Messages { get; }

public InMemoryMessageContainer()
{
Messages = new List<ToastMessage>();
}
public void Add(ToastMessage message)
{
Messages.Add(message);
}

public void RemoveAll()
{
Messages.Clear();
}

public IList<ToastMessage> GetAll()
{
return Messages;
}

public IList<ToastMessage> ReadAll()
{
var messages = new List<ToastMessage>(Messages);
Messages.Clear();
return messages;
}
}
}
Loading

0 comments on commit 7cc37d4

Please sign in to comment.