Skip to content

Commit 3a8e156

Browse files
committed
showing time in header
1 parent b569cf8 commit 3a8e156

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ data Model = Model
7474
modelDonateViewer :: [FieldPair DynamicField Unique],
7575
modelCurrencies :: NonEmpty CurrencyInfo,
7676
modelWebOpts :: Web.Opts,
77-
modelMarket :: MVar Rates.Market
77+
modelMarket :: MVar Rates.Market,
78+
modelTime :: UTCTime
7879
}
7980
deriving stock (Eq, Generic)
8081

8182
data Action
8283
= Noop
84+
| Tick (Model -> Model)
8385
| InitUpdate (Maybe (St Unique))
8486
| SyncInputs
8587
| LinkUpdate (Model -> Model)
@@ -90,7 +92,6 @@ data St f = St
9092
{ stAssets :: [Asset f],
9193
stAssetCurrency :: Currency f,
9294
stExchangeRate :: Field Rational f,
93-
stExchangeRateAt :: UTCTime,
9495
stMerchantCurrency :: Currency f,
9596
stMerchantTele :: Field Unicode f,
9697
stMerchantWhats :: Field Unicode f,
@@ -121,7 +122,6 @@ newSt :: (MonadIO m) => m (St Unique)
121122
newSt = do
122123
assetCur <- newCurrency cny
123124
rate <- newRatioField 1
124-
ct <- getCurrentTime
125125
merchantCur <- newCurrency rub
126126
tele <- newTextField "Functora"
127127
whats <- newTextField "TODO"
@@ -132,7 +132,6 @@ newSt = do
132132
{ stAssets = mempty,
133133
stAssetCurrency = assetCur,
134134
stExchangeRate = rate,
135-
stExchangeRateAt = ct,
136135
stMerchantCurrency = merchantCur,
137136
stMerchantTele = tele,
138137
stMerchantWhats = whats,

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module App.Widgets.Menu
66
where
77

88
import App.Types
9+
import qualified Data.Time.Format as TF
10+
import qualified Data.Time.LocalTime as LT
911
import qualified Functora.Miso.Jsm as Jsm
1012
import Functora.Miso.Prelude
1113
import qualified Functora.Miso.Widgets.BrowserLink as BrowserLink
@@ -22,11 +24,10 @@ menu st =
2224
[ keyed "menu"
2325
$ nav_
2426
[ style_
25-
[ ("grid-template-columns", "1fr auto")
27+
[ ("grid-template-columns", "auto 1fr")
2628
]
2729
]
28-
[ div_ mempty mempty,
29-
button_
30+
[ button_
3031
[ role_ "button",
3132
style_
3233
[ ("min-width", "0")
@@ -38,6 +39,13 @@ menu st =
3839
.~ Opened
3940
]
4041
[ icon Icon.IconMenu
42+
],
43+
label_
44+
mempty
45+
[ text
46+
. from @String @Unicode
47+
$ TF.formatTime TF.defaultTimeLocale "%H:%M" chinaTime
48+
<> " in China"
4149
]
4250
]
4351
]
@@ -203,6 +211,10 @@ menu st =
203211
]
204212
}
205213
where
214+
chinaTime =
215+
LT.utcToLocalTime chinaTimeZone $ modelTime st
216+
chinaTimeZone =
217+
LT.hoursToTimeZone 8
206218
shareOnClick :: Attribute Action
207219
shareOnClick =
208220
onClick

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ newModel webOpts sink mSt uri = do
2222
mApp <- unShareUri uri
2323
donate <- newDonateViewer
2424
market <- maybe Rates.newMarket pure $ mSt ^? _Just . #modelMarket
25+
ct <- getCurrentTime
2526
pure
2627
Model
2728
{ modelSink = sink,
@@ -37,7 +38,8 @@ newModel webOpts sink mSt uri = do
3738
modelCurrencies =
3839
fromMaybe [btc, usd, rub, cny] (mSt ^? _Just . #modelCurrencies),
3940
modelWebOpts = webOpts,
40-
modelMarket = market
41+
modelMarket = market,
42+
modelTime = ct
4143
}
4244

4345
newDonateViewer :: (MonadIO m) => m [FieldPair DynamicField Unique]

ghcjs/delivery-calculator/src/Main.hs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,14 @@ runApp = JSaddle.Wasm.run
120120

121121
updateModel :: Action -> Model -> Effect Action Model
122122
updateModel Noop st = noEff st
123+
updateModel (Tick f) st =
124+
f st <# do
125+
sleepSeconds 1
126+
ct <- getCurrentTime
127+
pure . Tick $ #modelTime .~ ct
123128
updateModel (InitUpdate ext) prevSt = do
124129
effectSub prevSt $ \sink -> do
130+
liftIO . sink $ Tick id
125131
mvSink <- newMVar sink
126132
let nextSt = prevSt {modelSink = mvSink}
127133
if isJust ext
@@ -383,28 +389,27 @@ evalModel prev = do
383389
. #stAssetCurrency
384390
. #currencyOutput
385391
. #currencyInfoCode
386-
let prevQuote =
387-
Rates.QuoteAt
388-
{ Rates.quoteMoneyAmount =
389-
Tagged $ prev ^. #modelState . #stExchangeRate . #fieldOutput,
390-
Rates.quoteCreatedAt =
391-
prev ^. #modelState . #stExchangeRateAt,
392-
Rates.quoteUpdatedAt =
393-
prev ^. #modelState . #stExchangeRateAt
394-
}
395-
nextQuote <-
396-
fmap (fromRight prevQuote)
392+
rateValue <-
393+
fmap
394+
( either
395+
( const
396+
$ prev
397+
^. #modelState
398+
. #stExchangeRate
399+
. #fieldOutput
400+
)
401+
( ^.
402+
#quoteMoneyAmount
403+
. to unTagged
404+
)
405+
)
397406
. Rates.tryMarket
398407
. Rates.getQuote web base
399408
$ prev
400409
^. #modelState
401410
. #stMerchantCurrency
402411
. #currencyOutput
403412
. #currencyInfoCode
404-
let rateValue =
405-
unTagged $ nextQuote ^. #quoteMoneyAmount
406-
let rateUpdated =
407-
nextQuote ^. #quoteCreatedAt
408413
pure
409414
$ ( #modelState
410415
%~ Syb.everywhere
@@ -427,10 +432,6 @@ evalModel prev = do
427432
. #fieldOutput
428433
.~ rateValue
429434
)
430-
. ( #modelState
431-
. #stExchangeRateAt
432-
.~ rateUpdated
433-
)
434435

435436
syncUri :: URI -> JSM ()
436437
syncUri uri = do

0 commit comments

Comments
 (0)