{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}
module Ema.Example.Ex01_Basic where
import Ema
import Ema.Example.Common (tailwindLayout)
import Ema.Route.Generic.TH
import Text.Blaze.Html5 ((!))
import Text.Blaze.Html5 qualified as H
import Text.Blaze.Html5.Attributes qualified as A
data Route
= Route_Index
| Route_About
deriving stock (Int -> Route -> ShowS
[Route] -> ShowS
Route -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Route] -> ShowS
$cshowList :: [Route] -> ShowS
show :: Route -> String
$cshow :: Route -> String
showsPrec :: Int -> Route -> ShowS
$cshowsPrec :: Int -> Route -> ShowS
Show, Route -> Route -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Route -> Route -> Bool
$c/= :: Route -> Route -> Bool
== :: Route -> Route -> Bool
$c== :: Route -> Route -> Bool
Eq, Eq Route
Route -> Route -> Bool
Route -> Route -> Ordering
Route -> Route -> Route
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Route -> Route -> Route
$cmin :: Route -> Route -> Route
max :: Route -> Route -> Route
$cmax :: Route -> Route -> Route
>= :: Route -> Route -> Bool
$c>= :: Route -> Route -> Bool
> :: Route -> Route -> Bool
$c> :: Route -> Route -> Bool
<= :: Route -> Route -> Bool
$c<= :: Route -> Route -> Bool
< :: Route -> Route -> Bool
$c< :: Route -> Route -> Bool
compare :: Route -> Route -> Ordering
$ccompare :: Route -> Route -> Ordering
Ord, forall x. Rep Route x -> Route
forall x. Route -> Rep Route x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Route x -> Route
$cfrom :: forall x. Route -> Rep Route x
Generic)
deriveGeneric ''Route
deriveIsRoute ''Route [t|'[]|]
instance EmaSite Route where
siteInput :: forall (m :: Type -> Type).
(MonadIO m, MonadUnliftIO m, MonadLoggerIO m) =>
Some @Type Action
-> SiteArg Route -> m (Dynamic m (RouteModel Route))
siteInput Some @Type Action
_ SiteArg Route
_ = forall (f :: Type -> Type) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall (f :: Type -> Type) a. Applicative f => a -> f a
pure ()
siteOutput :: forall (m :: Type -> Type).
(MonadIO m, MonadLoggerIO m) =>
Prism' String Route
-> RouteModel Route -> Route -> m (SiteOutput Route)
siteOutput Prism' String Route
rp () Route
r =
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Format -> a -> Asset a
Ema.AssetGenerated Format
Ema.Html forall a b. (a -> b) -> a -> b
$
Html -> Html -> LByteString
tailwindLayout (Html -> Html
H.title Html
"Basic site" forall (m :: Type -> Type) a b. Monad m => m a -> m b -> m b
>> Html
H.base forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.href AttributeValue
"/") forall a b. (a -> b) -> a -> b
$
Html -> Html
H.div forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"container mx-auto mt-8 p-2" forall a b. (a -> b) -> a -> b
$ do
Html -> Html
H.h1 forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"text-3xl font-bold" forall a b. (a -> b) -> a -> b
$ Html
"Basic site"
case Route
r of
Route
Route_Index -> do
Html
"You are on the index page. "
Route -> Html -> Html
routeElem Route
Route_About Html
"Go to About"
Route
Route_About -> do
Route -> Html -> Html
routeElem Route
Route_Index Html
"Go to Index"
Html
". You are on the about page. "
where
routeElem :: Route -> Html -> Html
routeElem Route
r' Html
w = do
Html -> Html
H.a forall h. Attributable h => h -> Attribute -> h
! AttributeValue -> Attribute
A.class_ AttributeValue
"text-red-500 hover:underline" forall h. Attributable h => h -> Attribute -> h
! Route -> Attribute
routeHref Route
r' forall a b. (a -> b) -> a -> b
$ Html
w
routeHref :: Route -> Attribute
routeHref Route
r' =
AttributeValue -> Attribute
A.href (forall a. IsString a => String -> a
fromString forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToString a => a -> String
toString forall a b. (a -> b) -> a -> b
$ forall r. HasCallStack => Prism' String r -> r -> Text
Ema.routeUrl Prism' String Route
rp Route
r')
main :: IO ()
main :: IO ()
main = forall (f :: Type -> Type) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ forall r.
(Show r, Eq r, EmaStaticSite r) =>
SiteArg r -> IO [String]
Ema.runSite @Route ()