Skip to content

Commit 9d664dc

Browse files
committed
time-based order id
1 parent 90ec081 commit 9d664dc

File tree

6 files changed

+48
-16
lines changed

6 files changed

+48
-16
lines changed

ghcjs/delivery-calculator/delivery-calculator.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ common pkg
9797
, network-uri
9898
, regex-compat
9999
, syb
100+
, time
100101
, xlsx
101102

102103
if flag(ghcid)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module App.Types
77
St (..),
88
newSt,
99
Asset (..),
10+
newOrderId,
1011
newAsset,
1112
verifyAsset,
1213
newFieldPair,
@@ -40,6 +41,7 @@ import qualified Data.ByteString.Base64.URL as B64URL
4041
import qualified Data.ByteString.Lazy as BL
4142
import Data.Functor.Barbie
4243
import qualified Data.Generics as Syb
44+
import qualified Data.Time.Format as TF
4345
import qualified Data.Version as Version
4446
import Functora.Cfg
4547
import Functora.Miso.Prelude
@@ -94,6 +96,7 @@ data St f = St
9496
stMerchantFeePercent :: Field DynamicField f,
9597
stOnlineOrOffline :: OnlineOrOffline,
9698
stPreview :: Field Unicode f,
99+
stOrderId :: Field Unicode f,
97100
stScreen :: Screen,
98101
stEnableTheme :: Bool,
99102
stTheme :: Theme
@@ -123,6 +126,7 @@ newSt = do
123126
tele <- newTextField "Functora"
124127
fee <- newDynamicField $ DynamicFieldNumber 2
125128
pre <- newTextField "Delivery Calculator"
129+
oid <- newOrderId ct
126130
pure
127131
St
128132
{ stAssets = mempty,
@@ -134,6 +138,7 @@ newSt = do
134138
stMerchantFeePercent = fee & #fieldType .~ FieldTypePercent,
135139
stOnlineOrOffline = Online,
136140
stPreview = pre & #fieldType .~ FieldTypeTitle,
141+
stOrderId = oid,
137142
stScreen = Main,
138143
stEnableTheme = True,
139144
stTheme = Theme.Matcha
@@ -160,6 +165,12 @@ instance TraversableB Asset
160165

161166
deriving via GenericType (Asset Identity) instance Binary (Asset Identity)
162167

168+
newOrderId :: (MonadIO m) => UTCTime -> m (Field Unicode Unique)
169+
newOrderId ct =
170+
newTextField
171+
. from @String @Unicode
172+
$ TF.formatTime TF.defaultTimeLocale "%Y%m%d%H%M%S" ct
173+
163174
newAsset :: (MonadIO m) => m (Asset Unique)
164175
newAsset = do
165176
link <-

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ screenWidget st@Model {modelState = St {stScreen = Main}} =
155155
onClick . PushUpdate . Instant . EffectUpdate $ do
156156
let doc = st ^. #modelState
157157
imgs <- Jsm.fetchBlobUris doc
158-
Jsm.saveFile Xlsx.xlsxFile Xlsx.xlsxMime
158+
Jsm.saveFile (Xlsx.xlsxFile doc) Xlsx.xlsxMime
159159
. from @BL.ByteString @ByteString
160160
$ Xlsx.newXlsx doc imgs
161161
]

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,29 @@ menu st =
8484
Dialog.argsOptic = #modelMenu,
8585
Dialog.argsAction = PushUpdate . Instant,
8686
Dialog.argsContent =
87-
Currency.selectCurrency
88-
Currency.defOpts
89-
{ Currency.optsButtonLabel = Just "Marketplace currency",
90-
Currency.optsExtraOnClick = #modelLoading .~ True
91-
}
92-
Currency.Args
93-
{ Currency.argsModel = st,
94-
Currency.argsOptic = #modelState . #stAssetCurrency,
95-
Currency.argsAction = PushUpdate . Instant,
96-
Currency.argsEmitter = pushActionQueue st . Instant,
97-
Currency.argsCurrencies = #modelCurrencies
87+
Field.textField
88+
Field.Args
89+
{ Field.argsModel = st,
90+
Field.argsOptic = #modelState . #stOrderId,
91+
Field.argsAction = PushUpdate . Instant,
92+
Field.argsEmitter = pushActionQueue st . Instant
9893
}
94+
( Field.defOpts
95+
& #optsLabel
96+
.~ Just ("Order id" :: Unicode)
97+
)
98+
<> Currency.selectCurrency
99+
Currency.defOpts
100+
{ Currency.optsButtonLabel = Just "Marketplace currency",
101+
Currency.optsExtraOnClick = #modelLoading .~ True
102+
}
103+
Currency.Args
104+
{ Currency.argsModel = st,
105+
Currency.argsOptic = #modelState . #stAssetCurrency,
106+
Currency.argsAction = PushUpdate . Instant,
107+
Currency.argsEmitter = pushActionQueue st . Instant,
108+
Currency.argsCurrencies = #modelCurrencies
109+
}
99110
<> Currency.selectCurrency
100111
Currency.defOpts
101112
{ Currency.optsButtonLabel = Just "Merchant currency",

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,11 @@ newImg (RowIndex rowIdx) (ColumnIndex colIdx) imgIdx rfc2397 =
192192
fiContents = rfc2397Bytes rfc2397
193193
}
194194

195-
xlsxFile :: Unicode
196-
xlsxFile = "delivery-calculator.xlsx"
195+
xlsxFile :: St Unique -> Unicode
196+
xlsxFile st =
197+
"delivery-calculator-"
198+
<> (st ^. #stOrderId . #fieldOutput)
199+
<> ".xlsx"
197200

198201
xlsxMime :: Unicode
199202
xlsxMime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

ghcjs/delivery-calculator/src/Main.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,15 @@ syncInputs st = do
382382

383383
evalModel :: (MonadThrow m, MonadUnliftIO m) => Model -> m (Model -> Model)
384384
evalModel prev = do
385+
let oid = prev ^. #modelState . #stOrderId
385386
let oof = prev ^. #modelState . #stOnlineOrOffline
386387
let web = prev ^. #modelWebOpts
388+
updateOid <-
389+
if null $ oid ^. #fieldOutput
390+
then getCurrentTime >>= newOrderId >>= pure . (#modelState . #stOrderId .~)
391+
else pure id
387392
case oof of
388-
Offline -> pure id
393+
Offline -> pure updateOid
389394
Online ->
390395
Rates.withMarket web (prev ^. #modelMarket) $ do
391396
let prevCurs :: [Money.CurrencyInfo] =
@@ -443,7 +448,8 @@ evalModel prev = do
443448
let rateUpdated =
444449
nextQuote ^. #quoteCreatedAt
445450
pure
446-
$ ( #modelState
451+
$ updateOid
452+
. ( #modelState
447453
%~ Syb.everywhere
448454
( Syb.mkT $ \cur ->
449455
fromMaybe cur

0 commit comments

Comments
 (0)