Skip to content

Commit

Permalink
refactor(elm): Split update (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrapacz authored Mar 4, 2018
1 parent 0722f17 commit 1c6454e
Show file tree
Hide file tree
Showing 37 changed files with 1,181 additions and 928 deletions.
19 changes: 11 additions & 8 deletions aion/web/elm/src/App.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ module App exposing (..)

import Bootstrap.Navbar as Navbar
import General.Models exposing (Flags, Model, initialModel)
import Msgs exposing (Msg(NavbarMsg))
import Msgs exposing (Msg(MkGeneralMsg, MkPanelMsg, MkRoomMsg, MkUserMsg, NavbarMsg))
import Multiselect
import Navigation exposing (Location, modifyUrl)
import Panel.Api exposing (fetchCategories)
import Panel.Msgs exposing (PanelMsg(MultiselectMsg))
import Phoenix.Socket
import Room.Api exposing (fetchRooms)
import Room.Msgs exposing (RoomMsg(PhoenixMsg))
import Room.Subscriptions
import Routing
import Update exposing (setHomeUrl, update)
import Update exposing (update)
import UpdateHelpers exposing (setHomeUrl)
import User.Api exposing (fetchCurrentUser)
import View exposing (view)

Expand All @@ -31,20 +34,20 @@ init flags location =
, Cmd.batch
[ setHomeUrl location
, navbarCmd
, fetchRooms location flags.token
, fetchCategories location flags.token
, fetchCurrentUser location flags.token
, Cmd.map MkGeneralMsg (fetchRooms location flags.token)
, Cmd.map MkPanelMsg (fetchCategories location flags.token)
, Cmd.map MkUserMsg (fetchCurrentUser location flags.token)
]
)


subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Phoenix.Socket.listen model.socket Msgs.PhoenixMsg
[ Phoenix.Socket.listen model.socket PhoenixMsg |> Sub.map MkRoomMsg
, Navbar.subscriptions model.navbarState NavbarMsg
, Sub.map Msgs.MultiselectMsg <| Multiselect.subscriptions model.panelData.categoryMultiSelect
, Room.Subscriptions.subscriptions model
, Multiselect.subscriptions model.panelData.categoryMultiSelect |> Sub.map MultiselectMsg |> Sub.map MkPanelMsg
, Room.Subscriptions.subscriptions model |> Sub.map MkRoomMsg
]


Expand Down
6 changes: 3 additions & 3 deletions aion/web/elm/src/Auth/Api.elm
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Auth.Api exposing (..)

import Auth.Models exposing (RegistrationForm, RegistrationResultData)
import Auth.Msgs exposing (AuthMsg(LoginResult, RegistrationResult))
import Forms
import Http
import Json.Decode as Decode exposing (Value)
import Json.Decode.Pipeline exposing (decode, required)
import Json.Encode as Encode
import Msgs exposing (Msg(..))
import Navigation exposing (Location)
import RemoteData
import Urls exposing (loginUrl, registerUrl)
Expand All @@ -20,7 +20,7 @@ authenticate url decoder credentials =
Http.post url (Http.jsonBody credentials) decoder


submitCredentials : Location -> Forms.Form -> Cmd Msg
submitCredentials : Location -> Forms.Form -> Cmd AuthMsg
submitCredentials location form =
let
payload =
Expand All @@ -46,7 +46,7 @@ tokenStringDecoder =
-- register section


registerUser : Location -> Forms.Form -> Cmd Msg
registerUser : Location -> Forms.Form -> Cmd AuthMsg
registerUser location form =
Http.post (registerUrl location) (registrationDataEncoder form) registrationResultDecoder
|> RemoteData.sendRequest
Expand Down
18 changes: 18 additions & 0 deletions aion/web/elm/src/Auth/Msgs.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Auth.Msgs exposing (..)

import Auth.Models exposing (RegistrationResultData)
import Http
import RemoteData exposing (WebData)
import Toasty
import Toasty.Defaults


type AuthMsg
= Login
| LoginResult (Result Http.Error String)
| Register
| RegistrationResult (WebData RegistrationResultData)
| ChangeAuthForm
| UpdateLoginForm String String
| UpdateRegistrationForm String String
| ToastyMsg (Toasty.Msg Toasty.Defaults.Toast)
21 changes: 17 additions & 4 deletions aion/web/elm/src/Auth/Notifications.elm
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module Auth.Notifications exposing (..)

