Authentication form is rendered only if no Token. Component output TODO.

master
Philippe Pittoli 2023-06-01 03:20:53 +02:00
parent 39bc79d05c
commit 361558f9f1
2 changed files with 25 additions and 12 deletions

View File

@ -127,12 +127,12 @@ type WebSocketMessageType = ArrayBuffer
-- Root component module
--------------------------------------------------------------------------------
type Slot = H.Slot Query Void
data Output = AuthToken String
type Slot = H.Slot Query Output
type Query :: forall k. k -> Type
type Query = Const Void
type Input = String
type Output = Void
data Action
= Initialize

View File

@ -15,12 +15,13 @@ import Halogen.HTML.Properties as HP
import Type.Proxy (Proxy(..))
import Effect.Aff.Class (class MonadAff)
data Action = ReadStates
data Action = ReadStates | Authenticated AF.Output
type State =
{ a :: Maybe Boolean
, b :: Maybe Int
, c :: Maybe String
, token :: Maybe String
}
type ChildSlots =
@ -46,10 +47,10 @@ component =
}
initialState :: forall i. i -> State
initialState _ = { a: Nothing, b: Nothing, c: Nothing }
initialState _ = { a: Nothing, b: Nothing, c: Nothing, token: Nothing }
render :: forall m. MonadAff m => State -> H.ComponentHTML Action ChildSlots m
render state = HH.div_
render state = HH.div_ $
[ HH.div
[ HP.class_ (H.ClassName "box") ]
[ HH.h1_ [ HH.text "Component A" ]
@ -75,22 +76,34 @@ render state = HH.div_
, HH.button
[ HE.onClick \_ -> ReadStates ]
[ HH.text "Check states now" ]
, HH.div
[ HP.class_ (H.ClassName "box") ]
[ HH.h1_ [ HH.text "Authentication form" ]
, HH.slot_ _af unit AF.component "ws://127.0.0.1:8081"
]
, HH.div
] <> [ render_auth_form ] <>
[ HH.div
[ HP.class_ (H.ClassName "box") ]
[ HH.h1_ [ HH.text "Original interface" ]
, HH.slot_ _oi unit OI.component "ws://127.0.0.1:8080"
]
]
where
render_auth_form :: forall m. MonadAff m => H.ComponentHTML Action ChildSlots m
render_auth_form = case state.token of
Nothing -> HH.div
[ HP.class_ (H.ClassName "box") ]
[ HH.h1_ [ HH.text "Authentication form" ]
, HH.slot_ _af unit AF.component "ws://127.0.0.1:8081"
]
Just current_token -> HH.div
[ HP.class_ (H.ClassName "box") ]
[ HH.p_ [ HH.text ("Token is: " <> current_token) ] ]
handleAction :: forall o m. MonadAff m => Action -> H.HalogenM State Action ChildSlots o m Unit
handleAction = case _ of
Authenticated authaction -> case authaction of
AF.AuthToken newtoken -> do
-- TODO: do something more.
H.modify_ _ { token = Just newtoken }
ReadStates -> do
{ token } <- H.get
a <- H.request _a unit CA.IsOn
b <- H.request _b unit CB.GetCount
c <- H.request _c unit CC.GetValue
H.put { a, b, c }
H.put { a, b, c, token }