/
Main.elm
287 lines (241 loc) · 6.61 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput, onClick)
import Json.Encode as E
import Json.Decode as D
import Http
import Time
import Task
main = Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
type alias Model =
{ draft: String
, posts: List Post
, dark : Bool
, count: Int
, height: Int
, time: Time.Posix
, zone: Time.Zone
, loading: Bool
, user: String
, url: String
, error: String
}
init : () -> (Model, Cmd Msg)
init _ =
let
model =
{ draft = ""
, height = 0
, dark = True
, posts = [emptyPost]
, count = 1
, time = Time.millisToPosix 0
, zone = Time.utc
, loading = False
, user = "grady"
, url = "localhost:3000"
, error = ""
}
in
(
model
, Cmd.batch [Task.perform AdjustTimeZone Time.here, getPosts model]
)
type alias Post =
{ count: Int
, favorite: Bool
, text: String
, time: Time.Posix
}
type alias Posts = List Post
emptyPost : Post
emptyPost =
{ count = 0
, favorite = False
, text = "hi"
, time = Time.millisToPosix 0
}
posixDecoder : D.Decoder Time.Posix
posixDecoder =
D.map (\n -> n |> round |> Time.millisToPosix)
D.float
postDecoder : D.Decoder Post
postDecoder =
D.map4
Post
(D.field "count" D.int)
(D.field "favorite" D.bool)
(D.field "text" D.string)
(D.field "time" posixDecoder)
postsDecoder : D.Decoder Posts
postsDecoder =
D.field "posts" (D.list postDecoder)
userEncode : String -> E.Value
userEncode user = E.object [("user", E.string user)]
postsEncode : Model -> E.Value
postsEncode model =
let
-- posts = List.map postEncode model.posts
posts = E.list postEncode model.posts
in
E.object [ ("posts", posts) ]
postEncode : Post -> E.Value
postEncode post =
E.object
[ ( "count", E.int post.count)
, ( "favorite", E.bool post.favorite)
, ( "text", E.string post.text )
, ( "time", E.int (Time.posixToMillis post.time) )
]
getPosts : Model -> Cmd Msg
getPosts model =
Http.get
{ url = model.url ++ "/user/" ++ model.user
, expect = Http.expectJson GotPosts postsDecoder
}
sendPosts : Model -> Cmd Msg
sendPosts model =
Http.post
{ url = model.url ++ "/user/" ++ model.user
, body = Http.jsonBody (postsEncode model)
, expect = Http.expectJson GotPosts postsDecoder
}
type Msg
= Draft String
| Send
| Tick Time.Posix
| AdjustTimeZone Time.Zone
| DarkMode
| GotPosts (Result Http.Error (List Post))
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Send ->
let
draft = String.trim(model.draft)
post = {
count = model.count
, time = model.time
, text = draft
, favorite = False
}
newModel =
{ model | posts = post :: model.posts
, draft = ""
, count = model.count + 1
, height = 0
}
in
if draft == "" then
(model , Cmd.none)
else
(newModel, sendPosts newModel)
Draft draft ->
({ model | draft = draft, height = String.length draft }, Cmd.none)
Tick time ->
({ model | time = time }, Cmd.none)
AdjustTimeZone zone ->
({ model | zone = zone }, Cmd.none)
DarkMode ->
({ model | dark = not model.dark}, Cmd.none)
GotPosts result ->
case result of
Err e ->
(model, Cmd.none)
-- ({ model | error = errorToString e}, Cmd.none)
Ok posts ->
({ model | posts = posts}, Cmd.none)
errorToString : Http.Error -> String
errorToString err =
case err of
Http.Timeout ->
"Timeout exceeded"
Http.NetworkError ->
"Network error"
Http.BadStatus resp ->
"Bad status"
Http.BadBody bb ->
Debug.log(bb)
"bad bod " ++ bb
Http.BadUrl url ->
"Malformed url: " ++ url
subscriptions : Model -> Sub Msg
subscriptions model =
Time.every 1000 Tick
view : Model -> Html Msg
view model =
main_ [class "main", class (darkMode model)]
[
div [class "container"] [
div [class "errors"] [text model.error]
, header [class "header"] [
h1 [class "large"] [text "Posts"]
, img [src (sunriseSVG model), onClick DarkMode] []
]
, renderPosts model.zone model.posts
, footer [class "footer", style "bottom" (heightStyle model)] [
textarea [class "draft", autofocus True, onInput Draft, value model.draft] []
, button [class "fab", type_ "submit", onClick Send] [text "post"]
]
]
]
renderPosts : Time.Zone -> List Post -> Html Msg
renderPosts zone posts =
posts |> List.map (\p ->
div [ class "post"] [
pre [class "post__header"] [text p.text]
, div [class "post__footer"] [
span [title (mdy zone p.time)] [text (prettyTime zone p.time)]
]
]) |> div [class "posts"]
darkMode : Model -> String
darkMode model = if model.dark then "dark" else ""
sunriseSVG : Model -> String
sunriseSVG model = if model.dark then "sunrise-white.svg" else "sunrise.svg"
heightStyle : Model -> String
heightStyle model = (String.fromInt (model.height // 3) ++ "px")
prettyTime : Time.Zone -> Time.Posix -> String
prettyTime zone time =
let
japan = Time.toWeekday zone time |> toJapaneseWeekday
in
japan
mdy : Time.Zone -> Time.Posix -> String
mdy zone time =
let
month = Time.toMonth zone time |> toMonth
day = Time.toDay zone time |> String.fromInt
year = Time.toYear zone time |> String.fromInt
in
month ++ " " ++ day ++ " " ++ year
toMonth: Time.Month -> String
toMonth month =
case month of
Time.Jan -> "january"
Time.Feb -> "february"
Time.Mar -> "march"
Time.Apr -> "april"
Time.May -> "may"
Time.Jun -> "june"
Time.Jul -> "july"
Time.Aug -> "august"
Time.Sep -> "september"
Time.Oct -> "oktober"
Time.Nov -> "november"
Time.Dec -> "december"
toJapaneseWeekday : Time.Weekday -> String
toJapaneseWeekday weekday =
case weekday of
Time.Mon -> "月"
Time.Tue -> "火"
Time.Wed -> "水"
Time.Thu -> "木"
Time.Fri -> "金"
Time.Sat -> "土"
Time.Sun -> "日"