module View.SignIn ( SignInMessage (..) , view ) where import qualified Data.Either as Either import qualified Data.Maybe as Maybe import Data.Text (Text) import Data.Validation (Validation) import Prelude hiding (error) import Reflex.Dom (Event, MonadWidget) import qualified Reflex.Dom as R import Common.Model (SignInForm (SignInForm)) import qualified Common.Msg as Msg import qualified Common.Validation.SignIn as SignInValidation import qualified Component.Button as Button import qualified Component.Form as Form import qualified Component.Input as Input import qualified Util.Ajax as Ajax import qualified Util.Validation as ValidationUtil import qualified Util.WaitFor as WaitFor data SignInMessage = SuccessMessage Text | ErrorMessage Text | EmptyMessage view :: forall t m. MonadWidget t m => SignInMessage -> m () view signInMessage = R.divClass "signIn" $ Form.view $ do rec input <- (Input.view (Input.defaultIn { Input._in_label = Msg.get Msg.SignIn_EmailLabel , Input._in_validation = SignInValidation.email }) ("" <$ R.ffilter Either.isRight signInResult) validate) validate <- Button._out_clic <$> (Button.view $ (Button.defaultIn (R.text $ Msg.get Msg.SignIn_Button)) { Button._in_class = R.constDyn "validate" , Button._in_waiting = waiting , Button._in_submit = True }) let form = SignInForm <$> Input._out_raw input (signInResult, waiting) <- WaitFor.waitFor (Ajax.post "/api/askSignIn") (ValidationUtil.fireMaybe ((\f -> f <$ SignInValidation.signIn f) <$> form) validate) showSignInResult signInMessage signInResult showSignInResult :: forall t m. MonadWidget t m => SignInMessage -> Event t (Either Text Text) -> m () showSignInResult signInMessage signInResult = do _ <- R.widgetHold (showInitResult signInMessage) $ R.ffor signInResult showResult R.blank where showInitResult (SuccessMessage success) = showSuccess success showInitResult (ErrorMessage error) = showError error showInitResult EmptyMessage = R.blank showResult (Left error) = showError error showResult (Right success) = showSuccess success showError = R.divClass "error" . R.text showSuccess = R.divClass "success" . R.text