{-|
Module: IHP.FlashMessages.ControllerFunctions
Description: Success and error messages for your application
Copyright: (c) digitally induced GmbH, 2020
-}
module IHP.FlashMessages.ControllerFunctions where

import IHP.Prelude
import IHP.FlashMessages.Types
import IHP.Controller.Context
import IHP.Controller.Session
import qualified Data.Maybe as Maybe

-- | Sets a flash messages. This is shown to the user when the next view is rendered.
--
-- Will be rendered in a bootstrap alert, with the @alert-success@ styling.
-- Take a look at https://getbootstrap.com/docs/4.5/components/alerts/ for how this will look like.
--
-- This requires 'IHP.ViewSupport.renderFlashMessages' to be placed somewhere in the layout or page of the next view.
-- For example:
--
-- > myLayout view = [hsx|
-- >     {renderFlashMessages}
-- >     <main>{view}</main>
-- > |]
setSuccessMessage :: (?context :: ControllerContext) => Text -> IO ()
setSuccessMessage :: (?context::ControllerContext) => Text -> IO ()
setSuccessMessage = ByteString -> Text -> IO ()
forall value.
(?context::ControllerContext, Serialize value) =>
ByteString -> value -> IO ()
setSession ByteString
successMessageKey

-- | Sets a flash messages. This is shown to the user when the next view is rendered.
--
-- Will be rendered in a bootstrap alert, with the @alert-danger@ styling.
-- Take a look at https://getbootstrap.com/docs/4.5/components/alerts/ for how this will look like.
--
-- This requires 'IHP.ViewSupport.renderFlashMessages' to be placed somewhere in the layout or page of the next view.
-- For example:
--
-- > myLayout view = [hsx|
-- >     {renderFlashMessages}
-- >     <main>{view}</main>
-- > |]
setErrorMessage :: (?context :: ControllerContext) => Text -> IO ()
setErrorMessage :: (?context::ControllerContext) => Text -> IO ()
setErrorMessage = ByteString -> Text -> IO ()
forall value.
(?context::ControllerContext, Serialize value) =>
ByteString -> value -> IO ()
setSession ByteString
errorMessageKey

-- | Returns the flash message currently set
getSuccessMessage :: (?context :: ControllerContext) => IO (Maybe Text)
getSuccessMessage :: (?context::ControllerContext) => IO (Maybe Text)
getSuccessMessage = ByteString -> IO (Maybe Text)
forall value.
(?context::ControllerContext, Serialize value) =>
ByteString -> IO (Maybe value)
getSession ByteString
successMessageKey

-- | Removes the current flash message
clearSuccessMessage :: (?context :: ControllerContext) => IO ()
clearSuccessMessage :: (?context::ControllerContext) => IO ()
clearSuccessMessage = ByteString -> Text -> IO ()
forall value.
(?context::ControllerContext, Serialize value) =>
ByteString -> value -> IO ()
setSession ByteString
successMessageKey (Text
"" :: Text)

-- Returns a list of all flash messages which need to be displayed to the user
--
-- Then clears the flash messages so they won't be displayed again.
getAndClearFlashMessages :: (?context :: ControllerContext) => IO [FlashMessage]
getAndClearFlashMessages :: (?context::ControllerContext) => IO [FlashMessage]
getAndClearFlashMessages = do
    Maybe Text
successMessage <- ByteString -> IO (Maybe Text)
forall value.
(?context::ControllerContext, Serialize value) =>
ByteString -> IO (Maybe value)
getSessionAndClear ByteString
successMessageKey
    Maybe Text
errorMessage <- ByteString -> IO (Maybe Text)
forall value.
(?context::ControllerContext, Serialize value) =>
ByteString -> IO (Maybe value)
getSessionAndClear ByteString
errorMessageKey
    [FlashMessage] -> IO [FlashMessage]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([FlashMessage] -> IO [FlashMessage])
-> [FlashMessage] -> IO [FlashMessage]
forall a b. (a -> b) -> a -> b
$ [Maybe FlashMessage] -> [FlashMessage]
forall a. [Maybe a] -> [a]
Maybe.catMaybes (((Text -> FlashMessage) -> Maybe Text -> Maybe FlashMessage
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> FlashMessage
SuccessFlashMessage Maybe Text
successMessage)Maybe FlashMessage -> [Maybe FlashMessage] -> [Maybe FlashMessage]
forall a. a -> [a] -> [a]
:((Text -> FlashMessage) -> Maybe Text -> Maybe FlashMessage
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> FlashMessage
ErrorFlashMessage Maybe Text
errorMessage)Maybe FlashMessage -> [Maybe FlashMessage] -> [Maybe FlashMessage]
forall a. a -> [a] -> [a]
:[])

successMessageKey :: ByteString
successMessageKey :: ByteString
successMessageKey = ByteString
"flashSuccessMessage"

errorMessageKey :: ByteString
errorMessageKey :: ByteString
errorMessageKey = ByteString
"flashErrorMessage"

initFlashMessages :: (?context :: ControllerContext) => IO ()
initFlashMessages :: (?context::ControllerContext) => IO ()
initFlashMessages = do
    [FlashMessage]
flashMessages <- IO [FlashMessage]
(?context::ControllerContext) => IO [FlashMessage]
getAndClearFlashMessages
    [FlashMessage] -> IO ()
forall value.
(?context::ControllerContext, Typeable value) =>
value -> IO ()
putContext [FlashMessage]
flashMessages