Skip to content

Commit 0a11d35

Browse files
committed
Run elm-format; layout tweaks
1 parent 270e6ac commit 0a11d35

File tree

2 files changed

+148
-72
lines changed

2 files changed

+148
-72
lines changed

frontend/src/Events.elm

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
module Events
2-
(onChange, onEnter)
3-
where
1+
module Events (onChange, onEnter, onSubmitPreventDefault) where
42

53
{-| Extensions to the Html.Events library.
64
@@ -15,16 +13,28 @@ import Signal exposing (..)
1513

1614
onEnter : Address a -> a -> Attribute
1715
onEnter address value =
18-
on "keydown"
19-
(Json.customDecoder keyCode is13)
20-
(\_ -> Signal.message address value)
16+
on
17+
"keydown"
18+
(Json.customDecoder keyCode is13)
19+
(\_ -> Signal.message address value)
2120

2221

2322
is13 : Int -> Result String ()
2423
is13 code =
25-
if code == 13 then Ok () else Err "not the right key code"
24+
if code == 13 then
25+
Ok ()
26+
else
27+
Err "not the right key code"
2628

2729

2830
onChange : Address a -> (String -> a) -> Html.Attribute
2931
onChange address f =
3032
on "input" targetValue (message (forwardTo address f))
33+
34+
35+
onSubmitPreventDefault address message =
36+
onWithOptions
37+
"submit"
38+
{ defaultOptions | preventDefault = True }
39+
Json.value
40+
(\_ -> Signal.message address message)

frontend/src/Main.elm

+131-65
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
module Main where
1+
module Main (..) where
22

33
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)
55
import Html.Attributes exposing (placeholder, value, type', class)
66
import Html.Events exposing (onClick)
77
import StartApp
88
import String
99
import Task
1010
import Json.Decode as Json
11-
12-
import Events exposing (onChange, onEnter)
11+
import Events exposing (onChange, onEnter, onSubmitPreventDefault)
1312
import Generated.Api exposing (..)
1413

1514

@@ -37,17 +36,23 @@ type alias Model =
3736
{ books : List Book
3837
, newBookTitle : String
3938
, newBookAuthorName : String
40-
, newBookAuthorYearOfBirth : Int }
39+
, newBookAuthorYearOfBirth : Int
40+
}
41+
4142

43+
init : ( Model, Effects Action )
44+
init =
45+
fetchBooks initModel
4246

43-
init : (Model, Effects Action)
44-
init = fetchBooks initModel
4547

4648
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+
5156

5257
type Action
5358
= FetchBooks
@@ -58,7 +63,7 @@ type Action
5863
| CreateBook
5964

6065

61-
update : Action -> Model -> (Model, Effects Action)
66+
update : Action -> Model -> ( Model, Effects Action )
6267
update action model =
6368
case action of
6469
FetchBooks ->
@@ -77,83 +82,144 @@ update action model =
7782
pure { model | newBookAuthorYearOfBirth = year }
7883

7984
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 )
94104
fetchBooks model =
95105
( model
96106
, getBooks
97-
|> Task.toMaybe
98-
|> Effects.task
99-
|> Effects.map SetBooks )
107+
|> Task.toMaybe
108+
|> Effects.task
109+
|> Effects.map SetBooks
110+
)
100111

101112

102113
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 ]
105116

106117

107118
view : Signal.Address Action -> Model -> Html.Html
108119
view address model =
109-
div [class "container-fluid"]
110-
[ h1 [] [text "Books"]
120+
div
121+
[ class "container-fluid" ]
122+
[ h1 [] [ text "Books" ]
111123
, 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" ]
114140
]
115141

116142

117143
viewBookForm : Signal.Address Action -> Model -> Html.Html
118144
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+
]
137191
]
138192
]
139193

140194

141195
viewBook : Book -> Html.Html
142196
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+
]
153218
]
154219
]
155220
]
156221

157222

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

Comments
 (0)