import Auth.Msgs exposing (AuthMsg(ToastyMsg))
import General.Models exposing (Model)
import General.Notifications exposing (addToast)
import Msgs exposing (Msg(..))
import Html.Attributes exposing (class)
import Toasty
import Toasty.Defaults


-- registration notifications


registrationErrorToast : ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
registrationErrorToast : ( Model, Cmd AuthMsg ) -> ( Model, Cmd AuthMsg )
registrationErrorToast =
addToast (Toasty.Defaults.Error "Error!" "Failed to register :(")

Expand All @@ -18,6 +19,18 @@ registrationErrorToast =
-- login notifications


loginErrorToast : ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
loginErrorToast : ( Model, Cmd AuthMsg ) -> ( Model, Cmd AuthMsg )
loginErrorToast =
addToast (Toasty.Defaults.Error "Error!" "Failed to login :(")


toastsConfig : Toasty.Config AuthMsg
toastsConfig =
Toasty.Defaults.config
|> Toasty.delay 1000
|> Toasty.containerAttrs [ class "toasty-notification" ]


addToast : Toasty.Defaults.Toast -> ( Model, Cmd AuthMsg ) -> ( Model, Cmd AuthMsg )
addToast toast ( model, cmd ) =
Toasty.addToast toastsConfig ToastyMsg toast ( model, cmd )
138 changes: 138 additions & 0 deletions aion/web/elm/src/Auth/Update.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
module Auth.Update exposing (..)

import Auth.Api exposing (registerUser, submitCredentials)
import Auth.Models exposing (UnauthenticatedViewToggle(LoginView, RegisterView))
import Auth.Msgs exposing (AuthMsg(ChangeAuthForm, Login, LoginResult, Register, RegistrationResult, ToastyMsg, UpdateLoginForm, UpdateRegistrationForm))
import Auth.Notifications exposing (loginErrorToast, registrationErrorToast, toastsConfig)
import Forms
import General.Constants exposing (loginFormMsg, registerFormMsg)
import General.Models exposing (Model)
import RemoteData
import Socket exposing (initSocket)
import Toasty
import UpdateHelpers exposing (postTokenActions, updateForm)


update : AuthMsg -> Model -> ( Model, Cmd AuthMsg )
update msg model =
case msg of
Login ->
model
! [ submitCredentials model.location model.authData.loginForm ]

LoginResult res ->
let
oldAuthData =
model.authData
in
case res of
Ok token ->
{ model
| authData = { oldAuthData | token = Just token, msg = "" }
, socket = initSocket token model.location
}
! []

Err err ->
{ model | authData = { oldAuthData | msg = toString err } }
! []
|> loginErrorToast

Register ->
model ! [ registerUser model.location model.authData.registrationForm ]

RegistrationResult response ->
case response of
RemoteData.Success responseData ->
let
token =
responseData.token

oldAuthData =
model.authData

oldRegistrationForm =
oldAuthData.registrationForm

newRegistrationForm =
updateForm "name" "" oldRegistrationForm
|> updateForm "email" ""
|> updateForm "password" ""
in
{ model
| authData = { oldAuthData | registrationForm = newRegistrationForm, token = Just token }
, socket = initSocket token model.location
}
! []

_ ->
model
! []
|> registrationErrorToast

ChangeAuthForm ->
let
oldAuthData =
model.authData

oldUnauthenticatedView =
oldAuthData.unauthenticatedView
in
case oldUnauthenticatedView of
LoginView ->
{ model
| authData =
{ oldAuthData
| unauthenticatedView = RegisterView
, formMsg = registerFormMsg
}
}
! []

RegisterView ->
{ model
| authData =
{ oldAuthData
| unauthenticatedView = LoginView
, formMsg = loginFormMsg
}
}
! []

UpdateLoginForm name value ->
let
oldAuthData =
model.authData

loginForm =
oldAuthData.loginForm

updatedLoginForm =
Forms.updateFormInput loginForm name value
in
{ model
| authData =
{ oldAuthData | loginForm = updatedLoginForm }
}
! []

UpdateRegistrationForm name value ->
let
oldAuthData =
model.authData

registrationForm =
oldAuthData.registrationForm

updatedRegistrationForm =
Forms.updateFormInput registrationForm name value
in
{ model
| authData =
{ oldAuthData | registrationForm = updatedRegistrationForm }
}
! []

-- Toasty
ToastyMsg subMsg ->
Toasty.update toastsConfig ToastyMsg subMsg model
26 changes: 13 additions & 13 deletions aion/web/elm/src/Auth/View.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Auth.View exposing (..)

