-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
116 lines (95 loc) · 2.85 KB
/
Main.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module Main exposing (main)
import Browser
import Dict
import Html exposing (Html)
import Html.Events
import Json.Encode as JE
import Phoenix
type alias Model =
{ phoenix : Phoenix.Model
, phoenixUrl : String
, phoenixChannels : Phoenix.Channels
, messages : List Phoenix.OutMsg
, response : Maybe Phoenix.Response
}
type Msg
= PhoenixMsg (Phoenix.Msg Msg)
| PhoenixOutMsg Phoenix.OutMsg
| ButtonClickResponse Phoenix.Response
main : Program () Model Msg
main =
Browser.element
{ init = init
, subscriptions = subscriptions
, update = phoenixUpdate
, view = view
}
init : () -> ( Model, Cmd Msg )
init () =
let
( phoenixModel, phoenixCmd ) =
Phoenix.init
in
( { phoenix = phoenixModel
, phoenixUrl = "ws://your-url/socket"
, phoenixChannels = Dict.fromList [ ( "your_channel", { topic = "your_channel", params = JE.object [] } ) ]
, messages = []
, response = Nothing
}
, Cmd.map PhoenixMsg phoenixCmd
)
subscriptions : Model -> Sub Msg
subscriptions model =
Phoenix.subscriptions PhoenixMsg model.phoenix
phoenixUpdate : Msg -> Model -> ( Model, Cmd Msg )
phoenixUpdate msg model =
let
( newModel, cmd ) =
update msg model
( phoenixModel, phoenixCmd ) =
Phoenix.listen model.phoenixUrl model.phoenixChannels newModel.phoenix
in
( { newModel | phoenix = phoenixModel }, Cmd.batch [ cmd, phoenixCmd ] )
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
PhoenixMsg phoenixMsg ->
let
( phoenixModel, phoenixCmd ) =
Phoenix.update PhoenixOutMsg PhoenixMsg phoenixMsg model.phoenix
in
( { model | phoenix = phoenixModel }
, phoenixCmd
)
PhoenixOutMsg outMsg ->
( { model | messages = outMsg :: model.messages }
, Cmd.none
)
ButtonClickResponse response ->
( { model | response = Just response }
, Cmd.none
)
view : Model -> Html Msg
view model =
Html.div []
[ Html.button
[ Html.Events.onClick
(Phoenix.pushMsg PhoenixMsg
{ url = model.phoenixUrl
, topic = "your_channel"
, event = "your_event"
, payload = JE.object []
, onResponse = ButtonClickResponse
}
)
]
[ Html.text "Send push" ]
, case model.response of
Nothing ->
Html.text ""
Just response ->
Html.dl []
[ Html.dt [] [ Html.text "ref" ]
, Html.dd [] [ Html.text response.ref ]
]
]