Skip to content

Commit ac3272d

Browse files
committed
WIP
1 parent 146a872 commit ac3272d

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

ghcjs/currency-converter/app.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ common pkg
4646
App.Widgets.SwapAmounts
4747
App.Widgets.Switch
4848
App.Widgets.Templates
49+
Functora.Miso.Prelude
50+
Functora.Miso.Storage
4951

5052
ghc-options:
5153
-Werror -Weverything -Wno-all-missed-specialisations

ghcjs/currency-converter/src/App/Widgets/Fav.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fav st =
4646
$ Button.raised
4747
( Button.config
4848
& Button.setOnClick saveAction
49-
& Button.setIcon (Just "add_box")
49+
& Button.setIcon (Just "favorite")
5050
& Button.setAttributes
5151
[ Theme.secondaryBg,
5252
class_ "fill"

ghcjs/currency-converter/src/App/Widgets/Menu.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,16 @@ menu st =
6262
$ IconButton.iconButton
6363
( IconButton.config
6464
& IconButton.setOnClick
65-
( pureUpdate 0 (& #modelFav .~ Opened)
65+
( pureUpdate 0
66+
$ (& #modelFav .~ Opened)
67+
. ( &
68+
#modelState
69+
. #stDoc
70+
. #stDocPreFavName
71+
. #fieldInput
72+
. #uniqueValue
73+
.~ mempty
74+
)
6675
)
6776
& IconButton.setAttributes
6877
[ TopAppBar.actionItem,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{-# LANGUAGE CPP #-}
2+
{-# OPTIONS_GHC -Wno-orphans #-}
3+
4+
module Functora.Miso.Prelude
5+
( module X,
6+
inspect,
7+
consoleLog,
8+
)
9+
where
10+
11+
#if defined(__GHCJS__) || defined(ghcjs_HOST_OS) || defined(wasi_HOST_OS)
12+
import qualified Data.Binary as Binary (get, put)
13+
#endif
14+
import Functora.Cfg as X
15+
import Functora.Prelude as X hiding
16+
( Field (..),
17+
String,
18+
Text,
19+
field,
20+
inspect,
21+
)
22+
import qualified Functora.Prelude as Prelude
23+
import Miso as X hiding
24+
( Key,
25+
URI,
26+
at,
27+
consoleLog,
28+
for_,
29+
view,
30+
)
31+
import qualified Miso
32+
import Miso.String as X
33+
( FromMisoString,
34+
MisoString,
35+
ToMisoString,
36+
fromMisoString,
37+
toMisoString,
38+
)
39+
import Type.Reflection
40+
41+
#if defined(__GHCJS__) || defined(ghcjs_HOST_OS) || defined(wasi_HOST_OS)
42+
instance Binary MisoString where
43+
put = Binary.put . fromMisoString @Prelude.Text
44+
get = fmap (toMisoString @Prelude.Text) Binary.get
45+
46+
instance From Prelude.Text MisoString where
47+
from = toMisoString
48+
49+
instance From Prelude.String MisoString where
50+
from = toMisoString
51+
52+
instance From MisoString Prelude.Text where
53+
from = fromMisoString
54+
55+
56+
instance From MisoString Prelude.String where
57+
from = fromMisoString
58+
59+
instance ConvertUtf8 MisoString ByteString where
60+
encodeUtf8 = encodeUtf8 . from @MisoString @Prelude.Text
61+
decodeUtf8 = from @Prelude.Text @MisoString . decodeUtf8
62+
decodeUtf8Strict = fmap (from @Prelude.Text @MisoString) . decodeUtf8Strict
63+
#endif
64+
65+
inspect :: (Show a, Data a) => a -> MisoString
66+
inspect x =
67+
case typeOf x `eqTypeRep` typeRep @MisoString of
68+
Just HRefl -> x
69+
Nothing -> toMisoString $ Prelude.inspect @Prelude.Text x
70+
71+
consoleLog :: (Show a, Data a) => a -> JSM ()
72+
consoleLog = Miso.consoleLog . inspect
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module Functora.Miso.Storage
2+
( insertStorage,
3+
selectStorage,
4+
)
5+
where
6+
7+
import qualified Data.Text.Lazy.Encoding as TL
8+
import Functora.Miso.Prelude
9+
import qualified Language.Javascript.JSaddle as JS
10+
11+
insertStorage :: (ToJSON a) => MisoString -> a -> JSM ()
12+
insertStorage key raw = do
13+
val <-
14+
either throw (pure . toMisoString)
15+
. TL.decodeUtf8'
16+
. unTagged
17+
$ encodeJson raw
18+
void
19+
$ JS.global
20+
^. JS.js2 @MisoString "insertStorage" key val
21+
22+
selectStorage :: (FromJSON a) => MisoString -> (Maybe a -> JSM ()) -> JSM ()
23+
selectStorage key after = do
24+
success <- JS.function $ \_ _ ->
25+
handleAny (\e -> consoleLog e >> after Nothing) . \case
26+
[val] -> do
27+
valExist <- ghcjsPure $ JS.isTruthy val
28+
if not valExist
29+
then after Nothing
30+
else do
31+
raw <- JS.fromJSVal @MisoString val
32+
str <- maybe (throwString @MisoString "Storage bad type!") pure raw
33+
res <- either throwString pure $ decodeJson str
34+
after $ Just res
35+
_ ->
36+
throwString @MisoString "Storage bad argv!"
37+
failure <-
38+
JS.function $ \_ _ _ -> consoleLog @MisoString "Storage reader failure!"
39+
prom <-
40+
JS.global ^. JS.js1 @MisoString "selectStorage" key
41+
void
42+
$ prom
43+
^. JS.js2 @MisoString "then" success failure

0 commit comments

Comments
 (0)