import Auth.Models exposing (AuthData, LoginForm, RegistrationForm, UnauthenticatedViewToggle(LoginView, RegisterView))
import Auth.Msgs exposing (AuthMsg(ChangeAuthForm, Login, Register, ToastyMsg, UpdateLoginForm, UpdateRegistrationForm))
import Auth.Notifications exposing (toastsConfig)
import Bootstrap.Badge as Badge
import Bootstrap.Button as Button
import Bootstrap.Form as Form
Expand All @@ -9,15 +11,13 @@ import Bootstrap.Grid as Grid
import Forms
import General.Constants exposing (authPageRightColumnContent)
import General.Models exposing (Model)
import General.Notifications exposing (toastsConfig)
import Html exposing (Html, br, div, h2, p, span, text)
import Html.Attributes exposing (class, for)
import Msgs exposing (Msg(..))
import Toasty
import Toasty.Defaults


authView : Model -> Html Msg
authView : Model -> Html AuthMsg
authView model =
let
unauthenticatedView =
Expand All @@ -43,7 +43,7 @@ authView model =
]


authForm : UnauthenticatedViewToggle -> LoginForm -> RegistrationForm -> String -> Html Msg
authForm : UnauthenticatedViewToggle -> LoginForm -> RegistrationForm -> String -> Html AuthMsg
authForm unauthenticatedView loginForm registrationForm formMsg =
case unauthenticatedView of
LoginView ->
Expand All @@ -53,7 +53,7 @@ authForm unauthenticatedView loginForm registrationForm formMsg =
registrationFormView registrationForm formMsg


authPageRightColumn : Html Msg
authPageRightColumn : Html AuthMsg
authPageRightColumn =
div []
[ h2 []
Expand All @@ -74,7 +74,7 @@ authPageRightColumn =
-- auth form switch section


authFormToggle : String -> Html Msg
authFormToggle : String -> Html AuthMsg
authFormToggle formMsg =
Button.button
[ Button.attrs [ class "auth-toggle-button" ]
Expand All @@ -88,7 +88,7 @@ authFormToggle formMsg =
-- login form section


loginFormView : LoginForm -> String -> Html Msg
loginFormView : LoginForm -> String -> Html AuthMsg
loginFormView loginForm formMsg =
div [ class "login-container" ]
[ h2 [] [ text "Log in" ]
Expand All @@ -106,7 +106,7 @@ loginFormView loginForm formMsg =
]


emailLoginFormElement : Forms.Form -> Html Msg
emailLoginFormElement : Forms.Form -> Html AuthMsg
emailLoginFormElement form =
Form.group []
[ Form.label [ for "email" ] [ text "Email" ]
Expand All @@ -119,7 +119,7 @@ emailLoginFormElement form =
]


passwordLoginFormElement : Forms.Form -> Html Msg
passwordLoginFormElement : Forms.Form -> Html AuthMsg
passwordLoginFormElement form =
Form.group []
[ Form.label [ for "password" ] [ text "Password" ]
Expand All @@ -136,7 +136,7 @@ passwordLoginFormElement form =
-- registration form section


registrationFormView : RegistrationForm -> String -> Html Msg
registrationFormView : RegistrationForm -> String -> Html AuthMsg
registrationFormView registrationForm formMsg =
div [ class "registration-container" ]
[ h2 [] [ text "Register" ]
Expand All @@ -155,7 +155,7 @@ registrationFormView registrationForm formMsg =
]


nameRegisterFormElement : Forms.Form -> Html Msg
nameRegisterFormElement : Forms.Form -> Html AuthMsg
nameRegisterFormElement form =
Form.group []
[ Form.label [ for "name" ] [ text "Username" ]
Expand All @@ -168,7 +168,7 @@ nameRegisterFormElement form =
]


emailRegisterFormElement : Forms.Form -> Html Msg
emailRegisterFormElement : Forms.Form -> Html AuthMsg
emailRegisterFormElement form =
Form.group []
[ Form.label [ for "email" ] [ text "Email" ]
Expand All @@ -181,7 +181,7 @@ emailRegisterFormElement form =
]


passwordRegisterFormElement : Forms.Form -> Html Msg
passwordRegisterFormElement : Forms.Form -> Html AuthMsg
passwordRegisterFormElement form =
Form.group []
[ Form.label [ for "password" ] [ text "Password" ]
Expand Down
Loading

0 comments on commit 1c6454e

Please sign in to comment.