Search code examples
haskellihp

How to pass an action into Application.Helper.View


In Application.Helper.View I would like to create an HTML component like:

button :: action -> Text -> Html
button action label = [hsx|<a href={urlTo action}>{label}</a>|]

which I can use in various views like: {button NewSessionAction "Login"}

However, what is the correct type signature for an action? Checking the source for urlTo (https://ihp.digitallyinduced.com/api-docs/src/IHP.RouterSupport.html#urlTo) shows that action conforms to HasPath but still I am not sure how I should write my function.

I am a novice at Haskell and so any guidance along with the "why" would be a great help.


Solution

  • (Answer by @pedrofurla and @MarcScholten)

    1. Import IHP.RouterPrelude:
    import qualified IHP.RouterPrelude as Router
    
    1. Change the HTML component function to look like:
    button :: (?context :: context, ConfigProvider context, Router.HasPath action) => action -> Text -> Html
    button action label = [hsx|<a href={Router.urlTo action}>{label}</a>|]