{-# LANGUAGE OverloadedStrings #-}
module Yesod.Core.Class.Breadcrumbs where
import Yesod.Core.Handler
import Yesod.Routes.Class
import Data.Text (Text)
class YesodBreadcrumbs site where
breadcrumb :: Route site -> HandlerFor site (Text , Maybe (Route site))
breadcrumbs :: YesodBreadcrumbs site => HandlerFor site (Text, [(Route site, Text)])
breadcrumbs :: HandlerFor site (Text, [(Route site, Text)])
breadcrumbs = do
Maybe (Route site)
x <- HandlerFor site (Maybe (Route site))
forall (m :: * -> *).
MonadHandler m =>
m (Maybe (Route (HandlerSite m)))
getCurrentRoute
case Maybe (Route site)
x of
Maybe (Route site)
Nothing -> (Text, [(Route site, Text)])
-> HandlerFor site (Text, [(Route site, Text)])
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
"Not found", [])
Just Route site
y -> do
(Text
title, Maybe (Route site)
next) <- Route site -> HandlerFor site (Text, Maybe (Route site))
forall site.
YesodBreadcrumbs site =>
Route site -> HandlerFor site (Text, Maybe (Route site))
breadcrumb Route site
y
[(Route site, Text)]
z <- [(Route site, Text)]
-> Maybe (Route site) -> HandlerFor site [(Route site, Text)]
forall site.
YesodBreadcrumbs site =>
[(Route site, Text)]
-> Maybe (Route site) -> HandlerFor site [(Route site, Text)]
go [] Maybe (Route site)
next
(Text, [(Route site, Text)])
-> HandlerFor site (Text, [(Route site, Text)])
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
title, [(Route site, Text)]
z)
where
go :: [(Route site, Text)]
-> Maybe (Route site) -> HandlerFor site [(Route site, Text)]
go [(Route site, Text)]
back Maybe (Route site)
Nothing = [(Route site, Text)] -> HandlerFor site [(Route site, Text)]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Route site, Text)]
back
go [(Route site, Text)]
back (Just Route site
this) = do
(Text
title, Maybe (Route site)
next) <- Route site -> HandlerFor site (Text, Maybe (Route site))
forall site.
YesodBreadcrumbs site =>
Route site -> HandlerFor site (Text, Maybe (Route site))
breadcrumb Route site
this
[(Route site, Text)]
-> Maybe (Route site) -> HandlerFor site [(Route site, Text)]
go ((Route site
this, Text
title) (Route site, Text) -> [(Route site, Text)] -> [(Route site, Text)]
forall a. a -> [a] -> [a]
: [(Route site, Text)]
back) Maybe (Route site)
next