From 361558f9f118c1b5678b0bb39c9540ea51327f68 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Thu, 1 Jun 2023 03:20:53 +0200 Subject: [PATCH] Authentication form is rendered only if no Token. Component output TODO. --- src/App/AuthenticationForm.purs | 4 ++-- src/App/Container.purs | 33 +++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/App/AuthenticationForm.purs b/src/App/AuthenticationForm.purs index 426b405..5558fd1 100644 --- a/src/App/AuthenticationForm.purs +++ b/src/App/AuthenticationForm.purs @@ -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 diff --git a/src/App/Container.purs b/src/App/Container.purs index cbf723e..19a5d57 100644 --- a/src/App/Container.purs +++ b/src/App/Container.purs @@ -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 }