-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelloDBClient.elm
177 lines (143 loc) · 4.43 KB
/
HelloDBClient.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
module HelloDBClient exposing (main)
import Browser exposing (Document)
import Element exposing (..)
import Element.Font as Font
import Element.Input as Input
import Http exposing (Error(..), Response(..))
import Json.Encode
import Person exposing (Person)
main : Program () Model Msg
main =
Browser.document
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
type alias Model =
{ persons : Request (List Person)
}
type Request d
= NotYetRequested
| Loading
| Success d
| Failure Http.Error
init : () -> ( Model, Cmd Msg )
init _ =
( { persons = Loading }
, getPeople
)
getPeople : Cmd Msg
getPeople =
Http.get
{ url = "/persons"
, expect = Http.expectJson GotPersons Person.decodeMany
}
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
type Msg
= NoOp
| GotPersons (Result Http.Error (List Person))
| DeletePerson Person
| PersonDeleted (Result Http.Error Person)
| CreatePerson
| NewPersonAdded (Result Http.Error ())
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model, Cmd.none )
GotPersons response ->
case response of
Ok persons ->
( { model | persons = Success persons }, Cmd.none )
Err err ->
( { model | persons = Failure err }, Cmd.none )
DeletePerson person ->
( model
, Http.request
{ method = "DELETE"
, headers = []
, url =
person
|> Person.getId
|> String.fromInt
|> (++) "/persons/"
, body = Http.emptyBody
, expect = Http.expectWhatever (Result.map (\() -> person) >> PersonDeleted)
, timeout = Nothing
, tracker = Nothing
}
)
PersonDeleted response ->
case ( model.persons, response ) of
( Success persons, Ok deletedPerson ) ->
( { model
| persons =
persons
|> List.filter (Person.getId >> (/=) (Person.getId deletedPerson))
|> Success
}
, Cmd.none
)
_ ->
( model, Cmd.none )
NewPersonAdded response ->
case response of
Ok () ->
( { model | persons = Loading }, getPeople )
Err _ ->
( model, Cmd.none )
CreatePerson ->
( model
, Http.post
{ url = "/persons"
, body =
[ ( "name", Json.Encode.string "Barl" )
, ( "age", Json.Encode.int 55 )
]
|> Json.Encode.object
|> Http.jsonBody
, expect = Http.expectWhatever NewPersonAdded
}
)
view : Model -> Document Msg
view model =
{ title = "Hello DB Client"
, body = [ layout [ width fill, height fill ] (viewBody model) ]
}
viewBody : Model -> Element Msg
viewBody { persons } =
column [ centerX, spacing 16, padding 16 ]
[ el [ Font.size 32, Font.underline ] <|
text "PERSONS"
, column [ spacing 16, centerX ] <|
case persons of
NotYetRequested ->
[ text "You should load the persons" ]
Loading ->
[ text "Loading Persons..." ]
Failure err ->
[ text ("Failed to load the persons: " ++ Debug.toString err) ]
Success people ->
List.map viewPerson people
, Input.button []
{ label = text "Create"
, onPress = Just CreatePerson
}
]
viewPerson : Person -> Element Msg
viewPerson person =
column []
[ text ("Name: " ++ Person.getName person)
, person
|> Person.getAge
|> String.fromInt
|> (++) "Age: "
|> text
, Input.button []
{ label = text "Delete"
, onPress = Just (DeletePerson person)
}
]