Skip to content

Commit

Permalink
Extract addresses UI related code in separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Sep 25, 2024
1 parent 4ca589b commit c7a04f7
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 59 deletions.
2 changes: 2 additions & 0 deletions lib/ui/cardano-wallet-ui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ library
Cardano.Wallet.UI.Common.Layer
Cardano.Wallet.UI.Cookies
Cardano.Wallet.UI.Deposit.API
Cardano.Wallet.UI.Deposit.Handlers.Addresses
Cardano.Wallet.UI.Deposit.Handlers.Lib
Cardano.Wallet.UI.Deposit.Handlers.Wallet
Cardano.Wallet.UI.Deposit.Html.Pages.About
Cardano.Wallet.UI.Deposit.Html.Pages.Addresses
Cardano.Wallet.UI.Deposit.Html.Pages.Page
Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
Cardano.Wallet.UI.Deposit.Server
Expand Down
6 changes: 6 additions & 0 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Pages =
:<|> "network" :> SessionedHtml Get
:<|> "settings" :> SessionedHtml Get
:<|> "wallet" :> SessionedHtml Get
:<|> "addresses" :> SessionedHtml Get

-- | Data endpoints
type Data =
Expand All @@ -91,6 +92,7 @@ type Data =
:<|> "wallet" :> "delete" :> "modal" :> SessionedHtml Get
:<|> "customer" :> "address" :> ReqBody '[FormUrlEncoded] Customer
:> SessionedHtml Post
:<|> "addresses" :> SessionedHtml Get

instance FromForm Customer where
fromForm form = fromIntegral @Int <$> parseUnique "customer" form
Expand All @@ -109,6 +111,7 @@ homePageLink :: Link
aboutPageLink :: Link
networkPageLink :: Link
settingsPageLink :: Link
addressesPageLink :: Link
networkInfoLink :: Link
settingsGetLink :: Link
settingsSseToggleLink :: Link
Expand All @@ -122,11 +125,13 @@ walletPostXPubLink :: Link
walletDeleteLink :: Link
walletDeleteModalLink :: Link
customerAddressLink :: Link
addressesLink :: Link
homePageLink
:<|> aboutPageLink
:<|> networkPageLink
:<|> settingsPageLink
:<|> walletPageLink
:<|> addressesPageLink
:<|> networkInfoLink
:<|> settingsGetLink
:<|> settingsSseToggleLink
Expand All @@ -139,5 +144,6 @@ homePageLink
:<|> walletDeleteLink
:<|> walletDeleteModalLink
:<|> customerAddressLink
:<|> addressesLink
=
allLinks (Proxy @UI)
52 changes: 52 additions & 0 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Handlers/Addresses.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{-# LANGUAGE LambdaCase #-}

module Cardano.Wallet.UI.Deposit.Handlers.Addresses
where

import Prelude

import Cardano.Wallet.Deposit.Pure
( Customer
)
import Cardano.Wallet.Deposit.Read
( Address
)
import Cardano.Wallet.Deposit.REST
( WalletResource
, customerAddress
)
import Cardano.Wallet.UI.Common.Layer
( SessionLayer (..)
)
import Cardano.Wallet.UI.Deposit.Handlers.Lib
( catchRunWalletResourceHtml
, walletPresence
)
import Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
( WalletPresent
)
import Servant
( Handler
)

import qualified Data.ByteString.Lazy.Char8 as BL

getAddresses
:: SessionLayer WalletResource
-> (WalletPresent -> html) -- success report
-> Handler html
getAddresses layer render = render <$> walletPresence layer

getCustomerAddress
:: SessionLayer WalletResource
-> (Address -> html)
-> (BL.ByteString -> html)
-> Customer
-> Handler html
getCustomerAddress layer render alert customer = do
catchRunWalletResourceHtml layer alert render'
$ customerAddress customer
where
render' = \case
Just a -> render a
Nothing -> alert "Address not discovered"
19 changes: 0 additions & 19 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Handlers/Wallet.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}

