-
-
Save adetokunbo/0b363d3e767542f7533665666cb46943 to your computer and use it in GitHub Desktop.
Using javascript function with Reflex and JSaddle
This file contains hidden or 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
{-# LANGUAGE OverloadedStrings #-} | |
{-# LANGUAGE ExtendedDefaultRules #-} | |
import Reflex.Dom | |
import Control.Monad.IO.Class | |
import Control.Monad (void) | |
import Data.Text (Text) | |
import Language.Javascript.JSaddle | |
import Control.Lens ((^.)) | |
main :: IO () | |
main = mainWidget localStorageTest | |
saveLocal :: Text -> JSM () | |
saveLocal msg = do | |
jsg "window" | |
^. js "localStorage" | |
^. jss "saveLocation" [msg] | |
return () | |
getLocal :: JSM (Maybe Text) | |
getLocal = do | |
jsv <- jsg "window" | |
^. js "localStorage" | |
^. js "saveLocation" | |
liftJSM (fromJSVal jsv) | |
localStorageTest :: MonadWidget t m => m () | |
localStorageTest = do | |
ctx <- unJSContextSingleton <$> askJSContext | |
el "div" $ do | |
t <- textInput def | |
set <- button "set value" | |
performEvent_ $ ffor (tag (current $ value t) set) $ liftIO . runJSaddle ctx . saveLocal | |
el "div" $ do | |
get <- button "get value" | |
val <- performEvent $ ffor get $ \_ -> liftIO . runJSaddle ctx $ getLocal | |
display =<< holdDyn Nothing val | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment