Skip to content

Commit 24db52d

Browse files
committed
WIP
1 parent f3c223b commit 24db52d

File tree

7 files changed

+169
-43
lines changed

7 files changed

+169
-43
lines changed

ghcjs/delivery-calculator/delivery-calculator.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ common pkg
105105
, functora-ghcjs
106106
, fuzzy
107107
, jsaddle
108+
, microlens
108109
, miso
109110
, miso-components
110111
, miso-widgets

ghcjs/delivery-calculator/src/App/Types.hs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module App.Types
99
Asset (..),
1010
newAsset,
1111
newFieldPair,
12+
newFieldPairId,
1213
Screen (..),
1314
isQrCode,
1415
unQrCode,
@@ -37,7 +38,12 @@ import Data.Functor.Barbie
3738
import qualified Data.Version as Version
3839
import Functora.Cfg
3940
import Functora.Miso.Prelude
40-
import Functora.Miso.Types as X hiding (Asset (..), newAsset, newFieldPair)
41+
import Functora.Miso.Types as X hiding
42+
( Asset (..),
43+
newAsset,
44+
newFieldPair,
45+
newFieldPairId,
46+
)
4147
import qualified Functora.Miso.Types as FM
4248
import Functora.Money hiding (Currency, Money, Text)
4349
import qualified Functora.Prelude as Prelude
@@ -77,7 +83,7 @@ data St f = St
7783
stExchangeRateAt :: UTCTime,
7884
stMerchantCurrency :: Currency f,
7985
stMerchantTele :: Field Unicode f,
80-
stMerchantFeePercent :: Field Rational f,
86+
stMerchantFeePercent :: Field DynamicField f,
8187
stOnlineOrOffline :: OnlineOrOffline,
8288
stFavName :: Field Unicode f,
8389
stPreview :: Field Unicode f,
@@ -106,7 +112,7 @@ newSt = do
106112
ct <- getCurrentTime
107113
merchantCur <- newCurrency rub
108114
tele <- newTextField "Functora"
109-
fee <- newRatioField 2
115+
fee <- newDynamicField $ DynamicFieldNumber 2
110116
fav <- newTextField mempty
111117
pre <- newTextField "Delivery Calculator"
112118
pure
@@ -117,7 +123,7 @@ newSt = do
117123
stExchangeRateAt = ct,
118124
stMerchantCurrency = merchantCur,
119125
stMerchantTele = tele,
120-
stMerchantFeePercent = fee,
126+
stMerchantFeePercent = fee & #fieldType .~ FieldTypePercent,
121127
stOnlineOrOffline = Online,
122128
stFavName = fav,
123129
stPreview = pre & #fieldType .~ FieldTypeTitle,
@@ -153,7 +159,7 @@ newAsset = do
153159
newFieldPair "Photo"
154160
$ DynamicFieldText "https://bitcoin.org/img/home/bitcoin-img.svg?1725887272"
155161
price <-
156-
newFieldPair "Price" $ DynamicFieldNumber 0
162+
newFieldPair "Price" $ DynamicFieldNumber 10
157163
qty <-
158164
newFieldPair "Quantity" $ DynamicFieldNumber 1
159165
pure
@@ -177,6 +183,17 @@ newFieldPair key val = do
177183
. #fieldOptsQrState
178184
.~ Nothing
179185

186+
newFieldPairId ::
187+
Unicode ->
188+
DynamicField ->
189+
FieldPair DynamicField Identity
190+
newFieldPairId key val = do
191+
FM.newFieldPairId key val
192+
& #fieldPairValue
193+
. #fieldOpts
194+
. #fieldOptsQrState
195+
.~ Nothing
196+
180197
data Screen
181198
= Main
182199
| Donate
@@ -218,10 +235,19 @@ stQuery st = do
218235

219236
stTeleUri :: (MonadThrow m) => Model -> m URI
220237
stTeleUri st = do
221-
base <- URI.mkURI "https://t.me"
222-
user <- URI.mkPathPiece $ st ^. #modelState . #stMerchantTele . #fieldOutput
223-
link <- stUri st
224-
key <- URI.mkQueryKey "text"
238+
base <-
239+
URI.mkURI "https://t.me"
240+
user <-
241+
URI.mkPathPiece
242+
. from @Unicode @Text
243+
$ st
244+
^. #modelState
245+
. #stMerchantTele
246+
. #fieldOutput
247+
link <-
248+
stUri st
249+
key <-
250+
URI.mkQueryKey "text"
225251
val <-
226252
URI.mkQueryValue
227253
$ "Hello, I have a question about the delivery of the following items: "

