How to turn a Msg into a Cmd Msg?
Only Use Sparsely: Better split the update function into multiple smaller functions.
Problem
Solution
Alternative Solution
1
type Msg =
2
LoginSucceeded User
3
| InfoMessage String
4
5
update : Msg -> Model -> (Model, Cmd Msg)
6
update msg model =
7
case msg of
8
LoginSucceeded newUser ->
9
( { model | currentUser = newUser }
10
, Cmd.none
11
)
12
InfoMessage message ->
13
( { model | message = Just message }
14
, Cmd.none
15
)
Copied!
1
type Msg =
2
LoginSucceeded User
3
| InfoMessage String
4
5
update : Msg -> Model -> (Model, Cmd Msg)
6
update msg model =
7
case msg of
8
LoginSucceeded newUser ->
9
( { model | currentUser = newUser }
10
, sendMsg <| InfoMessage "Login Successful"
11
)
12
InfoMessage message ->
13
( { model | message = Just message }
14
, Cmd.none
15
)
16
​
17
sendMsg : msg -> Cmd msg
18
sendMsg msg =
19
Task.succeed msg
20
|> Task.perform identity
Copied!
1
type Msg =
2
LoginSucceeded User
3
| InfoMessage String
4
5
update : Msg -> Model -> (Model, Cmd Msg)
6
update msg model =
7
case msg of
8
LoginSucceeded newUser ->
9
( model
10
|> updateUser newUser
11
|> updateMessage "Login Successful"
12
, Cmd.none
13
)
14
InfoMessage message ->
15
( model |> updateMessage message
16
, Cmd.none
17
)
18
​
19
updateUser : User -> Model -> Model
20
updateUser user model =
21
{ model | currentUser = user }
22
​
23
updateMessage : String -> Model -> Model
24
updateMessage message model =
25
{ model | message = Just message }
Copied!
This is the a better solution

Question

How can I display a Message after a user logged in?

Answer

Use the following function:
1
sendMsg : msg -> Cmd msg
2
sendMsg msg =
3
Task.succeed msg
4
|> Task.perform identity
Copied!

Further reading