Authentication form is rendered only if no Token. Component output TODO.
parent
39bc79d05c
commit
361558f9f1
|
@ -127,12 +127,12 @@ type WebSocketMessageType = ArrayBuffer
|
||||||
-- Root component module
|
-- 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 :: forall k. k -> Type
|
||||||
type Query = Const Void
|
type Query = Const Void
|
||||||
type Input = String
|
type Input = String
|
||||||
type Output = Void
|
|
||||||
|
|
||||||
data Action
|
data Action
|
||||||
= Initialize
|
= Initialize
|
||||||
|
|
|
@ -15,12 +15,13 @@ import Halogen.HTML.Properties as HP
|
||||||
import Type.Proxy (Proxy(..))
|
import Type.Proxy (Proxy(..))
|
||||||
import Effect.Aff.Class (class MonadAff)
|
import Effect.Aff.Class (class MonadAff)
|
||||||
|
|
||||||
data Action = ReadStates
|
data Action = ReadStates | Authenticated AF.Output
|
||||||
|
|
||||||
type State =
|
type State =
|
||||||
{ a :: Maybe Boolean
|
{ a :: Maybe Boolean
|
||||||
, b :: Maybe Int
|
, b :: Maybe Int
|
||||||
, c :: Maybe String
|
, c :: Maybe String
|
||||||
|
, token :: Maybe String
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChildSlots =
|
type ChildSlots =
|
||||||
|
@ -46,10 +47,10 @@ component =
|
||||||
}
|
}
|
||||||
|
|
||||||
initialState :: forall i. i -> State
|
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 :: forall m. MonadAff m => State -> H.ComponentHTML Action ChildSlots m
|
||||||
render state = HH.div_
|
render state = HH.div_ $
|
||||||
[ HH.div
|
[ HH.div
|
||||||
[ HP.class_ (H.ClassName "box") ]
|
[ HP.class_ (H.ClassName "box") ]
|
||||||
[ HH.h1_ [ HH.text "Component A" ]
|
[ HH.h1_ [ HH.text "Component A" ]
|
||||||
|
@ -75,22 +76,34 @@ render state = HH.div_
|
||||||
, HH.button
|
, HH.button
|
||||||
[ HE.onClick \_ -> ReadStates ]
|
[ HE.onClick \_ -> ReadStates ]
|
||||||
[ HH.text "Check states now" ]
|
[ HH.text "Check states now" ]
|
||||||
, HH.div
|
] <> [ render_auth_form ] <>
|
||||||
[ HP.class_ (H.ClassName "box") ]
|
[ HH.div
|
||||||
[ HH.h1_ [ HH.text "Authentication form" ]
|
|
||||||
, HH.slot_ _af unit AF.component "ws://127.0.0.1:8081"
|
|
||||||
]
|
|
||||||
, HH.div
|
|
||||||
[ HP.class_ (H.ClassName "box") ]
|
[ HP.class_ (H.ClassName "box") ]
|
||||||
[ HH.h1_ [ HH.text "Original interface" ]
|
[ HH.h1_ [ HH.text "Original interface" ]
|
||||||
, HH.slot_ _oi unit OI.component "ws://127.0.0.1:8080"
|
, 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 :: forall o m. MonadAff m => Action -> H.HalogenM State Action ChildSlots o m Unit
|
||||||
handleAction = case _ of
|
handleAction = case _ of
|
||||||
|
Authenticated authaction -> case authaction of
|
||||||
|
AF.AuthToken newtoken -> do
|
||||||
|
-- TODO: do something more.
|
||||||
|
H.modify_ _ { token = Just newtoken }
|
||||||
ReadStates -> do
|
ReadStates -> do
|
||||||
|
{ token } <- H.get
|
||||||
a <- H.request _a unit CA.IsOn
|
a <- H.request _a unit CA.IsOn
|
||||||
b <- H.request _b unit CB.GetCount
|
b <- H.request _b unit CB.GetCount
|
||||||
c <- H.request _c unit CC.GetValue
|
c <- H.request _c unit CC.GetValue
|
||||||
H.put { a, b, c }
|
H.put { a, b, c, token }
|
||||||
|
|
Loading…
Reference in New Issue