module Cardano.Wallet.UI.Deposit.Handlers.Wallet
Expand All @@ -12,13 +11,9 @@ import Cardano.Address.Derivation
import Cardano.Wallet.Deposit.Pure
( Customer
)
import Cardano.Wallet.Deposit.Read
( Address
)
import Cardano.Wallet.Deposit.REST
( WalletResource
, WalletResourceM
, customerAddress
)
import Cardano.Wallet.Deposit.REST.Wallet.Create
( PostWalletViaMenmonic (..)
Expand Down Expand Up @@ -122,17 +117,3 @@ deleteWalletHandler
-> Handler html
deleteWalletHandler layer deleteWallet alert render =
catchRunWalletResourceHtml layer alert render deleteWallet

getCustomerAddress
:: SessionLayer WalletResource
-> (Address -> html)
-> (BL.ByteString -> html)
-> Customer
-> Handler html
getCustomerAddress layer render alert customer = do
catchRunWalletResourceHtml layer alert render'
$ customerAddress customer
where
render' = \case
Just a -> render a
Nothing -> alert "Address not discovered"
115 changes: 115 additions & 0 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Html/Pages/Addresses.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.Wallet.UI.Deposit.Html.Pages.Addresses
where

import Prelude

import Cardano.Wallet.Deposit.IO
( WalletPublicIdentity (..)
)
import Cardano.Wallet.Deposit.Read
( Address
)
import Cardano.Wallet.UI.Common.Html.Copy
( copyButton
, copyableHidden
)
import Cardano.Wallet.UI.Common.Html.Htmx
( hxPost_
, hxTarget_
, hxTrigger_
)
import Cardano.Wallet.UI.Common.Html.Lib
( linkText
)
import Cardano.Wallet.UI.Common.Html.Pages.Lib
( record
, simpleField
, sseH
)
import Cardano.Wallet.UI.Deposit.API
( addressesLink
, customerAddressLink
)
import Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
( WalletPresent (..)
)
import Cardano.Wallet.UI.Lib.Address
( encodeMainnetAddress
)
import Cardano.Wallet.UI.Type
( WHtml
)
import Data.Text.Class
( ToText (..)
)
import Lucid
( Html
, HtmlT
, ToHtml (..)
, class_
, div_
, h5_
, id_
, input_
, min_
, name_
, type_
, value_
)
import Lucid.Html5
( max_
, step_
)

import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.Text as T

addressesH :: WHtml ()
addressesH = do
sseH addressesLink "addresses" ["wallet"]

customerAddressH :: Monad m => Address -> HtmlT m ()
customerAddressH addr = div_ [class_ "d-flex justify-content-end"] $ do
div_ (copyableHidden "address") $ toHtml encodedAddr
div_ [class_ ""] $ toHtml addrShortened
div_ [class_ "ms-1"] $ copyButton "address"
where
encodedAddr = encodeMainnetAddress addr
addrShortened =
T.take 10 (T.drop 5 encodedAddr)
<> " .. "
<> T.takeEnd 10 encodedAddr

addressElementH :: (BL.ByteString -> Html ()) -> WalletPresent -> Html ()
addressElementH alert = \case
WalletPresent (WalletPublicIdentity _xpub customers) -> do
div_ [class_ "row mt-5"] $ do
h5_ [class_ "text-center"] "Addresses"
div_ [class_ "col"] $ record $ do
simpleField "Customer Number"
$ input_
[ type_ "number"
, hxTarget_ "#customer-address"
, class_ "form-control"
, hxTrigger_ "load, change"
, hxPost_ $ linkText customerAddressLink
, min_ "0"
, max_ $ toText $ customers - 1
, step_ "1"
, name_ "customer"
, value_ "0"
, class_ "w-3"
]
simpleField "Address" $ div_ [id_ "customer-address"] mempty
WalletAbsent -> alert "Wallet is absent"
WalletFailedToInitialize err ->
alert
$ "Failed to initialize wallet"
<> BL.pack (show err)
WalletVanished e -> alert $ "Wallet vanished " <> BL.pack (show e)
WalletInitializing -> alert "Wallet is initializing"
WalletClosing -> alert "Wallet is closing"
28 changes: 21 additions & 7 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Html/Pages/Page.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Cardano.Wallet.UI.Common.Html.Pages.Template.Navigation
)
import Cardano.Wallet.UI.Deposit.API
( aboutPageLink
, addressesPageLink
, faviconLink
, networkInfoLink
, networkPageLink
Expand All @@ -47,8 +48,13 @@ import Cardano.Wallet.UI.Deposit.API
import Cardano.Wallet.UI.Deposit.Html.Pages.About
( aboutH
)
import Cardano.Wallet.UI.Deposit.Html.Pages.Addresses
( addressesH
)
import Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
( walletH
( WalletPresent
, isPresent
, walletH
)
import Cardano.Wallet.UI.Type
( WalletType (..)
Expand All @@ -73,6 +79,7 @@ data Page
| Network
| Settings
| Wallet
| Addresses

makePrisms ''Page

Expand All @@ -81,27 +88,34 @@ page
-- ^ Page configuration
-> Page
-- ^ Current page
-> WalletPresent
-- ^ Wallet present
-> RawHtml
page c@PageConfig{..} p = RawHtml
page c@PageConfig{..} p wp = RawHtml
$ renderBS
$ runWHtml Deposit
$ pageFromBodyH faviconLink c
$ do
bodyH sseLink (headerH prefix p)
bodyH sseLink (headerH prefix p wp)
$ do
modalsH
case p of
About -> aboutH
Network -> networkH networkInfoLink
Settings -> settingsPageH settingsGetLink
Wallet -> walletH
Addresses -> addressesH

headerH :: Text -> Page -> Monad m => HtmlT m ()
headerH prefix p =
headerH :: Text -> Page -> WalletPresent -> Monad m => HtmlT m ()
headerH prefix p wp =
navigationH
prefix
prefix $
[ (is _Wallet p, walletPageLink, "Wallet")
, (is _Network p, networkPageLink, "Network")
]
<>
[(is _Addresses p, addressesPageLink, "Addresses") | isPresent wp]
<>
[ (is _Network p, networkPageLink, "Network")
, (is _Settings p, settingsPageLink, "Settings")
, (is _About p, aboutPageLink, "About")
]
Loading

0 comments on commit c7a04f7

Please sign in to comment.