-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement HX-Retarget error handling
- Loading branch information
Showing
14 changed files
with
250 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,79 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
|
||
module Api.Base where | ||
|
||
import Lucid ( | ||
Html, | ||
a_, | ||
b_, | ||
body_, | ||
br_, | ||
class_, | ||
div_, | ||
doctypehtml_, | ||
h1_, | ||
href_, | ||
id_, | ||
p_, | ||
script_, | ||
src_, | ||
target_, | ||
) | ||
import Servant ( | ||
Get, | ||
HasServer (ServerT), | ||
Proxy (..), | ||
(:<|>), | ||
(:>), | ||
type (:<|>) (..), | ||
) | ||
|
||
import Api.Templates.Base.About (renderAbout) | ||
import Api.Templates.Base.Footer (renderFooter) | ||
import Api.Templates.Base.Header (renderBanner, renderHeader, renderNavigation) | ||
import Api.Templates.Base.Home (renderHome) | ||
import Api.Templates.Helpers.Alpine (useAlpine) | ||
import Api.Templates.Helpers.Htmx (hxGet_, hxSwap_, hxTrigger_, useHtmxJsExt, useHtmxVersion) | ||
import Api.Templates.User.User (renderAddUserForm) | ||
import Api.Templates.Helpers.Htmx (useHtmxJsExt, useHtmxVersion) | ||
import Config (AppT) | ||
import Control.Monad.Cont (MonadIO) | ||
import Data.Text (Text) | ||
import Servant.API.ContentTypes.Lucid (HTML) | ||
|
||
baseTemplate :: Html () -> Html () -> Html () | ||
baseTemplate title content = do | ||
baseTemplate :: Html () -> Text -> Html () -> Html () | ||
baseTemplate title pageName content = do | ||
doctypehtml_ $ do | ||
renderHeader title | ||
body_ $ do | ||
renderBanner | ||
renderNavigation | ||
renderNavigation pageName | ||
div_ [id_ "errors", class_ "max-w-screen-xl mx-auto"] mempty | ||
content | ||
renderFooter | ||
useHtmxVersion (1, 9, 12) | ||
useHtmxJsExt | ||
useAlpine | ||
|
||
type BaseAPI = Get '[HTML] (Html ()) | ||
type BaseAPI = | ||
Get '[HTML] (Html ()) | ||
:<|> "about" :> Get '[HTML] (Html ()) | ||
|
||
baseApi :: Proxy BaseAPI | ||
baseApi = Proxy | ||
|
||
-- | The server that runs the UserAPI | ||
baseServer :: (MonadIO m) => ServerT BaseAPI (AppT m) | ||
baseServer = base | ||
baseServer = base :<|> about | ||
|
||
about :: (MonadIO m) => AppT m (Html ()) | ||
about = | ||
return $ | ||
baseTemplate | ||
"Hastl | About" | ||
"About" | ||
renderAbout | ||
|
||
base :: (MonadIO m) => AppT m (Html ()) | ||
base = | ||
return $ | ||
baseTemplate | ||
"hastl" | ||
( div_ [class_ "mt-4"] $ do | ||
div_ [class_ "max-w-screen-xl mx-auto"] $ do | ||
h1_ [class_ "text-3xl font-bold text-gray-900"] "Welcome to hastl" | ||
p_ [class_ "text-gray-600"] $ do | ||
"hastl is a modern " | ||
a_ [href_ "haskell.org", target_ "_blank"] "Haskell" | ||
" web application using " | ||
b_ "(H)tmx, " | ||
b_ "(A)lpine.js, " | ||
b_ "(S)ervant, " | ||
b_ "(T)ailwind-css " | ||
"and" | ||
b_ "(L)ucid. " | ||
"It is licensed under MIT and is entirely free and open source." | ||
br_ [] | ||
p_ [class_ "text-gray-600"] "Try it out below by adding guests to your awesome party!" | ||
renderAddUserForm | ||
div_ [hxGet_ "/users", hxSwap_ "innerHTML", hxTrigger_ "load"] $ p_ "Loading..." | ||
) | ||
"Hastl | Modern Haskell Web Application Starter Kit" | ||
"Home" | ||
renderHome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Api.Templates.Base.About where | ||
|
||
import Api.Templates.Base.Home (renderBadges) | ||
import Lucid | ||
|
||
renderAbout :: Html () | ||
renderAbout = | ||
div_ [class_ "mt-4"] $ do | ||
div_ [class_ "max-w-screen-xl mx-auto"] $ do | ||
h1_ [class_ "text-xl font-bold text-gray-900"] "About" | ||
p_ [class_ "text-gray-600 mt-1"] $ do | ||
"hastl is a modern Haskell web application using Htmx, Alpine.js, Servant," | ||
"Tailwind-css and Lucid. It is licensed under MIT and is entirely free and open source." | ||
br_ [] | ||
br_ [] | ||
"This is a simple about page, showing one of the features of the hastl template, routing." | ||
"Hastl allows you to split your application into multiple strongly typed Servant API's and combine different endpoints that return either JSON, raw HTML or HTMX-enabled hypermedia." | ||
br_ [] | ||
br_ [] | ||
"Additionally, hastl provides tooling to auto-reload your server on changes and recompile your tailwindcss stylesheets automatically, giving you a fast feedback loop. " | ||
"Check out " | ||
a_ [href_ "https://github.com/eldr-io/hastl", target_ "_blank"] "the hastl docs" | ||
" for more information." | ||
br_ [] | ||
br_ [] | ||
b_ "hastl was built on top of the awesome servant-persistant example by Matt Parsons, so be sure to " | ||
a_ [href_ "https://github.com/parsonsmatt/servant-persistent", target_ "_blank"] "check it out" | ||
"!" | ||
div_ [class_ "mt-5"] $ do | ||
h2_ [class_ "text-md font-bold text-gray-900"] "Built with:" | ||
renderBadges |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module Api.Templates.Base.Home where | ||
|
||
import Api.Templates.Helpers.Htmx | ||
import Api.Templates.User.User (renderAddUserForm) | ||
import Lucid | ||
|
||
renderHome :: Lucid.Html () | ||
renderHome = | ||
div_ [class_ "mt-4"] $ do | ||
div_ | ||
[class_ "max-w-screen-xl mx-auto"] | ||
$ do | ||
h1_ [class_ "text-3xl font-bold text-gray-900"] "Welcome to hastl" | ||
p_ [class_ "text-gray-600"] $ do | ||
"hastl is a modern " | ||
a_ [href_ "haskell.org", target_ "_blank"] "Haskell" | ||
" web application using " | ||
b_ "(H)tmx, " | ||
b_ "(A)lpine.js, " | ||
b_ "(S)ervant, " | ||
b_ "(T)ailwind-css " | ||
"and" | ||
b_ "(L)ucid. " | ||
"It is licensed under MIT and is entirely free and open source." | ||
br_ [] | ||
renderBadges | ||
p_ [class_ "text-gray-600 mt-5"] "Try it out below by adding guests to your awesome party!" | ||
renderAddUserForm | ||
p_ [class_ "text-xs text-gray-500 mt-2 mx-auto text-center"] "* Don't worry, we won't actually send anything!" | ||
div_ [hxGet_ "/users", hxSwap_ "innerHTML", hxTrigger_ "load"] $ p_ "Loading..." | ||
|
||
renderBadges :: Html () | ||
renderBadges = | ||
div_ [class_ "mx-auto max-w-screen-xl"] $ do | ||
img_ [class_ "col-span-1 inline", src_ "https://img.shields.io/badge/haskell-5D4F85?style=for-the-badge&logo=haskell&logoColor=white"] | ||
img_ [class_ "col-span-1 inline", src_ "https://img.shields.io/badge/htmxjs-3366CC?style=for-the-badge&logo=htmx&logoColor=white"] | ||
img_ [class_ "col-span-1 inline", src_ "https://img.shields.io/badge/alpinejs-8BC0D0?style=for-the-badge&logo=alpine.js&logoColor=white"] | ||
img_ [class_ "col-span-1 inline", src_ "https://img.shields.io/badge/Servant-5D4F85?style=for-the-badge&logo=haskell&logoColor=white"] | ||
img_ [class_ "col-span-1 inline", src_ "https://img.shields.io/badge/Tailwind-06B6D4?style=for-the-badge&logo=tailwindcss&logoColor=white"] | ||
img_ [class_ "col-span-1 inline", src_ "https://img.shields.io/badge/Lucid-5D4F85?style=for-the-badge&logo=haskell&logoColor=white"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,6 +140,12 @@ hxTrigger_ = makeAttributes "data-hx-trigger" | |
hxVals_ :: Text -> Attributes | ||
hxVals_ = makeAttributes "data-hx-vals" | ||
|
||
hxTarget500_ :: Text -> Attributes | ||
hxTarget500_ = makeAttributes "data-hx-target-500" | ||
|
||
hxTargetError_ :: Text -> Attributes | ||
hxTargetError_ = makeAttributes "data-hx-target-error" | ||
|
||
-- | <https://htmx.org/attributes/hx-ws/> | ||
hxWs_ :: Text -> Attributes | ||
hxWs_ = makeAttributes "data-hx-ws" | ||
|
@@ -203,6 +209,9 @@ useHtmxVersion semVer = script_ [defer_ "", src_ $ htmxSrcWithSemVer semVer] ("" | |
useHtmxJsExt :: (Monad m) => HtmlT m () | ||
useHtmxJsExt = script_ [defer_ "", src_ "https://unpkg.com/[email protected]/dist/ext/json-enc.js"] ("" :: Html ()) | ||
|
||
useHtmxRetargetErrorsExt :: (Monad m) => HtmlT m () | ||
useHtmxRetargetErrorsExt = script_ [defer_ "", src_ "https://unpkg.com/[email protected]/dist/ext/response-targets.js"] ("" :: Html ()) | ||
|
||
htmxSrc :: Text | ||
htmxSrc = "https://unpkg.com/htmx.org" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.