1
- module Main where
1
+ module Main (..) where
2
2
3
3
import Effects exposing (Effects )
4
- import Html exposing (div , button , text , input , p , h1 )
4
+ import Html exposing (div , button , text , input , p , h1 , h2 , form )
5
5
import Html.Attributes exposing (placeholder , value , type' , class )
6
6
import Html.Events exposing (onClick )
7
7
import StartApp
8
8
import String
9
9
import Task
10
10
import Json.Decode as Json
11
-
12
- import Events exposing (onChange , onEnter )
11
+ import Events exposing (onChange , onEnter , onSubmitPreventDefault )
13
12
import Generated.Api exposing (..)
14
13
15
14
@@ -37,17 +36,23 @@ type alias Model =
37
36
{ books : List Book
38
37
, newBookTitle : String
39
38
, newBookAuthorName : String
40
- , newBookAuthorYearOfBirth : Int }
39
+ , newBookAuthorYearOfBirth : Int
40
+ }
41
+
41
42
43
+ init : ( Model , Effects Action )
44
+ init =
45
+ fetchBooks initModel
42
46
43
- init : (Model , Effects Action )
44
- init = fetchBooks initModel
45
47
46
48
initModel : Model
47
- initModel = { books = []
48
- , newBookTitle = " "
49
- , newBookAuthorName = " "
50
- , newBookAuthorYearOfBirth = 0 }
49
+ initModel =
50
+ { books = []
51
+ , newBookTitle = " "
52
+ , newBookAuthorName = " "
53
+ , newBookAuthorYearOfBirth = 0
54
+ }
55
+
51
56
52
57
type Action
53
58
= FetchBooks
@@ -58,7 +63,7 @@ type Action
58
63
| CreateBook
59
64
60
65
61
- update : Action -> Model -> (Model , Effects Action )
66
+ update : Action -> Model -> ( Model , Effects Action )
62
67
update action model =
63
68
case action of
64
69
FetchBooks ->
@@ -77,83 +82,144 @@ update action model =
77
82
pure { model | newBookAuthorYearOfBirth = year }
78
83
79
84
CreateBook ->
80
- if validate model
81
- then ( { model | newBookTitle = " " , newBookAuthorName = " " }
82
- , postBooks { bookId = Nothing
83
- , title = model. newBookTitle
84
- , author = { name = model. newBookAuthorName
85
- , yearOfBirth = model. newBookAuthorYearOfBirth } }
86
- |> Task . toMaybe
87
- |> Task . map ( \ _ -> FetchBooks )
88
- |> Effects . task
89
- )
90
- else pure model
91
-
92
-
93
- fetchBooks : Model -> (Model , Effects Action )
85
+ if validate model then
86
+ ( { model | newBookTitle = " " , newBookAuthorName = " " , newBookAuthorYearOfBirth = 0 }
87
+ , postBooks
88
+ { bookId = Nothing
89
+ , title = model. newBookTitle
90
+ , author =
91
+ { name = model. newBookAuthorName
92
+ , yearOfBirth = model. newBookAuthorYearOfBirth
93
+ }
94
+ }
95
+ |> Task . toMaybe
96
+ |> Task . map ( \ _ -> FetchBooks )
97
+ |> Effects . task
98
+ )
99
+ else
100
+ pure model
101
+
102
+
103
+ fetchBooks : Model -> ( Model , Effects Action )
94
104
fetchBooks model =
95
105
( model
96
106
, getBooks
97
- |> Task . toMaybe
98
- |> Effects . task
99
- |> Effects . map SetBooks )
107
+ |> Task . toMaybe
108
+ |> Effects . task
109
+ |> Effects . map SetBooks
110
+ )
100
111
101
112
102
113
validate : Model -> Bool
103
- validate { newBookTitle, newBookAuthorName} =
104
- List . all ( not << String . isEmpty) [ newBookTitle, newBookAuthorName]
114
+ validate { newBookTitle, newBookAuthorName } =
115
+ List . all ( not << String . isEmpty) [ newBookTitle, newBookAuthorName ]
105
116
106
117
107
118
view : Signal .Address Action -> Model -> Html .Html
108
119
view address model =
109
- div [ class " container-fluid" ]
110
- [ h1 [] [ text " Books" ]
120
+ div
121
+ [ class " container-fluid" ]
122
+ [ h1 [] [ text " Books" ]
111
123
, viewBookForm address model
112
- , button [ onClick address FetchBooks ] [ text " refresh" ]
113
- , div [ class " row" ] ( List . map viewBook model. books)
124
+ , viewBookList address model
125
+ ]
126
+
127
+
128
+ viewBookList address model =
129
+ div
130
+ []
131
+ [ h2 [] [ text " All books" ]
132
+ , div
133
+ [ class " row" ]
134
+ ( List . map viewBook model. books)
135
+ , button
136
+ [ onClick address FetchBooks
137
+ , class " btn btn-default"
138
+ ]
139
+ [ text " Refresh book list" ]
114
140
]
115
141
116
142
117
143
viewBookForm : Signal .Address Action -> Model -> Html .Html
118
144
viewBookForm address model =
119
- div [ class " row" ]
120
- [ div [ class " col-lg-12" ]
121
- [ input
122
- [ placeholder " Title"
123
- , value model. newBookTitle
124
- , onChange address SetNewBookTitle
125
- , onEnter address CreateBook ] []
126
- , input
127
- [ placeholder " Author"
128
- , value model. newBookAuthorName
129
- , onChange address SetNewBookAuthorName
130
- , onEnter address CreateBook ] []
131
- , input
132
- [ placeholder " Date of birth"
133
- , value ( toString model. newBookAuthorYearOfBirth)
134
- , type' " number"
135
- , onChange address ( SetNewBookAuthorYearOfBirth << Maybe . withDefault 0 << Result . toMaybe << String . toInt)
136
- , onEnter address CreateBook ] []
145
+ div
146
+ [ class " row" ]
147
+ [ div
148
+ [ class " col-lg-12" ]
149
+ [ h2 [] [ text " Create a book" ]
150
+ , form
151
+ [ class " form-inline"
152
+ , onSubmitPreventDefault address CreateBook
153
+ ]
154
+ [ div
155
+ [ class " form-group" ]
156
+ [ input
157
+ [ placeholder " Title"
158
+ , class " form-control"
159
+ , value model. newBookTitle
160
+ , onChange address SetNewBookTitle
161
+ ]
162
+ []
163
+ ]
164
+ , div
165
+ [ class " form-group" ]
166
+ [ input
167
+ [ placeholder " Author"
168
+ , class " form-control"
169
+ , value model. newBookAuthorName
170
+ , onChange address SetNewBookAuthorName
171
+ ]
172
+ []
173
+ ]
174
+ , div
175
+ [ class " form-group" ]
176
+ [ input
177
+ [ placeholder " Date of birth"
178
+ , class " form-control"
179
+ , value ( toString model. newBookAuthorYearOfBirth)
180
+ , type' " number"
181
+ , onChange address ( SetNewBookAuthorYearOfBirth << Maybe . withDefault 0 << Result . toMaybe << String . toInt)
182
+ ]
183
+ []
184
+ ]
185
+ , button
186
+ [ type' " submit"
187
+ , class " btn btn-default"
188
+ ]
189
+ [ text " Create book" ]
190
+ ]
137
191
]
138
192
]
139
193
140
194
141
195
viewBook : Book -> Html .Html
142
196
viewBook book =
143
- div [ class " col-lg-3" ]
144
- [ div [ class " panel panel-default" ]
145
- [ div [ class " panel-heading" ]
146
- [ text book. title]
147
- , div [ class " panel-body" ]
148
- [ p []
149
- [ text
150
- ( book. author. name
151
- ++ " (b." ++ toString book. author. yearOfBirth ++ " )"
152
- ++ " {" ++ Maybe . withDefault " unknown" book. bookId ++ " }" ) ]
197
+ div
198
+ [ class " col-lg-3" ]
199
+ [ div
200
+ [ class " panel panel-default" ]
201
+ [ div
202
+ [ class " panel-heading" ]
203
+ [ text book. title ]
204
+ , div
205
+ [ class " panel-body" ]
206
+ [ p
207
+ []
208
+ [ text
209
+ ( book. author. name
210
+ ++ " (b."
211
+ ++ toString book. author. yearOfBirth
212
+ ++ " )"
213
+ ++ " {"
214
+ ++ Maybe . withDefault " unknown" book. bookId
215
+ ++ " }"
216
+ )
217
+ ]
153
218
]
154
219
]
155
220
]
156
221
157
222
158
- pure : a -> (a , Effects b )
159
- pure model = ( model, Effects . none)
223
+ pure : a -> ( a , Effects b )
224
+ pure model =
225
+ ( model, Effects . none )
0 commit comments