Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of MonadWidget #1062

Open
romefeller opened this issue Feb 29, 2024 · 2 comments
Open

Use of MonadWidget #1062

romefeller opened this issue Feb 29, 2024 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@romefeller
Copy link
Contributor

If I have this function:

mainForm :: (MonadCall t m, MonadWidget t m) => m ()
mainForm = do
ck <- getCookie
...

The getCookie functon is:
cookie :: JSM Text
cookie = document ^. js (t_ "cookie") >>= fromJSValUnchecked

getCookie :: MonadWidget t m => m Text
getCookie = liftJSM cookie

But I get the error:

error:
• Could not deduce: DomBuilderSpace m ~ GhcjsDomSpace
arising from a use of ‘mainForm’
from the context: ObeliskWidget t (R FrontendRoute) m
bound by a type expected by the context:
forall t (m :: * -> *).
ObeliskWidget t (R FrontendRoute) m =>
RoutedT t (R FrontendRoute) m ()
at /home/romefeller/weivi/weivi-frontend-poc/frontend/src/Frontend.hs:(24,22)-(25,38)
or from: a ~ ()

where:

frontend_body = subRoute $ \case
FrontendRoute_Main -> mainForm

I'm unsure how to circumvent this error (I do not remember), as it seems to involve a mismatch between the expected DOM environment and what's provided by my ObeliskWidget setup. Has anyone faced a similar issue or can offer guidance on making mainForm compatible within this context?

Thanks in advance for any help or insights!

@romefeller romefeller added the help wanted Extra attention is needed label Feb 29, 2024
@alexfmpe
Copy link
Contributor

alexfmpe commented Feb 29, 2024

You are seeing a type error because one can only run JSM inside the second argument of prerender as the first is statically rendered by the backend and does not have JS access.

prerender_ blank $ liftJSM $ void

Now, cookies in particular can be fetched both on the backend and the frontend, so you want askCookies instead:

class Monad m => HasCookies m where
askCookies :: m Cookies

@romefeller
Copy link
Contributor Author

Thanks, @alexfmpe!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants