Register: new form inputs.

master
Philippe Pittoli 2023-06-03 00:54:18 +02:00
parent c42cbbeb8a
commit 57367168ae
1 changed files with 169 additions and 76 deletions

View File

@ -7,9 +7,6 @@ import Control.Monad.State (class MonadState)
import Data.Array as A import Data.Array as A
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Data.Bifunctor (lmap) import Data.Bifunctor (lmap)
-- import Data.Codec.Argonaut (JsonCodec, JsonDecodeError)
-- import Data.Argonaut.Core as J
-- import Data.Codec.Argonaut as CA
import Data.Const (Const) import Data.Const (Const)
import Data.Either (Either(..)) import Data.Either (Either(..))
import Data.Maybe (Maybe(..), isJust, isNothing, maybe) import Data.Maybe (Maybe(..), isJust, isNothing, maybe)
@ -138,17 +135,35 @@ data Action
= Initialize = Initialize
| WebSocketParseError String | WebSocketParseError String
| ConnectWebSocket | ConnectWebSocket
-- Authentication.
| HandleLoginInputUpdate String | HandleLoginInputUpdate String
| HandlePassInputUpdate String | HandlePassInputUpdate String
-- Register.
| HandleLoginRegisterInputUpdate String
| HandleEmailRegisterInputUpdate String
| HandlePassRegisterInputUpdate String
| AuthenticationAttempt Event | AuthenticationAttempt Event
| RegisterAttempt Event
| Finalize | Finalize
| HandleWebSocket (WebSocketEvent WebSocketMessageType) | HandleWebSocket (WebSocketEvent WebSocketMessageType)
type State = type State =
{ messages :: Array String { messages :: Array String
, messageHistoryLength :: Int , messageHistoryLength :: Int
-- Authentication.
, loginInputText :: String , loginInputText :: String
, passInputText :: String , passInputText :: String
-- Register.
, loginRegisterInputText :: String
, emailRegisterInputText :: String
, passRegisterInputText :: String
-- Network stuff.
, wsUrl :: String , wsUrl :: String
, wsConnection :: Maybe WS.WebSocket , wsConnection :: Maybe WS.WebSocket
, canReconnect :: Boolean , canReconnect :: Boolean
@ -170,8 +185,17 @@ initialState :: Input -> State
initialState input = initialState input =
{ messages: [] { messages: []
, messageHistoryLength: 10 , messageHistoryLength: 10
-- Authentication.
, loginInputText: "" , loginInputText: ""
, passInputText: "" , passInputText: ""
-- Register.
, loginRegisterInputText: ""
, emailRegisterInputText: ""
, passRegisterInputText: ""
-- Network stuff.
, wsUrl: input , wsUrl: input
, wsConnection: Nothing , wsConnection: Nothing
, canReconnect: false , canReconnect: false
@ -191,21 +215,36 @@ wrapperStyle =
""" """
render :: forall m. State -> H.ComponentHTML Action () m render :: forall m. State -> H.ComponentHTML Action () m
render { messages, loginInputText, passInputText, wsConnection, canReconnect } = render {
HH.div messages,
wsConnection,
canReconnect,
-- Authentication.
loginInputText,
passInputText,
-- Register.
loginRegisterInputText,
emailRegisterInputText,
passRegisterInputText }
= HH.div
[ HP.style wrapperStyle ] [ HP.style wrapperStyle ]
[ HH.h2_ [ HH.text "Authentication!" ] [ render_auth_form
, render_auth_form , render_register_form
, render_messages , render_messages
--, renderMaxHistoryLength messageHistoryLength
, renderReconnectButton (isNothing wsConnection && canReconnect)
] ]
where where
render_auth_form = HH.form render_auth_form = HH.form
[ HE.onSubmit AuthenticationAttempt ] [ HE.onSubmit AuthenticationAttempt ]
[ HH.p_ [ HH.h2_ [ HH.text "Authentication!" ]
, HH.p_
[ HH.div_ [ HH.div_
[ HH.input [ HH.input
[ HP.style "padding: 0.5rem 0.75rem; margin-bottom: 0.25rem;" [ inputCSS
, HP.type_ HP.InputText , HP.type_ HP.InputText
, HP.value loginInputText , HP.value loginInputText
, HE.onValueInput HandleLoginInputUpdate , HE.onValueInput HandleLoginInputUpdate
@ -214,8 +253,8 @@ render { messages, loginInputText, passInputText, wsConnection, canReconnect } =
] ]
, HH.div_ , HH.div_
[ HH.input [ HH.input
[ HP.style "padding: 0.5rem 0.75rem; margin-bottom: 0.25rem;" [ inputCSS
, HP.type_ HP.InputText , HP.type_ HP.InputPassword
, HP.value passInputText , HP.value passInputText
, HE.onValueInput HandlePassInputUpdate , HE.onValueInput HandlePassInputUpdate
, maybe (HP.disabled true) (\_ -> HP.enabled true) wsConnection , maybe (HP.disabled true) (\_ -> HP.enabled true) wsConnection
@ -230,8 +269,48 @@ render { messages, loginInputText, passInputText, wsConnection, canReconnect } =
[ HH.text "Send Message to Server" ] [ HH.text "Send Message to Server" ]
] ]
] ]
--, renderMaxHistoryLength messageHistoryLength ]
, renderReconnectButton (isNothing wsConnection && canReconnect)
render_register_form = HH.form
[ HE.onSubmit RegisterAttempt ]
[ HH.h2_ [ HH.text "Register!" ]
, HH.p_
[ HH.div_
[ HH.input
[ inputCSS
, HP.type_ HP.InputText
, HP.value loginRegisterInputText
, HE.onValueInput HandleLoginRegisterInputUpdate
, maybe (HP.disabled true) (\_ -> HP.enabled true) wsConnection
]
]
, HH.div_
[ HH.input
[ inputCSS
, HP.type_ HP.InputText
, HP.value emailRegisterInputText
, HE.onValueInput HandleEmailRegisterInputUpdate
, maybe (HP.disabled true) (\_ -> HP.enabled true) wsConnection
]
]
, HH.div_
[ HH.input
[ inputCSS
, HP.type_ HP.InputPassword
, HP.value passRegisterInputText
, HE.onValueInput HandlePassRegisterInputUpdate
, maybe (HP.disabled true) (\_ -> HP.enabled true) wsConnection
]
]
, HH.div_
[ HH.button
[ HP.style "padding: 0.5rem 1.25rem;"
, HP.type_ HP.ButtonSubmit
, maybe (HP.disabled true) (\_ -> HP.enabled true) wsConnection
]
[ HH.text "Send Message to Server" ]
]
]
] ]
render_messages = HH.ul_ $ map (\msg -> HH.li_ [ HH.text msg ]) messages render_messages = HH.ul_ $ map (\msg -> HH.li_ [ HH.text msg ]) messages
@ -283,12 +362,24 @@ handleAction = case _ of
H.modify_ _ { wsConnection = Just webSocket } H.modify_ _ { wsConnection = Just webSocket }
void $ H.subscribe (HandleWebSocket <$> webSocketEmitter webSocket) void $ H.subscribe (HandleWebSocket <$> webSocketEmitter webSocket)
-- Authentication.
HandleLoginInputUpdate text -> do HandleLoginInputUpdate text -> do
H.modify_ _ { loginInputText = text } H.modify_ _ { loginInputText = text }
HandlePassInputUpdate text -> do HandlePassInputUpdate text -> do
H.modify_ _ { passInputText = text } H.modify_ _ { passInputText = text }
-- Register.
HandleLoginRegisterInputUpdate text -> do
H.modify_ _ { loginRegisterInputText = text }
HandleEmailRegisterInputUpdate text -> do
H.modify_ _ { emailRegisterInputText = text }
HandlePassRegisterInputUpdate text -> do
H.modify_ _ { passRegisterInputText = text }
RegisterAttempt ev -> do
H.liftEffect $ Event.preventDefault ev
-- TODO
AuthenticationAttempt ev -> do AuthenticationAttempt ev -> do
H.liftEffect $ Event.preventDefault ev H.liftEffect $ Event.preventDefault ev
@ -350,7 +441,7 @@ handleAction = case _ of
H.raise $ AuthToken msg.token H.raise $ AuthToken msg.token
-- WTH?! -- WTH?!
_ -> do _ -> do
appendMessage $ "[😈] Failed! Don't understand the answer received!" appendMessage $ "[😈] Failed! Authentication server didn't send a valid message."
WebSocketOpen -> do WebSocketOpen -> do
{ wsUrl } <- H.get { wsUrl } <- H.get
@ -444,6 +535,8 @@ foreignToArrayBuffer
renderForeignErrors = renderForeignErrors =
String.joinWith "; " <<< A.fromFoldable <<< map F.renderForeignError String.joinWith "; " <<< A.fromFoldable <<< map F.renderForeignError
inputCSS = HP.style "padding: 0.5rem 0.75rem; margin-bottom: 0.25rem;"
print_json_string :: forall m. MonadEffect m => MonadState State m => ArrayBuffer -> m Unit print_json_string :: forall m. MonadEffect m => MonadState State m => ArrayBuffer -> m Unit
print_json_string arraybuffer = do print_json_string arraybuffer = do
-- fromTypedIPC :: ArrayBuffer -> Effect (Either ParseError (Tuple UInt String)) -- fromTypedIPC :: ArrayBuffer -> Effect (Either ParseError (Tuple UInt String))