Skip to content

Commit

Permalink
Use WalletId everywhere possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jhbertra committed Sep 17, 2021
1 parent b39718f commit d6a764b
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 160 deletions.
24 changes: 10 additions & 14 deletions marlowe-dashboard-client/src/Dashboard/View.purs
Expand Up @@ -4,6 +4,7 @@ module Dashboard.View
) where

import Prelude hiding (div)
import Clipboard (Action(..)) as Clipboard
import Contract.Lenses (_Started, _stateNickname)
import Contract.State (isContractClosed)
import Contract.Types (State) as Contract
Expand Down Expand Up @@ -42,6 +43,7 @@ import WalletData.Lenses (_assets, _companionAppId, _walletNickname, _walletLibr
import WalletData.State (adaToken, getAda)
import WalletData.Types (WalletDetails)
import WalletData.View (walletDataCard)
import Web.Common.Components.WalletId as WalletId

dashboardScreen :: forall m. MonadAff m => Input -> State -> ComponentHTML Action ChildSlots m
dashboardScreen { currentSlot, tzOffset } state =
Expand Down Expand Up @@ -430,24 +432,18 @@ currentWalletCard walletDetails =
companionAppId = view _companionAppId walletDetails

assets = view _assets walletDetails

copyWalletId = (ClipboardAction <<< Clipboard.CopyToClipboard <<< UUID.toString <<< unwrap)
in
div [ classNames [ "h-full", "grid", "grid-rows-auto-1fr-auto", "divide-y", "divide-gray" ] ]
[ h2
[ classNames Css.cardHeader ]
[ text $ "Wallet " <> walletNickname ]
, div [ classNames [ "p-4", "overflow-y-auto", "space-y-4" ] ]
[ div
[ classNames Css.hasNestedLabel ]
[ label
[ classNames Css.nestedLabel ]
[ text "Wallet ID" ]
, input
[ type_ InputText
, classNames $ Css.input true
, value $ UUID.toString $ unwrap companionAppId
, readOnly true
]
]
[ text "My wallet" ]
, div [ classNames [ "p-4", "overflow-y-auto", "overflow-x-hidden", "space-y-4" ] ]
[ h3
[ classNames [ "font-semibold", "text-lg" ] ]
[ text walletNickname ]
, copyWalletId <$> WalletId.render WalletId.defaultParams { label = "Wallet ID", value = companionAppId }
, div_
[ h4
[ classNames [ "font-semibold" ] ]
Expand Down
23 changes: 15 additions & 8 deletions marlowe-dashboard-client/src/Template/View.purs
Expand Up @@ -6,6 +6,7 @@ import Data.Lens (view)
import Data.List (toUnfoldable) as List
import Data.Map (Map)
import Data.Map as Map
import Data.Map.Ordered.OMap as OMap
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Tuple (Tuple)
import Data.Tuple.Nested ((/\))
Expand All @@ -17,11 +18,11 @@ import Halogen.HTML.Properties (enabled, for, id_)
import Hint.State (hint)
import Humanize (contractIcon, humanizeValue)
import InputField.Lenses (_value)
import InputField.Types (InputDisplayOptions)
import InputField.Types (State) as InputField
import InputField.View (renderInput)
import MainFrame.Types (ChildSlots)
import Marlowe.Extended.Metadata (ContractTemplate, MetaData, NumberFormat(..), _contractName, _metaData, _slotParameterDescriptions, _valueParameterDescription, _valueParameterFormat, _valueParameterInfo)
import Data.Map.Ordered.OMap as OMap
import Marlowe.Market (contractTemplates)
import Marlowe.PAB (contractCreationFee)
import Marlowe.Semantics (Assets, TokenName)
Expand All @@ -35,6 +36,7 @@ import Template.Types (Action(..), ContractSetupStage(..), RoleError, SlotError,
import Text.Markdown.TrimmedInline (markdownToHTML)
import Tooltip.State (tooltip)
import Tooltip.Types (ReferenceId(..))
import Web.Common.Components.Label as Label
import WalletData.Lenses (_walletNickname)
import WalletData.State (adaToken, getAda)
import WalletData.Types (WalletLibrary)
Expand Down Expand Up @@ -184,6 +186,12 @@ contractSetup walletLibrary state =
, readOnly: false
, numberFormat: Nothing
, valueOptions: mempty
, after: Nothing
, before:
Just
$ Label.render
Label.defaultParams
{ for = "contractNickname", text = contractName <> " title" }
}
in
div
Expand All @@ -193,13 +201,7 @@ contractSetup walletLibrary state =
[ h2
[ classNames [ "text-lg", "font-semibold", "mb-2" ] ]
[ text $ contractName <> " setup" ]
, div
[ classNames Css.hasNestedLabel ]
[ label
[ classNames Css.nestedLabel ]
[ text $ contractName <> " title" ]
, ContractNicknameInputAction <$> renderInput contractNicknameInputDisplayOptions contractNicknameInput
]
, ContractNicknameInputAction <$> renderInput contractNicknameInputDisplayOptions contractNicknameInput
, roleInputs walletLibrary metaData roleWalletInputs
, parameterInputs metaData slotContentInputs valueContentInputs
]
Expand Down Expand Up @@ -365,6 +367,8 @@ roleInputs walletLibrary metaData roleWalletInputs =
, readOnly: false
, numberFormat: Nothing
, valueOptions: List.toUnfoldable $ Map.values $ view _walletNickname <$> walletLibrary
, after: Nothing
, before: Nothing
}

parameterInputs :: forall m. MonadAff m => MetaData -> Map String (InputField.State SlotError) -> Map String (InputField.State ValueError) -> ComponentHTML Action ChildSlots m
Expand Down Expand Up @@ -403,13 +407,16 @@ parameterInputs metaData slotContentInputs valueContentInputs =
templateInputItem key description
[ SlotContentInputAction key <$> renderInput (inputFieldOptions key true numberFormat) inputField ]

inputFieldOptions :: forall w i. String -> Boolean -> NumberFormat -> InputDisplayOptions w i
inputFieldOptions key readOnly numberFormat =
{ additionalCss: mempty
, id_: key
, placeholder: key
, readOnly
, numberFormat: Just numberFormat
, valueOptions: mempty
, after: Nothing
, before: Nothing
}

templateInputsSection :: forall p. Icon -> String -> Array (HTML p Action) -> HTML p Action
Expand Down
62 changes: 26 additions & 36 deletions marlowe-dashboard-client/src/WalletData/View.purs
Expand Up @@ -13,9 +13,9 @@ import Data.Newtype (unwrap)
import Data.Tuple.Nested ((/\))
import Data.UUID (toString) as UUID
import Halogen.Css (classNames)
import Halogen.HTML (HTML, a, button, div, div_, h2, h3, h4, label, li, p, span, text, ul)
import Halogen.HTML (HTML, a, button, div, div_, h2, h3, li, p, span, text, ul)
import Halogen.HTML.Events.Extra (onClick_)
import Halogen.HTML.Properties (disabled, for)
import Halogen.HTML.Properties (disabled)
import InputField.Lenses (_value)
import InputField.State (validate)
import InputField.Types (State) as InputField
Expand All @@ -24,6 +24,8 @@ import Material.Icons (Icon(..)) as Icon
import Material.Icons (icon_)
import WalletData.Lenses (_cardSection, _companionAppId, _walletIdInput, _walletLibrary, _walletNickname, _walletNicknameInput)
import WalletData.Types (Action(..), CardSection(..), State, WalletDetails, WalletIdError, WalletLibrary, WalletNicknameError)
import Web.Common.Components.Label as Label
import Web.Common.Components.WalletId as WalletId

walletDataCard :: forall p. WalletDetails -> State -> HTML p Action
walletDataCard currentWallet state =
Expand Down Expand Up @@ -113,27 +115,19 @@ walletDetailsCard currentWallet walletDetails =
companionAppIdString = UUID.toString $ unwrap companionAppId

isCurrentWallet = walletNickname == currentWallet ^. _walletNickname

copyWalletId = (ClipboardAction <<< Clipboard.CopyToClipboard <<< UUID.toString <<< unwrap)
in
[ div [ classNames [ "space-y-4", "p-4" ] ]
[ h3
[ classNames [ "text-lg", "font-semibold" ] ]
[ text walletNickname ]
, h4
[ classNames [ "text-sm", "font-semibold" ] ]
[ text "Demo wallet key" ]
, div
[ classNames [ "flex", "items-center", "justify-between" ] ]
[ span
[ classNames [ "font-mono", "text-xs", "whitespace-nowrap" ] ]
[ text companionAppIdString ]
, span
-- I don't understand why I have to set the width of this - for some reason it
-- grows very wide if I don't :/
[ classNames [ "cursor-pointer", "w-6" ]
, onClick_ $ ClipboardAction $ Clipboard.CopyToClipboard companionAppIdString
]
[ icon_ Icon.Copy ]
]
, copyWalletId
<$> WalletId.render
WalletId.defaultParams
{ label = "Demo wallet key"
, value = companionAppId
}
, walletIdTip
]
, div
Expand Down Expand Up @@ -170,6 +164,12 @@ newWalletCard walletNicknameInput walletIdInput mTokenName =
, readOnly: false
, numberFormat: Nothing
, valueOptions: mempty
, after: Nothing
, before:
Just
$ Label.render
Label.defaultParams
{ for = "newWalletNickname", text = "Wallet nickname" }
}

walletIdInputDisplayOptions =
Expand All @@ -179,27 +179,17 @@ newWalletCard walletNicknameInput walletIdInput mTokenName =
, readOnly: false
, numberFormat: Nothing
, valueOptions: mempty
, after: Nothing
, before:
Just
$ Label.render
Label.defaultParams
{ for = "newWalletId", text = "Wallet nickname" }
}
in
[ div [ classNames [ "space-y-4", "p-4" ] ]
[ div
[ classNames $ Css.hasNestedLabel ]
[ label
[ classNames Css.nestedLabel
, for walletNicknameInputDisplayOptions.id_
]
[ text "Wallet nickname" ]
, WalletNicknameInputAction <$> renderInput walletNicknameInputDisplayOptions walletNicknameInput
]
, div
[ classNames $ Css.hasNestedLabel ]
[ label
[ classNames Css.nestedLabel
, for walletIdInputDisplayOptions.id_
]
[ text "Demo wallet ID" ]
, WalletIdInputAction <$> renderInput walletIdInputDisplayOptions walletIdInput
]
[ WalletNicknameInputAction <$> renderInput walletNicknameInputDisplayOptions walletNicknameInput
, WalletIdInputAction <$> renderInput walletIdInputDisplayOptions walletIdInput
]
, div
[ classNames [ "flex", "gap-4", "p-4" ] ]
Expand Down
9 changes: 5 additions & 4 deletions marlowe-dashboard-client/src/Welcome/Lenses.purs
Expand Up @@ -4,7 +4,7 @@ module Welcome.Lenses
, _walletLibrary
, _walletNicknameOrIdInput
, _walletNicknameInput
, _walletIdInput
, _walletId
, _remoteWalletDetails
, _enteringDashboardState
) where
Expand All @@ -14,8 +14,9 @@ import Data.Lens.Record (prop)
import Data.Maybe (Maybe)
import Data.Symbol (SProxy(..))
import InputField.Types (State) as InputField
import Marlowe.PAB (PlutusAppId)
import Types (WebData)
import WalletData.Types (WalletDetails, WalletIdError, WalletLibrary, WalletNicknameError)
import WalletData.Types (WalletDetails, WalletLibrary, WalletNicknameError)
import Welcome.Types (Card, State, WalletNicknameOrIdError)

_card :: Lens' State (Maybe Card)
Expand All @@ -33,8 +34,8 @@ _walletNicknameOrIdInput = prop (SProxy :: SProxy "walletNicknameOrIdInput")
_walletNicknameInput :: Lens' State (InputField.State WalletNicknameError)
_walletNicknameInput = prop (SProxy :: SProxy "walletNicknameInput")

_walletIdInput :: Lens' State (InputField.State WalletIdError)
_walletIdInput = prop (SProxy :: SProxy "walletIdInput")
_walletId :: Lens' State PlutusAppId
_walletId = prop (SProxy :: SProxy "walletId")

_remoteWalletDetails :: Lens' State (WebData WalletDetails)
_remoteWalletDetails = prop (SProxy :: SProxy "remoteWalletDetails")
Expand Down
29 changes: 10 additions & 19 deletions marlowe-dashboard-client/src/Welcome/State.purs
Expand Up @@ -14,11 +14,11 @@ import Clipboard (handleAction) as Clipboard
import Control.Monad.Reader (class MonadAsk)
import Data.Either (Either(..))
import Data.Foldable (for_)
import Data.Lens (assign, modifying, set, use, view)
import Data.Lens (assign, modifying, set, use, view, (^.))
import Data.Map (filter, findMin, insert, lookup)
import Data.Maybe (Maybe(..))
import Data.Newtype (unwrap)
import Data.UUID (toString) as UUID
import Data.UUID (emptyUUID, toString) as UUID
import Effect.Aff.Class (class MonadAff)
import Env (Env)
import Halogen (HalogenM, liftEffect, modify_)
Expand All @@ -28,6 +28,7 @@ import InputField.State (handleAction, mkInitialState) as InputField
import InputField.Types (Action(..), State) as InputField
import MainFrame.Types (Action(..)) as MainFrame
import MainFrame.Types (ChildSlots, Msg)
import Marlowe.PAB (PlutusAppId(..))
import Network.RemoteData (RemoteData(..), fromEither)
import Toast.Types (ajaxErrorToast, errorToast, successToast)
import Types (WebData)
Expand All @@ -37,7 +38,7 @@ import WalletData.Types (WalletDetails, WalletIdError, WalletLibrary, WalletNick
import Web.HTML (window)
import Web.HTML.Location (reload)
import Web.HTML.Window (location)
import Welcome.Lenses (_card, _cardOpen, _enteringDashboardState, _remoteWalletDetails, _walletIdInput, _walletLibrary, _walletNicknameInput, _walletNicknameOrIdInput)
import Welcome.Lenses (_card, _cardOpen, _enteringDashboardState, _remoteWalletDetails, _walletId, _walletLibrary, _walletNicknameInput, _walletNicknameOrIdInput)
import Welcome.Types (Action(..), Card(..), State, WalletNicknameOrIdError(..))

-- see note [dummyState] in MainFrame.State
Expand All @@ -51,7 +52,7 @@ mkInitialState walletLibrary =
, cardOpen: false
, walletNicknameOrIdInput: InputField.mkInitialState Nothing
, walletNicknameInput: InputField.mkInitialState Nothing
, walletIdInput: InputField.mkInitialState Nothing
, walletId: PlutusAppId UUID.emptyUUID
, remoteWalletDetails: NotAsked
, enteringDashboardState: false
}
Expand All @@ -78,9 +79,9 @@ handleAction CloseCard = do
$ set _remoteWalletDetails NotAsked
<<< set _enteringDashboardState false
<<< set _cardOpen false
<<< set _walletId dummyState.walletId
handleAction $ WalletNicknameOrIdInputAction $ InputField.Reset
handleAction $ WalletNicknameInputAction $ InputField.Reset
handleAction $ WalletIdInputAction $ InputField.Reset

handleAction GenerateWallet = do
walletLibrary <- use _walletLibrary
Expand All @@ -92,7 +93,7 @@ handleAction GenerateWallet = do
Right walletDetails -> do
handleAction $ WalletNicknameInputAction $ InputField.Reset
handleAction $ WalletNicknameInputAction $ InputField.SetValidator $ walletNicknameError walletLibrary
handleAction $ WalletIdInputAction $ InputField.SetValue $ UUID.toString (unwrap (view _companionAppId walletDetails))
assign _walletId $ walletDetails ^. _companionAppId
handleAction $ OpenCard UseNewWalletCard

handleAction (WalletNicknameOrIdInputAction inputFieldAction) = do
Expand All @@ -112,17 +113,16 @@ handleAction (WalletNicknameOrIdInputAction inputFieldAction) = do
Right walletDetails -> do
-- check whether this wallet ID is already in the walletLibrary ...
walletLibrary <- use _walletLibrary
case findMin $ filter (\details -> UUID.toString (unwrap (view _companionAppId details)) == walletNicknameOrId) walletLibrary of
assign _walletId plutusAppId
case findMin $ filter (\w -> w ^. _companionAppId == plutusAppId) walletLibrary of
Just { key, value } -> do
-- if so, open the UseWalletCard
handleAction $ WalletNicknameInputAction $ InputField.SetValue key
handleAction $ WalletIdInputAction $ InputField.SetValue walletNicknameOrId
handleAction $ OpenCard UseWalletCard
Nothing -> do
-- otherwise open the UseNewWalletCard
handleAction $ WalletNicknameInputAction $ InputField.Reset
handleAction $ WalletNicknameInputAction $ InputField.SetValidator $ walletNicknameError walletLibrary
handleAction $ WalletIdInputAction $ InputField.SetValue $ UUID.toString (unwrap (view _companionAppId walletDetails))
handleAction $ OpenCard UseNewWalletCard
InputField.SetValueFromDropdown walletNicknameOrId -> do
-- in this case we know it's a wallet nickname, and we want to open the use card
Expand All @@ -140,13 +140,11 @@ handleAction (OpenUseWalletCardWithDetails walletDetails) = do
Right _ -> do
handleAction $ WalletNicknameOrIdInputAction $ InputField.Reset
handleAction $ WalletNicknameInputAction $ InputField.SetValue $ view _walletNickname walletDetails
handleAction $ WalletIdInputAction $ InputField.SetValue $ UUID.toString (unwrap (view _companionAppId walletDetails))
assign _walletId $ walletDetails ^. _companionAppId
handleAction $ OpenCard UseWalletCard

handleAction (WalletNicknameInputAction inputFieldAction) = toWalletNicknameInput $ InputField.handleAction inputFieldAction

handleAction (WalletIdInputAction inputFieldAction) = toWalletIdInput $ InputField.handleAction inputFieldAction

handleAction (UseWallet walletNickname) = do
assign _enteringDashboardState true
remoteWalletDetails <- use _remoteWalletDetails
Expand Down Expand Up @@ -189,13 +187,6 @@ toWalletNicknameInput ::
HalogenM State Action slots msg m Unit
toWalletNicknameInput = mapSubmodule _walletNicknameInput WalletNicknameInputAction

toWalletIdInput ::
forall m msg slots.
Functor m =>
HalogenM (InputField.State WalletIdError) (InputField.Action WalletIdError) slots msg m Unit ->
HalogenM State Action slots msg m Unit
toWalletIdInput = mapSubmodule _walletIdInput WalletIdInputAction

------------------------------------------------------------
walletNicknameOrIdError :: WebData WalletDetails -> String -> Maybe WalletNicknameOrIdError
walletNicknameOrIdError remoteWalletDetails _ = case remoteWalletDetails of
Expand Down

0 comments on commit d6a764b

Please sign in to comment.