Skip to content

Commit c100066

Browse files
committed
better currency widget
1 parent be1d461 commit c100066

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

ghcjs/currency-converter/Main.hs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ data ModelMoney = ModelMoney
8585
{ modelMoneyAmountUuid :: UUID,
8686
modelMoneyAmountInput :: Text,
8787
modelMoneyAmountOutput :: Money (Tags 'Signed |+| 'MoneyAmount),
88+
modelMoneyCurrencyUuid :: UUID,
8889
modelMoneyCurrencyInfo :: CurrencyInfo,
8990
modelMoneyCurrencyOpen :: Bool,
9091
modelMoneyCurrencySearch :: Text
@@ -104,8 +105,10 @@ newModel = do
104105
prod <- liftIO newBroadcastTChanIO
105106
cons <- liftIO . atomically $ dupTChan prod
106107
market <- newMarket
107-
topUuid <- newUuid
108-
bottomUuid <- newUuid
108+
topAmtUuid <- newUuid
109+
topCurUuid <- newUuid
110+
bottomAmtUuid <- newUuid
111+
bottomCurUuid <- newUuid
109112
let btc =
110113
CurrencyInfo
111114
{ currencyInfoCode = CurrencyCode "btc",
@@ -121,18 +124,20 @@ newModel = do
121124
ModelData
122125
{ modelDataTopMoney =
123126
ModelMoney
124-
{ modelMoneyAmountUuid = topUuid,
127+
{ modelMoneyAmountUuid = topAmtUuid,
125128
modelMoneyAmountInput = inspectMoneyAmount zero,
126129
modelMoneyAmountOutput = zero,
130+
modelMoneyCurrencyUuid = topCurUuid,
127131
modelMoneyCurrencyInfo = btc,
128132
modelMoneyCurrencyOpen = False,
129133
modelMoneyCurrencySearch = mempty
130134
},
131135
modelDataBottomMoney =
132136
ModelMoney
133-
{ modelMoneyAmountUuid = bottomUuid,
137+
{ modelMoneyAmountUuid = bottomAmtUuid,
134138
modelMoneyAmountInput = inspectMoneyAmount zero,
135139
modelMoneyAmountOutput = zero,
140+
modelMoneyCurrencyUuid = bottomCurUuid,
136141
modelMoneyCurrencyInfo = usd,
137142
modelMoneyCurrencyOpen = False,
138143
modelMoneyCurrencySearch = mempty
@@ -172,18 +177,20 @@ newModel = do
172177
ModelData
173178
{ modelDataTopMoney =
174179
ModelMoney
175-
{ modelMoneyAmountUuid = topUuid,
180+
{ modelMoneyAmountUuid = topAmtUuid,
176181
modelMoneyAmountInput = inspectMoneyAmount baseAmt,
177182
modelMoneyAmountOutput = unTag @'Base baseAmt,
183+
modelMoneyCurrencyUuid = topCurUuid,
178184
modelMoneyCurrencyInfo = baseCur,
179185
modelMoneyCurrencyOpen = False,
180186
modelMoneyCurrencySearch = mempty
181187
},
182188
modelDataBottomMoney =
183189
ModelMoney
184-
{ modelMoneyAmountUuid = bottomUuid,
190+
{ modelMoneyAmountUuid = bottomAmtUuid,
185191
modelMoneyAmountInput = inspectMoneyAmount quoteAmt,
186192
modelMoneyAmountOutput = unTag @'Quote quoteAmt,
193+
modelMoneyCurrencyUuid = bottomCurUuid,
187194
modelMoneyCurrencyInfo = quoteCur,
188195
modelMoneyCurrencyOpen = False,
189196
modelMoneyCurrencySearch = mempty
@@ -547,7 +554,7 @@ amountWidget st loc =
547554
^. #modelData
548555
. getMoneyOptic loc
549556
. #modelMoneyAmountUuid,
550-
onKeyDown onKeyDownAction,
557+
onKeyDown $ onKeyDownAction uuid,
551558
onBlur onBlurAction
552559
]
553560
]
@@ -568,18 +575,6 @@ amountWidget st loc =
568575
. getMoneyOptic loc
569576
. #modelMoneyAmountInput
570577
.~ inspectMoneyAmount output
571-
onKeyDownAction (KeyCode code) =
572-
let enterOrEscape = [13, 27] :: [Int]
573-
in PushUpdate
574-
( when (code `elem` enterOrEscape)
575-
. void
576-
. JS.eval @Text
577-
$ "document.getElementById('"
578-
<> htmlUuid uuid
579-
<> "').getElementsByTagName('input')[0].blur();"
580-
)
581-
( ChanItem 300 id
582-
)
583578
onInputAction txt =
584579
pureUpdate 300 $ \st' ->
585580
st'
@@ -621,6 +616,20 @@ amountWidget st loc =
621616
.~ loc
622617
)
623618

619+
onKeyDownAction :: UUID -> KeyCode -> Action
620+
onKeyDownAction uuid (KeyCode code) =
621+
let enterOrEscape = [13, 27] :: [Int]
622+
in PushUpdate
623+
( when (code `elem` enterOrEscape)
624+
. void
625+
. JS.eval @Text
626+
$ "document.getElementById('"
627+
<> htmlUuid uuid
628+
<> "').getElementsByTagName('input')[0].blur();"
629+
)
630+
( ChanItem 300 id
631+
)
632+
624633
currencyWidget ::
625634
Model ->
626635
TopOrBottom ->
@@ -686,7 +695,9 @@ currencyWidget st loc =
686695
. #modelMoneyCurrencyInfo
687696
)
688697
. TextField.setAttributes
689-
[ class_ "fill"
698+
[ class_ "fill",
699+
id_ . ms $ htmlUuid @Text uuid,
700+
onKeyDown $ onKeyDownAction uuid
690701
]
691702
$ TextField.config,
692703
Button.raised
@@ -701,6 +712,7 @@ currencyWidget st loc =
701712
)
702713
]
703714
where
715+
uuid = st ^. #modelData . getMoneyOptic loc . #modelMoneyCurrencyUuid
704716
search input =
705717
pureUpdate 0 $ \st' ->
706718
st'

ghcjs/currency-converter/default.nix

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
let
2-
functora = ../..;
3-
# functora = fetchTarball {
4-
# url = "https://github.com/functora/functora.github.io/archive/687980745c94639d3a8479363d4195b5d0c45033.tar.gz";
5-
# sha256 = "0rzkhwwkal0rpif4sjmp61viq0afm3d9lmlkj8vyr4fv5zk7ydjn";
6-
# };
2+
# functora = ../..;
3+
functora = fetchTarball {
4+
url = "https://github.com/functora/functora.github.io/archive/be1d461f56af9c056675779fa2be81c4347a9861.tar.gz";
5+
sha256 = "1ac4704nqhcv5bf6czawvm4l2lmnyyzzjcappbdc6zsljlac1ssn";
6+
};
77
functora-miso = import "${functora}/ghcjs/miso/default.nix" {
88
overlays = import ./overlays.nix {inherit functora;};
99
};

0 commit comments

Comments
 (0)