~sjm/builds-character

ada0f5c63c1813e4d1136a61109b15424c9b8d18 — Sam Marshall 3 years ago 073cb3c
chore: update class dropdown
2 files changed, 43 insertions(+), 9 deletions(-)

M src/UI/Class.purs
M src/UI/Entry.purs
M src/UI/Class.purs => src/UI/Class.purs +34 -6
@@ 6,15 6,22 @@ import CharSheet.Class as CC
import Data.Maybe (Maybe(..))
import Halogen as H
import Halogen.HTML as HH
import Halogen.HTML.Events as HE
import Halogen.HTML.Properties as HP
import UI.Util as Util

type Input = CC.Class
type State = CC.Class

data Output = ClassChosen CC.Class

data Action = Update Input
            | ChooseClass CC.Class
            | NoOp

type Slot s = forall q o. H.Slot q o s
type Slot s = forall q. H.Slot q Output s

component :: forall q o m. H.Component q Input o m
component :: forall q m. H.Component q Input Output m
component = H.mkComponent { initialState
                          , render
                          , eval : H.mkEval $ H.defaultEval { handleAction = handleAction


@@ 22,13 29,34 @@ component = H.mkComponent { initialState
                                                            }
                          }

classOption :: forall w i. CC.Class -> HH.HTML w i
classOption c = HH.option [ HP.value $ CC.toString c ] [ HH.text $ CC.toString c ]

initialState :: Input -> State
initialState = identity

render :: forall a m. State -> H.ComponentHTML a () m
render :: forall m. State -> H.ComponentHTML Action () m
render s =
  HH.div_ [ HH.text $ CC.toString s ]

handleAction :: forall o m. Action -> H.HalogenM State Action () o m Unit
  HH.div
  [ Util.classes [ "mb-2" ] ]
  [ HH.select
    [ HP.value $ CC.toString s
    , HE.onValueChange (\str -> case CC.fromString str of
                           Just c -> ChooseClass c
                           Nothing -> NoOp)
    ]
    [ classOption CC.Expert
    , classOption CC.Warrior
    , classOption CC.Psychic
    , classOption CC.ExpertWarrior
    , classOption CC.ExpertPsychic
    , classOption CC.PsychicWarrior
    , classOption CC.Undecided
    ]
  ]

handleAction :: forall m. Action -> H.HalogenM State Action () Output  m Unit
handleAction = case _ of
  Update i -> H.modify_ $ const i
  ChooseClass c -> H.raise ( ClassChosen c )
  NoOp -> pure unit

M src/UI/Entry.purs => src/UI/Entry.purs +9 -3
@@ 23,8 23,9 @@ data Output =

data Query a = Load Input a

data Action =
  XPOutput UIXP.Output
data Action
  = XPOutput UIXP.Output
  | ChangeClass UC.Output

type Slots = ( xp :: (UIXP.Slot Unit)
             , lvl :: (UILevel.Slot Unit)


@@ 56,13 57,18 @@ render state = let
                   , "p-2"
                   ]
    ]
    [ HH.slot_ _class unit UC.component cl
    [ HH.slot _class unit UC.component cl ChangeClass
    , HH.slot _xp unit UIXP.component { xp } XPOutput
    , HH.slot_ _lvl unit UILevel.component xp
    ]

handleAction :: forall m. MonadEffect m => Action -> H.HalogenM State Action Slots Output m Unit
handleAction = case _ of
  ChangeClass output -> do
    case output of
      UC.ClassChosen c -> H.modify_ \ch -> C.setClass ch c
    c <- H.get
    H.raise $ Save c
  XPOutput output -> do
    case output of
      UIXP.IncXP -> H.modify_ $ C.modifyXP CXP.inc