ghcjs/delivery-calculator/src/App/Widgets/Asset.hs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ where
66
import qualified App.Misc as Misc
77
import App.Types
88
import qualified Functora.Miso.Css as Css
9+
import qualified Functora.Miso.Jsm as Jsm
910
import Functora.Miso.Prelude
1011
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
1112
import qualified Functora.Miso.Widgets.Grid as Grid
@@ -38,7 +39,6 @@ assetViewer st idx =
3839
)
3940
"settings"
4041
]
41-
<> FieldPairs.fieldPairsViewer args
4242
<> ( if st ^? cloneTraversal modalOptic /= Just Opened
4343
then mempty
4444
else
@@ -51,27 +51,46 @@ assetViewer st idx =
5151
Nothing
5252
[ Grid.grid
5353
mempty
54-
$ ( FieldPairs.fieldPairsEditor
54+
$ Header.headerViewer title mempty
55+
<> ( FieldPairs.fieldPairsEditor
5556
args
5657
$ FieldPairs.defOpts
5758
& #optsAdvanced
5859
.~ False
59-
)
60-
<> [ Grid.bigCell
60+
)
61+
<> [ Grid.mediumCell
62+
[ Button.raised
63+
( Button.config
64+
& Button.setIcon (Just "delete_forever")
65+
& Button.setAttributes [Css.fullWidth]
66+
& Button.setOnClick
67+
( PushUpdate
68+
. Instant
69+
$ Jsm.removeAt
70+
( #modelState
71+
. #stAssets
72+
)
73+
idx
74+
)
75+
)
76+
"Remove"
77+
],
78+
Grid.mediumCell
6179
[ Button.raised
6280
( Button.config
6381
& Button.setOnClick closeAction
64-
& Button.setIcon (Just "arrow_back")
82+
& Button.setIcon (Just "save")
6583
& Button.setAttributes [Css.fullWidth]
6684
)
67-
"Back"
85+
"Save"
6886
]
6987
]
7088
]
7189
mempty
7290
)
7391
]
7492
)
93+
<> FieldPairs.fieldPairsViewer args
7594
where
7695
args =
7796
FieldPairs.Args

ghcjs/delivery-calculator/src/App/Widgets/Main.hs

Lines changed: 102 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module App.Widgets.Main (mainWidget, pasteWidget) where
1+
module App.Widgets.Main (mainWidget) where
22

