Bring back menu items for logging in and out

This commit is contained in:
Danielle McLean 2017-10-06 14:23:42 +11:00
parent ea47fb8b50
commit 1c4212f5d0
Signed by: 00dani
GPG key ID: 5A5D2D1AFF12EEC5
2 changed files with 16 additions and 16 deletions

View file

@ -36,13 +36,8 @@ data App = App
data MenuItem = MenuItem
{ menuItemLabel :: Text
, menuItemRoute :: Route App
, menuItemAccessCallback :: Bool
}
data MenuTypes
= NavbarLeft MenuItem
| NavbarRight MenuItem
-- This is where we define all of the routes in our application. For a full
-- explanation of the syntax, please see:
-- http://www.yesodweb.com/book/routing-and-handlers
@ -96,20 +91,14 @@ instance Yesod App where
master <- getYesod
mmsg <- getMessage
-- muser <- maybeAuthPair
muser <- maybeAuthPair
mcurrentRoute <- getCurrentRoute
-- Get the breadcrumbs, as defined in the YesodBreadcrumbs instance.
(title, parents) <- breadcrumbs
-- Define the menu items of the header.
let menuItems = []
let navbarLeftMenuItems = [x | NavbarLeft x <- menuItems]
let navbarRightMenuItems = [x | NavbarRight x <- menuItems]
let navbarLeftFilteredMenuItems = [x | x <- navbarLeftMenuItems, menuItemAccessCallback x]
let navbarRightFilteredMenuItems = [x | x <- navbarRightMenuItems, menuItemAccessCallback x]
let navbarLeftMenuItems = leftMenuItems muser
navbarRightMenuItems = rightMenuItems muser
-- We break up the default layout into two components:
-- default-layout is the contents of the body tag, and
@ -154,6 +143,17 @@ instance Yesod App where
makeLogger = return . appLogger
leftMenuItems, rightMenuItems :: Maybe (UserId, User) -> [MenuItem]
leftMenuItems _ = []
rightMenuItems = loggedOutItems `maybe` loggedInItems
where loggedOutItems =
[ "log in" `MenuItem` AuthR LoginR
]
loggedInItems (_id, user) =
[ userUsername user `MenuItem` HomeR
, "log out" `MenuItem` AuthR LogoutR
]
-- Define breadcrumbs.
instance YesodBreadcrumbs App where
breadcrumb (AuthR _) = return ("login", Just HomeR)

View file

@ -7,12 +7,12 @@
<div #navbar .collapse.navbar-collapse>
<ul .navbar-nav.mr-auto>
$forall MenuItem label route _ <- navbarLeftFilteredMenuItems
$forall MenuItem label route <- navbarLeftMenuItems
<li .nav-item :Just route == mcurrentRoute:.active>
<a .nav-link href=@{route}>#{label}
<ul .navbar-nav>
$forall MenuItem label route _ <- navbarRightFilteredMenuItems
$forall MenuItem label route <- navbarRightMenuItems
<li .nav-item :Just route == mcurrentRoute:.active>
<a .nav-link href=@{route}>#{label}
$if not $ null parents