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
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
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
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
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)
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