-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.elm
183 lines (141 loc) · 3.57 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import WebSocket
import Json.Encode exposing (..)
import Json.Decode exposing (..)
import Style exposing (..)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ query : String
, results : List User
}
init : (Model, Cmd Msg)
init =
let model =
Model "" []
in update Join model
-- UPDATE
type Msg
= Search String
| Results String
| Join
type alias Send =
{ topic : String
, event : String
, payload : String
, ref : String
}
type alias User =
{ name : Maybe String
, login : String
, avatarUrl : String
, url : String
}
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Join ->
let body = Send "typeahead:public" "phx_join" "typeahead:join" "1"
in
( model
, WebSocket.send "ws://localhost:4000/socket/websocket" (encode body)
)
Search query ->
let body = Send "typeahead:public" "search" query "1"
in
( { model | query = query }
, WebSocket.send "ws://localhost:4000/socket/websocket" (encode body)
)
Results string ->
case decodeString (responseDecoder) string of
Ok results ->
Debug.log "ok"
({ model | results = results }, Cmd.none)
Err err ->
Debug.log "not ok"
Debug.log err
(model, Cmd.none)
encode : Send -> String
encode search =
Json.Encode.object
[ ("topic", Json.Encode.string search.topic )
, ("event", Json.Encode.string search.event )
, ("payload", encodePayload search.payload )
, ("ref", Json.Encode.string search.ref )
]
|> Json.Encode.encode 0
responseDecoder : Decoder (List User)
responseDecoder =
at ["payload", "results"] (Json.Decode.list userDecoder)
userDecoder : Decoder User
userDecoder =
map4
User
(maybe (at ["node", "name"] Json.Decode.string))
(at ["node", "login"] Json.Decode.string)
(at ["node", "avatarUrl"] Json.Decode.string)
(at ["node", "url"] Json.Decode.string)
encodePayload : String -> Json.Encode.Value
encodePayload payload =
Json.Encode.object
[ ("query", Json.Encode.string payload ) ]
-- VIEW
type alias Styles = List ( String, String )
view : Model -> Html Msg
view model =
div []
[ input [type_ "text", placeholder "user", onInput Search ] []
, viewResults model.results
]
viewResults : List User -> Html Msg
viewResults users =
ul []
(List.map (\l -> li [ style userListItem ]
[ a [ href l.url ]
[ img [ src l.avatarUrl, style userThumb ] []
, div [ style userMeta ]
[ h1 [ style userName ] [ maybeText l.name ]
, h2 [ style userLogin ] [ text "@", text l.login ]
]
]
]) users)
maybeText name =
case name of
Just a ->
text a
Maybe.Nothing ->
text ""
userListItem : Styles
userListItem =
[ listStyleType none
]
userThumb : Styles
userThumb =
[ Style.width (pc 15)
]
userMeta : Styles
userMeta =
[ Style.width (pc 85)
, display "inline-block"
]
userName : Styles
userName =
[ fontSize (Style.em 1.5)
]
userLogin : Styles
userLogin =
[ fontSize (Style.em 1)
]
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
WebSocket.listen "ws://localhost:4000/socket/websocket" Results