Skip to content

Commit

Permalink
refactor(elm): Separate models (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrapacz authored Apr 21, 2018
1 parent 1c6454e commit 7af4177
Show file tree
Hide file tree
Showing 46 changed files with 693 additions and 777 deletions.
14 changes: 7 additions & 7 deletions aion/web/elm/src/App.elm
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module App exposing (..)

import Bootstrap.Navbar as Navbar
import General.Models exposing (Flags, Model, initialModel)
import Msgs exposing (Msg(MkGeneralMsg, MkPanelMsg, MkRoomMsg, MkUserMsg, NavbarMsg))
import Lobby.Api exposing (fetchRooms)
import Models exposing (Flags, Model, initModel)
import Msgs exposing (Msg(MkLobbyMsg, 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
Expand All @@ -28,13 +28,13 @@ init flags location =
Navbar.initialState NavbarMsg

getInitialModel =
initialModel flags currentRoute location
initModel flags currentRoute location
in
( { getInitialModel | navbarState = navbarState }
, Cmd.batch
[ setHomeUrl location
, navbarCmd
, Cmd.map MkGeneralMsg (fetchRooms location flags.token)
, Cmd.map MkLobbyMsg (fetchRooms location flags.token)
, Cmd.map MkPanelMsg (fetchCategories location flags.token)
, Cmd.map MkUserMsg (fetchCurrentUser location flags.token)
]
Expand All @@ -44,10 +44,10 @@ init flags location =
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Phoenix.Socket.listen model.socket PhoenixMsg |> Sub.map MkRoomMsg
[ Phoenix.Socket.listen model.roomData.socket PhoenixMsg |> Sub.map MkRoomMsg
, Navbar.subscriptions model.navbarState NavbarMsg
, Multiselect.subscriptions model.panelData.categoryMultiSelect |> Sub.map MultiselectMsg |> Sub.map MkPanelMsg
, Room.Subscriptions.subscriptions model |> Sub.map MkRoomMsg
, Room.Subscriptions.subscriptions model.roomData |> Sub.map MkRoomMsg
]


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


loginFormMsg : String
loginFormMsg =
"Don't have an account? Click here to register."


registerFormMsg : String
registerFormMsg =
"Already have an account? Click here to login."


authPageRightColumnContent : List String
authPageRightColumnContent =
[ "Aion is an e-learning platform written in Elixir and Elm based on real-time gameplay."
, "It's basically an erudite quiz with over 4000 questions."
, "Challenge your friends, gather points and climb up the rankings."
]
27 changes: 23 additions & 4 deletions aion/web/elm/src/Auth/Models.elm
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
module Auth.Models exposing (..)

import Auth.Constants exposing (loginFormMsg)
import Forms
import Navigation exposing (Location)
import Toasty
import Toasty.Defaults


type alias AuthData =
{ loginForm : LoginForm
{ formMsg : String
, location : Location
, loginForm : LoginForm
, msg : String
, registrationForm : RegistrationForm
, unauthenticatedView : UnauthenticatedViewToggle
, formMsg : String
, toasties : Toasty.Stack Toasty.Defaults.Toast
, token : Maybe Token
, msg : String
, unauthenticatedView : UnauthenticatedViewToggle
}


initAuthData : Location -> Maybe Token -> AuthData
initAuthData location token =
{ formMsg = loginFormMsg
, location = location
, loginForm = Forms.initForm loginForm
, msg = ""
, registrationForm = Forms.initForm registrationForm
, token = token
, toasties = Toasty.initialState
, unauthenticatedView = LoginView
}


Expand Down
8 changes: 4 additions & 4 deletions aion/web/elm/src/Auth/Notifications.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Auth.Notifications exposing (..)

import Auth.Models exposing (AuthData)
import Auth.Msgs exposing (AuthMsg(ToastyMsg))
import General.Models exposing (Model)
import Html.Attributes exposing (class)
import Toasty
import Toasty.Defaults
Expand All @@ -10,7 +10,7 @@ import Toasty.Defaults
-- registration notifications


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

Expand All @@ -19,7 +19,7 @@ registrationErrorToast =
-- login notifications


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

Expand All @@ -31,6 +31,6 @@ toastsConfig =
|> Toasty.containerAttrs [ class "toasty-notification" ]


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

import Auth.Api exposing (registerUser, submitCredentials)
import Auth.Models exposing (UnauthenticatedViewToggle(LoginView, RegisterView))
import Auth.Constants exposing (loginFormMsg, registerFormMsg)
import Auth.Models exposing (AuthData, 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 : AuthMsg -> AuthData -> ( AuthData, Cmd AuthMsg )
update msg model =
case msg of
Login ->
model
! [ submitCredentials model.location model.authData.loginForm ]
! [ submitCredentials model.location model.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
}
! []
case res of
Ok token ->
{ model | token = Just token, msg = "" } ! []

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

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

RegistrationResult response ->
case response of
Expand All @@ -48,90 +36,37 @@ update msg model =
token =
responseData.token

oldAuthData =
model.authData

oldRegistrationForm =
oldAuthData.registrationForm

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

_ ->
model
! []
|> registrationErrorToast
model ! [] |> registrationErrorToast

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

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

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

UpdateLoginForm name value ->
let
oldAuthData =
model.authData

loginForm =
oldAuthData.loginForm

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

UpdateRegistrationForm name value ->
let
oldAuthData =
model.authData

registrationForm =
oldAuthData.registrationForm

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

-- Toasty
ToastyMsg subMsg ->
Expand Down
13 changes: 6 additions & 7 deletions aion/web/elm/src/Auth/View.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Auth.View exposing (..)

import Auth.Constants exposing (authPageRightColumnContent)
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)
Expand All @@ -9,28 +10,26 @@ import Bootstrap.Form as Form
import Bootstrap.Form.Input as Input
import Bootstrap.Grid as Grid
import Forms
import General.Constants exposing (authPageRightColumnContent)
import General.Models exposing (Model)
import Html exposing (Html, br, div, h2, p, span, text)
import Html.Attributes exposing (class, for)
import Toasty
import Toasty.Defaults


authView : Model -> Html AuthMsg
authView : AuthData -> Html AuthMsg
authView model =
let
unauthenticatedView =
model.authData.unauthenticatedView
model.unauthenticatedView

loginForm =
model.authData.loginForm
model.loginForm

registrationForm =
model.authData.registrationForm
model.registrationForm

formMsg =
model.authData.formMsg
model.formMsg
in
Grid.container [ class "auth-container" ]
[ Grid.container []
Expand Down
9 changes: 9 additions & 0 deletions aion/web/elm/src/Constants.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Constants exposing (..)


footerContent : List String
footerContent =
[ "JPKS jest projektem niekomercyjnym, stworzonym ku nauce oraz uciesze uczniów i wszystkich osób lubiących tego typu rozrywki."
, "Autorem oryginalnego pomysłu (PKS = Projekt-Klient-Serwer, napisanego w ANSI C) jest Marek Lipert, zaś cały projekt był zgłoszony w programie Diversity firmy Motorola."
, "Jeśli właściciel praw autorskich do jakiegoś materiału nie wyraża zgody na jego wykorzystanie, prosimy o kontakt z autorem quizu (Andrzej Dyrek na fb)."
]
15 changes: 0 additions & 15 deletions aion/web/elm/src/General/Api.elm

This file was deleted.

Loading

0 comments on commit 7af4177

Please sign in to comment.