33
import qualified App.Misc as Misc
44
import App.Types
@@ -12,6 +12,8 @@ import qualified Functora.Miso.Widgets.Field as Field
1212
import qualified Functora.Miso.Widgets.FieldPairs as FieldPairs
1313
import qualified Functora.Miso.Widgets.Grid as Grid
1414
import qualified Functora.Miso.Widgets.Header as Header
15+
import qualified Functora.Money as Money
16+
import Lens.Micro ((^..))
1517
import qualified Material.Button as Button
1618
import qualified Material.LayoutGrid as LayoutGrid
1719
import qualified Material.Theme as Theme
@@ -106,8 +108,19 @@ screenWidget st@Model {modelState = St {stScreen = Donate}} =
106108
]
107109
]
108110
screenWidget st@Model {modelState = St {stScreen = Main}} =
109-
Asset.assetsViewer st
110-
<> [ Grid.mediumCell
111+
( if null assets
112+
then mempty
113+
else buttons
114+
)
115+
<> Asset.assetsViewer st
116+
<> totalViewer st
117+
<> buttons
118+
where
119+
assets :: [View Action]
120+
assets = Asset.assetsViewer st
121+
buttons :: [View Action]
122+
buttons =
123+
[ Grid.mediumCell
111124
[ Button.raised
112125
( Button.config
113126
& Button.setIcon (Just "add_box")
@@ -120,7 +133,7 @@ screenWidget st@Model {modelState = St {stScreen = Main}} =
120133
)
121134
"Add item"
122135
],
123-
Grid.mediumCell
136+
Grid.mediumCell
124137
[ Button.raised
125138
( Button.config
126139
& Button.setIcon (Just "send")
@@ -133,30 +146,92 @@ screenWidget st@Model {modelState = St {stScreen = Main}} =
133146
)
134147
"Order via Telegram"
135148
]
136-
]
149+
]
150+
151+
totalViewer :: Model -> [View Action]
152+
totalViewer st =
153+
if base == 0
154+
then mempty
155+
else
156+
Header.headerViewer "Total" mempty
157+
<> FieldPairs.fieldPairsViewer
158+
FieldPairs.Args
159+
{ FieldPairs.argsModel = st,
160+
FieldPairs.argsOptic =
161+
constTraversal
162+
[ newFieldPairId ("Subtotal " <> baseCur)
163+
. DynamicFieldText
164+
$ inspectRatioDef base,
165+
newFieldPairId ("Subtotal " <> quoteCur)
166+
. DynamicFieldText
167+
$ inspectRatioDef quote,
168+
FieldPair (newTextFieldId "Fee %")
169+
$ uniqueToIdentity fee
170+
& #fieldOpts
171+
. #fieldOptsQrState
172+
.~ Nothing,
173+
newFieldPairId ("Total " <> quoteCur)
174+
. DynamicFieldText
175+
. inspectRatioDef
176+
. foldField quote
177+
$ fee
178+
],
179+
FieldPairs.argsAction = PushUpdate . Instant,
180+
FieldPairs.argsEmitter = Misc.pushActionQueue st . Instant
181+
}
182+
where
183+
fee = st ^. #modelState . #stMerchantFeePercent
184+
rate = st ^. #modelState . #stExchangeRate . #fieldOutput
185+
base =
186+
foldl
187+
( \acc fps ->
188+
if any
189+
((== FieldTypeNumber) . (^. #fieldPairValue . #fieldType))
190+
fps
191+
then acc + foldl foldFieldPair 1 fps
192+
else acc
193+
)
194+
0
195+
( st
196+
^.. #modelState
197+
. #stAssets
198+
. each
199+
. #assetFieldPairs
200+
)
201+
quote =
202+
rate * base
203+
baseCur =
204+
st
205+
^. #modelState
206+
. #stAssetCurrency
207+
. #currencyOutput
208+
. #currencyInfoCode
209+
. to Money.inspectCurrencyCode
210+
. to toUpper
211+
quoteCur =
212+
st
213+
^. #modelState
214+
. #stMerchantCurrency
215+
. #currencyOutput
216+
. #currencyInfoCode
217+
. to Money.inspectCurrencyCode
218+
. to toUpper
219+
220+
foldField :: Rational -> Field DynamicField f -> Rational
221+
foldField acc Field {fieldType = typ, fieldOutput = out} =
222+
case out of
223+
DynamicFieldNumber x
224+
| typ == FieldTypeNumber ->
225+
acc * x
226+
DynamicFieldNumber x
227+
| typ == FieldTypePercent ->
228+
acc * (1 + (x / 100))
229+
_ ->
230+
acc
137231

138-
pasteWidget ::
139-
Unicode ->
140-
((Maybe Unicode -> JSM ()) -> JSM ()) ->
141-
ATraversal' Model (Field Unicode Unique) ->
142-
Maybe (Field.OptsWidget Model Action)
143-
pasteWidget icon selector optic =
144-
Just
145-
. Field.ActionWidget icon mempty
146-
. PushUpdate
147-
. Instant
148-
$ \prev -> do
149-
selector $ \case
150-
Nothing ->
151-
Jsm.popupText @Unicode "Failure!"
152-
Just res -> do
153-
Misc.pushActionQueue prev
154-
. Instant
155-
$ pure
156-
. (& cloneTraversal optic . #fieldOutput .~ res)
157-
. (& cloneTraversal optic . #fieldInput . #uniqueValue .~ res)
158-
Jsm.popupText @Unicode "Success!"
159-
pure prev
232+
foldFieldPair :: Rational -> FieldPair DynamicField f -> Rational
233+
foldFieldPair acc =
234+
foldField acc . fieldPairValue
160235

161236
tosWidget :: View Action
162237
tosWidget =

ghcjs/delivery-calculator/src/App/Widgets/Menu.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ menu st =
244244
)
245245
],
246246
Grid.mediumCell
247-
[ Field.ratioField
247+
[ Field.dynamicField
248248
Field.Args
249249
{ Field.argsModel = st,
250250
Field.argsOptic =
-2.53 KB
Loading

ghcjs/miso-widgets/src/Functora/Miso/Types.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Functora.Miso.Types
1414
newFieldId,
1515
newRatioField,
1616
newTextField,
17+
newTextFieldId,
1718
newPasswordField,
1819
newDynamicField,
1920
newDynamicFieldId,
@@ -197,6 +198,10 @@ newTextField :: (MonadIO m) => Unicode -> m (Field Unicode Unique)
197198
newTextField output =
198199
newField FieldTypeText output id
199200

201+
newTextFieldId :: Unicode -> Field Unicode Identity
202+
newTextFieldId output =
203+
newFieldId FieldTypeText id output
204+
200205
newPasswordField :: (MonadIO m) => Unicode -> m (Field Unicode Unique)
201206
newPasswordField output =
202207
newField FieldTypePassword output id

0 commit comments

Comments
 (0)