snap-utils-0.1.2: Snap Framework utilities.

Safe HaskellNone



There are two components to the Alerts module.

  1. Heist splices for rendering the alerts
  2. Alert redirection helpers which pass the alert type and message via the HTTP GET query string.

First the Heist splices can be added with the addAlertSplices function which takes the Heist Snaplet as an argument.

 initApp :: SnapletInit App App
 initApp = makeSnaplet "app" "An snaplet example application." Nothing $ do
     h <- nestSnaplet "heist" heist $ heistInit "templates"
     addAlertSplices h
     return $ App h

Second, generate an alert in a handler.

 import Snap.Utils.Alert (alertSuccess)

 actionSuccess :: Handler App App ()
 actionSuccess reg = alertSuccess msg url
     where msg = "Successfully completed an action!"
           url = "/"

Third, ensure the Heist template has a place to bind alerts.

  <div class="alerts">
   <div class="fade in alert alert-${alert-type} alert-dismissable">
     <button type="button" class="close" data-dismiss="alert">&times;</button>

Snap.Utils.Alert is different from similar modules because it relies on more traditional HTTP-based methods of stateless control flow like GET queries instead of relying on cookies and server state continuations. Snap.Extras.FlashNotice, for example, uses cookies to store alert state.



data AlertType Source




addAlertSplices :: HasHeist b => Snaplet (Heist b) -> Initializer b v ()Source

Add the compiled and interpreted alert splices to the <alerts> tag with nested tags of <alert-text> and <alert-type> which will be bound to an AlertType.

alertRedirect :: MonadSnap m => AlertType -> Text -> URL -> m aSource

302 redirect to the target page URL with the specified AlertType and message.

alertRedirect' :: MonadSnap m => AlertType -> Text -> URL -> SimpleQuery -> m aSource

Same as alertRedirect but accepts additional query parameters.

alertInfo :: MonadSnap m => Text -> URL -> m aSource

alertUrl :: AlertType -> Text -> URL -> SimpleQuery -> URLSource

Generate a URL with an alert query string without redirecting to the URL.