Skip to content

Commit 844cb43

Browse files
committed
Functora.Miso WIP
1 parent ac3272d commit 844cb43

File tree

1 file changed

+14
-41
lines changed
  • ghcjs/currency-converter/src

1 file changed

+14
-41
lines changed

ghcjs/currency-converter/src/Main.hs

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ import qualified Language.Javascript.JSaddle.Wasm as JSaddle.Wasm
1616
#endif
1717

1818
import qualified App.Misc as Misc
19-
import App.Prelude
2019
import App.Types
2120
import App.Widgets.Main
2221
import App.Widgets.Templates
2322
import qualified Data.Generics as Syb
2423
import qualified Data.Map as Map
24+
import Functora.Miso.Prelude
25+
import qualified Functora.Miso.Storage as Storage
2526
import Functora.Money hiding (Money)
2627
import Functora.Rates
2728
import qualified Functora.Web as Web
2829
import Language.Javascript.JSaddle ((!), (!!))
2930
import qualified Language.Javascript.JSaddle as JS
30-
import Miso hiding (view)
3131
import qualified Miso
3232
import qualified Text.URI as URI
3333

@@ -40,7 +40,7 @@ main =
4040
withUtf8
4141
. runApp
4242
. forever
43-
. handleAny (\e -> log e >> sleepSeconds 5)
43+
. handleAny (\e -> consoleLog e >> sleepSeconds 5)
4444
$ do
4545
uri <- URI.mkURI . inspect =<< getCurrentURI
4646
ext <- unShareUri uri
@@ -127,7 +127,7 @@ updateModel (InitUpdate ext) prevSt = do
127127
}
128128
if isJust ext
129129
then Misc.pushActionQueue nextSt $ ChanItem 0 (& #modelLoading .~ False)
130-
else selectCurrentUri $ \case
130+
else Storage.selectStorage ("current-" <> vsn) $ \case
131131
Nothing ->
132132
Misc.pushActionQueue nextSt $ ChanItem 0 (& #modelLoading .~ False)
133133
Just uri -> do
@@ -174,11 +174,12 @@ updateModel (ChanUpdate prevSt) _ = do
174174
nextSt <-
175175
handleAny
176176
( \e -> do
177-
log e
177+
consoleLog e
178178
pure $ prevSt & #modelLoading .~ False
179179
)
180180
$ foldlM (\acc updater -> evalModel $ updater acc) prevSt actions
181-
insertCurrentUri nextSt
181+
uri <- URI.mkURI $ shareLink (nextSt ^. #modelState . #stScreen) nextSt
182+
Storage.insertStorage ("current-" <> vsn) uri
182183
if nextSt ^. #modelLoading
183184
then do
184185
void
@@ -241,22 +242,23 @@ extendedEvents =
241242
syncInputs :: Model -> JSM ()
242243
syncInputs st = do
243244
void
244-
. JS.eval @Text
245+
. JS.eval @MisoString
245246
$ "Array.from(document.getElementsByTagName('mdc-text-field')).forEach( function (x) { if ( (x.getElementsByTagName('input')[0] && x.textField_.input_.tagName != 'INPUT') || (x.getElementsByTagName('textarea')[0] && x.textField_.input_.tagName != 'TEXTAREA')) { x.textField_.destroy(); x.textField_.initialize(); } });"
246247
void
247248
. Syb.everywhereM (Syb.mkM fun)
248249
$ modelState st
249250
where
250-
fun :: Unique Text -> JSM (Unique Text)
251+
fun :: Unique MisoString -> JSM (Unique MisoString)
251252
fun txt = do
252-
el <- getElementById . htmlUid @Text $ txt ^. #uniqueUid
253+
el <- getElementById . htmlUid @MisoString $ txt ^. #uniqueUid
253254
elExist <- ghcjsPure $ JS.isTruthy el
254255
when elExist $ do
255-
inps <- el ^. JS.js1 ("getElementsByTagName" :: Text) ("input" :: Text)
256+
inps <-
257+
el ^. JS.js1 ("getElementsByTagName" :: MisoString) ("input" :: MisoString)
256258
inp <- inps !! 0
257-
act <- JS.global ! ("document" :: Text) ! ("activeElement" :: Text)
259+
act <- JS.global ! ("document" :: MisoString) ! ("activeElement" :: MisoString)
258260
elActive <- JS.strictEqual inp act
259-
unless elActive $ el ^. JS.jss ("value" :: Text) (txt ^. #uniqueValue)
261+
unless elActive $ el ^. JS.jss ("value" :: MisoString) (txt ^. #uniqueValue)
260262
pure txt
261263

262264
evalModel :: (MonadThrow m, MonadUnliftIO m) => Model -> m Model
@@ -362,32 +364,3 @@ upToDate lhs rhs =
362364
diff < 3600
363365
where
364366
diff = abs . toRational $ diffUTCTime lhs rhs
365-
366-
log :: (Show a, Data a) => a -> JSM ()
367-
log = consoleLog . inspectMiso
368-
369-
insertCurrentUri :: Model -> JSM ()
370-
insertCurrentUri st = do
371-
uri <- URI.mkURI $ shareLink (st ^. #modelState . #stScreen) st
372-
void
373-
$ JS.global
374-
^. JS.js2 ("insertStorage" :: Text) ("current-" <> vsn) (ms $ URI.render uri)
375-
376-
selectCurrentUri :: (Maybe URI.URI -> JSM ()) -> JSM ()
377-
selectCurrentUri after = do
378-
success <- JS.function $ \_ _ ->
379-
handleAny (\e -> log e >> after Nothing) . \case
380-
[val] -> do
381-
valExist <- ghcjsPure $ JS.isTruthy val
382-
if not valExist
383-
then after Nothing
384-
else do
385-
raw <- JS.fromJSVal @Text val
386-
str <- maybe (throwString @Text "Storage bad type!") pure raw
387-
uri <- URI.mkURI $ fromMisoString str
388-
after $ Just uri
389-
_ ->
390-
throwString @Text "Storage bad argv!"
391-
failure <- JS.function $ \_ _ _ -> log @Text "Storage reader failure!"
392-
prom <- JS.global ^. JS.js1 ("selectStorage" :: Text) ("current-" <> vsn)
393-
void $ prom ^. JS.js2 ("then" :: Text) success failure

0 commit comments

Comments
 (0)