Skip to content

Instantly share code, notes, and snippets.

@ryan-senn
Last active December 31, 2019 13:21
Show Gist options
  • Save ryan-senn/d6731568a5b5eac893d7719f55e36c67 to your computer and use it in GitHub Desktop.
Save ryan-senn/d6731568a5b5eac893d7719f55e36c67 to your computer and use it in GitHub Desktop.
Update nesting
module Modules.Auth.Login.Msg exposing (..)
import Http exposing (Error)
import Types exposing (User)
type LoginMsg
= LoginUpdateEmail String
| LoginUpdatePassword String
| LoginRequest
| LoginResponse (Result Error User)
module Modules.Auth.Login.Update exposing (..)
import Model exposing (Model)
import Modules.Auth.Login.Model exposing (LoginModel, initialLoginModel)
import Msg exposing (Msg (LoginMsg))
import Modules.Auth.Login.Msg exposing (..)
import Types exposing (FlashMessage)
import Modules.Auth.Login.Api as Api exposing (..)
import Common.FlashMessages exposing (addFlashMessage)
import Routing.Routes exposing (navigate, Route (Portfolios))
update : LoginMsg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
LoginUpdateEmail value ->
{ model
| loginModel = updateEmail value model.loginModel
} ! []
LoginUpdatePassword value ->
{ model
| loginModel = updatePassword value model.loginModel
} ! []
LoginRequest ->
{ model
| loginModel = request model.loginModel
} ! [ Api.loginRequest model.loginModel.email model.loginModel.password ]
LoginResponse result ->
case result of
Err error ->
{ model
| loginModel = errorResponse (toString error) model.loginModel
, flashMessages = addFlashMessage (FlashMessage Types.Danger (toString error)) model.flashMessages
} ! []
Ok user ->
{ model
| loginModel = initialLoginModel
, user = Just user
} ! [ navigate Portfolios ]
updateEmail : String -> LoginModel -> LoginModel
updateEmail value model =
{ model
| email = value
}
updatePassword : String -> LoginModel -> LoginModel
updatePassword value model =
{ model
| password = value
}
request : LoginModel -> LoginModel
request model =
{ model
| isLoggingIn = True
}
errorResponse : String -> LoginModel -> LoginModel
errorResponse error model =
{ model
| isLoggingIn = False
, error = Just error
}
module Msg exposing (..)
import Navigation exposing (Location)
import Routing.Routes exposing (Route)
import Modules.Auth.Login.Msg exposing (LoginMsg)
import Modules.Auth.Logout.Msg exposing (LogoutMsg)
import Modules.Auth.Signup.Msg exposing (SignupMsg)
import Modules.Auth.ForgotPassword.Msg exposing (ForgotPasswordMsg)
import Modules.Auth.ResetPassword.Msg exposing (ResetPasswordMsg)
import Modules.Dividends.Add.Msg exposing (AddDividendMsg)
import Modules.Dividends.Edit.Msg exposing (EditDividendMsg)
import Modules.Stocks.List.Msg exposing (StockListMsg)
import Modules.Stocks.Add.Msg exposing (AddStockMsg)
import Modules.Stocks.View.Msg exposing (ViewStockMsg)
import Modules.Transactions.Add.Msg exposing (AddTransactionMsg)
import Modules.Transactions.Edit.Msg exposing (EditTransactionMsg)
import Modules.Portfolios.List.Msg exposing (PortfolioListMsg)
import Modules.Portfolios.View.Msg exposing (ViewPortfolioMsg)
import Modules.Portfolios.Add.Msg exposing (AddPortfolioMsg)
import Modules.Portfolios.Edit.Msg exposing (EditPortfolioMsg)
type Msg
= UrlChange Location
| Navigate Route
| LoginMsg LoginMsg
| LogoutMsg LogoutMsg
| SignupMsg SignupMsg
| ForgotPasswordMsg ForgotPasswordMsg
| ResetPasswordMsg ResetPasswordMsg
| PortfolioListMsg PortfolioListMsg
| ViewPortfolioMsg ViewPortfolioMsg
| AddPortfolioMsg AddPortfolioMsg
| EditPortfolioMsg EditPortfolioMsg
| StockListMsg StockListMsg
| AddStockMsg AddStockMsg
| ViewStockMsg ViewStockMsg
| AddDividendMsg AddDividendMsg
| EditDividendMsg EditDividendMsg
| AddTransactionMsg AddTransactionMsg
| EditTransactionMsg EditTransactionMsg
module Update exposing (update)
import Msg exposing (Msg (..))
import Model exposing (Model)
import Routing.Update
import Modules.Auth.Login.Update
import Modules.Auth.Logout.Update
import Modules.Auth.Signup.Update
import Modules.Auth.ForgotPassword.Update
import Modules.Auth.ResetPassword.Update
import Modules.Dividends.Add.Update
import Modules.Dividends.Edit.Update
import Modules.Transactions.Add.Update
import Modules.Transactions.Edit.Update
import Modules.Stocks.List.Update
import Modules.Stocks.Add.Update
import Modules.Stocks.View.Update
import Modules.Portfolios.List.Update
import Modules.Portfolios.View.Update
import Modules.Portfolios.Add.Update
import Modules.Portfolios.Edit.Update
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
UrlChange location ->
Routing.Update.urlChange location model
Navigate route ->
Routing.Update.routeChange route model
LoginMsg msg ->
Modules.Auth.Login.Update.update msg model
LogoutMsg msg ->
Modules.Auth.Logout.Update.update msg model
SignupMsg msg ->
Modules.Auth.Signup.Update.update msg model
ForgotPasswordMsg msg ->
Modules.Auth.ForgotPassword.Update.update msg model
ResetPasswordMsg msg ->
Modules.Auth.ResetPassword.Update.update msg model
PortfolioListMsg msg ->
Modules.Portfolios.List.Update.update msg model
ViewPortfolioMsg msg ->
Modules.Portfolios.View.Update.update msg model
AddPortfolioMsg msg ->
Modules.Portfolios.Add.Update.update msg model
EditPortfolioMsg msg ->
Modules.Portfolios.Edit.Update.update msg model
StockListMsg msg ->
Modules.Stocks.List.Update.update msg model
AddStockMsg msg ->
Modules.Stocks.Add.Update.update msg model
ViewStockMsg msg ->
Modules.Stocks.View.Update.update msg model
AddDividendMsg msg ->
Modules.Dividends.Add.Update.update msg model
EditDividendMsg msg ->
Modules.Dividends.Edit.Update.update msg model
AddTransactionMsg msg ->
Modules.Transactions.Add.Update.update msg model
EditTransactionMsg msg ->
Modules.Transactions.Edit.Update.update msg model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment