Initial commit
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
||||
using PowderCoating.Core.Entities;
|
||||
using PowderCoating.Core.Enums;
|
||||
|
||||
namespace PowderCoating.Web.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods on <see cref="ITempDataDictionary"/> for storing toast
|
||||
/// notification messages that survive a POST→redirect→GET cycle.
|
||||
/// <para>
|
||||
/// TempData is used (rather than ViewBag or ViewData) because toast messages
|
||||
/// must survive an HTTP redirect: a controller sets the message before calling
|
||||
/// <c>RedirectToAction</c>, and the layout reads and renders it on the next GET.
|
||||
/// TempData is session-backed and automatically cleared after it is read.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The four well-known string keys (<c>Success</c>, <c>Error</c>, <c>Warning</c>,
|
||||
/// <c>Info</c>) match the JavaScript toast-renderer in <c>_Layout.cshtml</c>;
|
||||
/// changing them here requires a matching update in the layout script.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static class ToastHelper
|
||||
{
|
||||
/// <summary>TempData key read by the layout to render a green success toast.</summary>
|
||||
private const string SuccessKey = "Success";
|
||||
|
||||
/// <summary>TempData key read by the layout to render a red error toast.</summary>
|
||||
private const string ErrorKey = "Error";
|
||||
|
||||
/// <summary>TempData key read by the layout to render a yellow warning toast.</summary>
|
||||
private const string WarningKey = "Warning";
|
||||
|
||||
/// <summary>TempData key read by the layout to render a blue info toast.</summary>
|
||||
private const string InfoKey = "Info";
|
||||
|
||||
/// <summary>
|
||||
/// Stores a success (green) toast message in TempData for display on the
|
||||
/// next page render after a redirect.
|
||||
/// </summary>
|
||||
/// <param name="tempData">The TempData dictionary for the current request.</param>
|
||||
/// <param name="message">The human-readable success message.</param>
|
||||
public static void Success(this ITempDataDictionary tempData, string message)
|
||||
{
|
||||
tempData[SuccessKey] = message;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores an error (red) toast message in TempData for display on the
|
||||
/// next page render after a redirect.
|
||||
/// </summary>
|
||||
/// <param name="tempData">The TempData dictionary for the current request.</param>
|
||||
/// <param name="message">The human-readable error message.</param>
|
||||
public static void Error(this ITempDataDictionary tempData, string message)
|
||||
{
|
||||
tempData[ErrorKey] = message;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores a warning (yellow) toast message in TempData for display on the
|
||||
/// next page render after a redirect.
|
||||
/// </summary>
|
||||
/// <param name="tempData">The TempData dictionary for the current request.</param>
|
||||
/// <param name="message">The human-readable warning message.</param>
|
||||
public static void Warning(this ITempDataDictionary tempData, string message)
|
||||
{
|
||||
tempData[WarningKey] = message;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores an informational (blue) toast message in TempData for display on
|
||||
/// the next page render after a redirect.
|
||||
/// </summary>
|
||||
/// <param name="tempData">The TempData dictionary for the current request.</param>
|
||||
/// <param name="message">The human-readable informational message.</param>
|
||||
public static void Info(this ITempDataDictionary tempData, string message)
|
||||
{
|
||||
tempData[InfoKey] = message;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convenience extension methods on <see cref="Controller"/> that delegate to
|
||||
/// <see cref="ToastHelper"/> via the controller's own <c>TempData</c>.
|
||||
/// <para>
|
||||
/// These shorten controller code from <c>TempData.Success("…")</c> to the
|
||||
/// more readable <c>this.ToastSuccess("…")</c>, which reads closer to
|
||||
/// natural language and is consistent with other MVC helper conventions.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public static class ControllerToastExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Stores a success (green) toast in the controller's TempData.
|
||||
/// Convenience wrapper around <see cref="ToastHelper.Success"/>.
|
||||
/// </summary>
|
||||
/// <param name="controller">The calling MVC controller.</param>
|
||||
/// <param name="message">The success message to display after redirect.</param>
|
||||
public static void ToastSuccess(this Controller controller, string message)
|
||||
{
|
||||
controller.TempData.Success(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores an error (red) toast in the controller's TempData.
|
||||
/// Convenience wrapper around <see cref="ToastHelper.Error"/>.
|
||||
/// </summary>
|
||||
/// <param name="controller">The calling MVC controller.</param>
|
||||
/// <param name="message">The error message to display after redirect.</param>
|
||||
public static void ToastError(this Controller controller, string message)
|
||||
{
|
||||
controller.TempData.Error(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores a warning (yellow) toast in the controller's TempData.
|
||||
/// Convenience wrapper around <see cref="ToastHelper.Warning"/>.
|
||||
/// </summary>
|
||||
/// <param name="controller">The calling MVC controller.</param>
|
||||
/// <param name="message">The warning message to display after redirect.</param>
|
||||
public static void ToastWarning(this Controller controller, string message)
|
||||
{
|
||||
controller.TempData.Warning(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores an informational (blue) toast in the controller's TempData.
|
||||
/// Convenience wrapper around <see cref="ToastHelper.Info"/>.
|
||||
/// </summary>
|
||||
/// <param name="controller">The calling MVC controller.</param>
|
||||
/// <param name="message">The informational message to display after redirect.</param>
|
||||
public static void ToastInfo(this Controller controller, string message)
|
||||
{
|
||||
controller.TempData.Info(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Translates a <see cref="NotificationLog"/> outcome into the appropriate
|
||||
/// toast severity and message.
|
||||
/// <para>
|
||||
/// Sent notifications use Info (not Success) because the notification is a
|
||||
/// side effect of the main action — the main action already gets a Success
|
||||
/// toast. Skipped and Failed outcomes are Warning (not Error) because the
|
||||
/// primary operation succeeded even if the notification did not.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="controller">The calling MVC controller.</param>
|
||||
/// <param name="log">
|
||||
/// The most recent <see cref="NotificationLog"/> record, or <c>null</c>
|
||||
/// if no notification was attempted (in which case this method is a no-op).
|
||||
/// </param>
|
||||
public static void SetNotificationResultToast(this Controller controller, NotificationLog? log)
|
||||
{
|
||||
if (log == null) return;
|
||||
|
||||
var channel = log.Channel == NotificationChannel.Sms ? "SMS" : "Email";
|
||||
switch (log.Status)
|
||||
{
|
||||
case NotificationStatus.Sent:
|
||||
controller.ToastInfo($"{channel} sent to {log.RecipientName} ({log.Recipient}).");
|
||||
break;
|
||||
case NotificationStatus.Skipped:
|
||||
controller.ToastWarning(!string.IsNullOrEmpty(log.Message)
|
||||
? $"{channel} skipped: {log.Message}"
|
||||
: $"{channel} notification was skipped.");
|
||||
break;
|
||||
case NotificationStatus.Failed:
|
||||
controller.ToastWarning(!string.IsNullOrEmpty(log.ErrorMessage)
|
||||
? $"{channel} delivery failed: {log.ErrorMessage}"
|
||||
: $"{channel} notification failed.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user