-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD_ListAddCheckDelete.elm
371 lines (309 loc) · 10 KB
/
D_ListAddCheckDelete.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
module D_ListAddCheckDelete exposing (..)
import Api.Mutation as Mutation
import Api.Object exposing (Todos_row)
import Api.Object.Todos_mutation_response
import Api.Object.Todos_row as Todos_row
import Api.Query as Query
import Api.Scalar exposing (Id(..))
import Browser
import Graphql.Http
import Graphql.OptionalArgument exposing (OptionalArgument(..))
import Graphql.SelectionSet as SelectionSet exposing (SelectionSet)
import Html exposing (button, div, form, h1, input, p, span, text)
import Html.Attributes exposing (checked, disabled, style, type_, value)
import Html.Events exposing (onCheck, onClick, onInput, onSubmit)
import Json.Decode
import List exposing (map)
import RemoteData exposing (RemoteData(..))
dbId : String
dbId = -- TODO
graphqlApiUrl : String
graphqlApiUrl =
"https://www.airsequel.com/dbs/" ++ dbId ++ "/graphql"
type alias TodoItem =
{ rowid : Maybe Int
, title : String
, completed : Maybe Bool
}
type Msg
= GotTasksResponse
(RemoteData
(Graphql.Http.Error (List TodoItem))
(List TodoItem)
)
| NewTask String
| AddTask
| InsertAffectedRowsResponse (RemoteData (Graphql.Http.Error Int) Int)
| SetCompleted Int Bool
| CompleteAffectedRowsResponse (RemoteData (Graphql.Http.Error Int) Int)
| DeleteTask Int
| DeleteAffectedRowsResponse (RemoteData (Graphql.Http.Error Int) Int)
type alias Model =
{ remoteTodos :
RemoteData
(Graphql.Http.Error (List TodoItem))
(List TodoItem)
, newTask : String
, submissionStatus : RemoteData (Graphql.Http.Error Int) Int
}
type alias Flags =
()
viewError : Graphql.Http.Error a -> Html.Html Msg
viewError error =
case error of
Graphql.Http.GraphqlError _ graphqlErrors ->
div []
(graphqlErrors
|> List.map (\gqlError -> p [] [ text gqlError.message ])
)
Graphql.Http.HttpError httpError ->
case httpError of
Graphql.Http.BadUrl url ->
p [] [ text <| "Bad URL: " ++ url ]
Graphql.Http.BadStatus response body ->
p []
[ text <|
"Bad status (code "
++ String.fromInt response.statusCode
++ "): "
++ body
]
Graphql.Http.NetworkError ->
p [] [ text <| "Network error" ]
Graphql.Http.Timeout ->
p [] [ text "Timeout" ]
Graphql.Http.BadPayload response ->
p []
[ text <|
"Bad payload: "
++ Json.Decode.errorToString response
]
viewTodo : TodoItem -> Html.Html Msg
viewTodo todo =
p []
[ input
[ type_ "checkbox"
, checked
(case todo.completed of
Just True ->
True
_ ->
False
)
, case todo.rowid of
Nothing ->
disabled True
Just rowid ->
onCheck (SetCompleted rowid)
]
[]
, span [] [ text todo.title ]
, button
[ style "margin-left" "1em"
, style "cursor" "pointer"
, case todo.rowid of
Nothing ->
disabled True
Just rowid ->
onClick (DeleteTask rowid)
]
[ text "x" ]
]
view : Model -> Browser.Document Msg
view model =
{ title = "Todo App"
, body =
[ div
[ style "font-family" "sans-serif"
, style "max-width" "20em"
, style "margin" "0 auto"
]
[ h1 [] [ text "Todos" ]
, case model.remoteTodos of
NotAsked ->
p [] [ text "Initializing …" ]
Loading ->
p [] [ text "Loading …" ]
Success todos ->
div [] (todos |> map viewTodo)
Failure error ->
viewError error
, let
inputForm =
form [ onSubmit AddTask ]
[ input
[ type_ "text"
, onInput NewTask
, value model.newTask
]
[]
, input
[ type_ "submit"
, if model.newTask == "" then
disabled True
else
disabled False
]
[]
]
in
case model.submissionStatus of
NotAsked ->
inputForm
Loading ->
p [] [ text "Submitting …" ]
Failure error ->
viewError error
Success _ ->
inputForm
]
]
}
todosSelection : SelectionSet TodoItem Todos_row
todosSelection =
SelectionSet.map3 TodoItem
Todos_row.rowid
Todos_row.title
Todos_row.completed
getTodos : Cmd Msg
getTodos =
Query.todos identity todosSelection
|> Graphql.Http.queryRequest graphqlApiUrl
|> Graphql.Http.send (RemoteData.fromResult >> GotTasksResponse)
insertTodo : String -> Cmd Msg
insertTodo title =
Mutation.insert_todos
{ objects =
[ { rowid = Null
, title = title
, completed = Present False
}
]
}
Api.Object.Todos_mutation_response.affected_rows
|> Graphql.Http.mutationRequest graphqlApiUrl
|> Graphql.Http.send
(RemoteData.fromResult >> InsertAffectedRowsResponse)
boolToString : Bool -> String
boolToString value =
if value then
"true"
else
"false"
setTodoCompleted : Int -> Bool -> Cmd Msg
setTodoCompleted rowid value =
Mutation.update_todos
{ filter =
{ rowid = Present { eq = Present rowid }
, title = Absent
, completed = Absent
}
, set =
{ rowid = Absent
, title = Absent
, completed = Present value
}
}
Api.Object.Todos_mutation_response.affected_rows
|> Graphql.Http.mutationRequest graphqlApiUrl
|> Graphql.Http.send
(RemoteData.fromResult >> CompleteAffectedRowsResponse)
deleteTodo : Int -> Cmd Msg
deleteTodo rowid =
Mutation.delete_todos
{ filter =
{ rowid = Present { eq = Present rowid }
, title = Absent
, completed = Absent
}
}
Api.Object.Todos_mutation_response.affected_rows
|> Graphql.Http.mutationRequest graphqlApiUrl
|> Graphql.Http.send
(RemoteData.fromResult >> DeleteAffectedRowsResponse)
init : Flags -> ( Model, Cmd Msg )
init _ =
( { remoteTodos = RemoteData.Loading
, newTask = ""
, submissionStatus = RemoteData.NotAsked
}
, getTodos
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GotTasksResponse response ->
( { model | remoteTodos = response }, Cmd.none )
AddTask ->
( { model | submissionStatus = RemoteData.Loading }
, if model.newTask /= "" then
insertTodo model.newTask
else
Cmd.none
)
NewTask title ->
( { model | newTask = title }, Cmd.none )
InsertAffectedRowsResponse response ->
( { model
| remoteTodos =
RemoteData.map
(\todos ->
todos
++ [ { rowid = Nothing
, title = "Loading …"
, completed = Nothing
}
]
)
model.remoteTodos
, submissionStatus = response
}
, getTodos
)
SetCompleted rowid value ->
( { model
| remoteTodos =
RemoteData.map
(\todos ->
List.map
(\todo ->
if todo.rowid == Just rowid then
{ todo | completed = Just value }
else
todo
)
todos
)
model.remoteTodos
}
, setTodoCompleted rowid value
)
CompleteAffectedRowsResponse response ->
( { model | submissionStatus = response }
, getTodos
)
DeleteTask rowid ->
( { model
| remoteTodos =
RemoteData.map
(\todos ->
List.filter
(\todo -> todo.rowid /= Just rowid)
todos
)
model.remoteTodos
}
, deleteTodo rowid
)
DeleteAffectedRowsResponse response ->
( { model | submissionStatus = response }
, getTodos
)
main : Platform.Program Flags Model Msg
main =
Browser.document
{ init = init
, update = update
, subscriptions = \_ -> Sub.none
, view = view
}