packages feed

ema-0.8.0.0: src/Ema/Example/Ex05_MultiRoute.hs

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Demonstration of `MultiRoute`.
module Ema.Example.Ex05_MultiRoute where

import Ema
import Ema.Example.Common (tailwindLayout)
import Ema.Example.Ex00_Hello qualified as Ex00
import Ema.Example.Ex01_Basic qualified as Ex01
import Ema.Example.Ex02_Clock qualified as Ex02
import Ema.Example.Ex03_Store qualified as Ex03
import Ema.Route.Generic
import Ema.Route.Lib.Multi (MultiRoute)
import Generics.SOP (I (I), NP (Nil, (:*)))
import Text.Blaze.Html5 ((!))
import Text.Blaze.Html5 qualified as H
import Text.Blaze.Html5.Attributes qualified as A

type R =
  MultiRoute
    '[ TopRoute
     , FolderRoute "hello" Ex00.Route
     , FolderRoute "basic" Ex01.Route
     , FolderRoute "clock" Ex02.Route
     , FolderRoute "clockfast" Ex02.Route
     , FolderRoute "store" Ex03.Route
     ]

newtype TopRoute = TopRoute ()
  deriving newtype
    (Show, Eq, Ord, Generic, IsRoute)

instance EmaSite TopRoute where
  siteInput _ _ = pure $ pure ()
  siteOutput _enc _ _ =
    pure $ Ema.AssetGenerated Ema.Html renderIndex

main :: IO ()
main = do
  void $ Ema.runSite @R $ I () :* I () :* I () :* I Ex02.delayNormal :* I Ex02.delayFast :* I () :* Nil

renderIndex :: LByteString
renderIndex =
  tailwindLayout (H.title "Ex05_MultiRoute" >> H.base ! A.href "/") $
    H.div ! A.class_ "container mx-auto text-center mt-8 p-2" $ do
      H.p "You can compose Ema sites. Here are three sites composed to produce one, using Ema.Route.Lib.Multi:"
      H.ul ! A.class_ "flex flex-col justify-center .items-center mt-4 space-y-4" $ do
        -- NOTE: Since we don't have the MultiModel, we must hardcode the routes here.
        H.li $ routeElem "hello" "Ex00_Hello"
        H.li $ routeElem "basic" "Ex01_Basic"
        H.li $ routeElem "clock" "Ex02_Clock"
        H.li $ routeElem "clockfast" "Ex02_Clock (Fast)"
        H.li $ routeElem "store" "Ex03_Store"
  where
    routeElem r w = do
      H.a ! A.class_ "text-xl text-purple-500 hover:underline" ! A.href r $ w