/
CapitalsQuizMD.elm
171 lines (152 loc) · 4.55 KB
/
CapitalsQuizMD.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
module Main exposing (..)
import Html.App as App
import Html exposing (..)
import Html.Attributes exposing (href, class, style)
import Material
import Material.Scheme
import Material.Button as Button
import Material.Options exposing (css)
import Material.Textfield as Textfield
import Material.List as Lists
import Http
import Task
import String exposing (..)
import Json.Decode exposing (..)
type Msg
= Answer String
| Check
| FetchSucceed Quiz
| FetchFail Http.Error
| Mdl (Material.Msg Msg)
type alias Quiz =
{ country : String
, capital : String
}
type Status
= None
| Right String
| Wrong String
type alias Model =
{ quiz : Quiz
, answer : String
, statistics : List Status
, status : Status
, mdl : Material.Model
}
type alias Mdl =
Material.Model
view : Model -> Html Msg
view model =
div []
[ h1 [ style [ ( "color", "CadetBlue " ) ] ] [ text "Capitals & Countries <| quiz" ]
, div [ style [ ( "padding", "2rem" ) ] ]
[ score model.statistics
, text ("What is the capital of " ++ model.quiz.country ++ ": ")
, Textfield.render Mdl
[ 2 ]
model.mdl
[ Textfield.label "the answer"
, Textfield.onInput Answer
, Textfield.value model.answer
, Textfield.floatingLabel
, Textfield.text'
]
, Button.render Mdl
[ 1 ]
model.mdl
[ Button.raised
, Button.colored
, Button.onClick Check
, css "margin" "0 24px"
]
[ text "Check" ]
]
, status model.status
, list model.statistics
, div [ style [ ( "color", "white" ) ] ] [ text model.quiz.capital ]
-- cheat
]
|> Material.Scheme.top
score : List Status -> Html a
score stats =
let
winsFun = \it ->
case it of
Right _ -> True
Wrong _ -> False
None -> False
in
let wins = stats |> List.filter winsFun |> List.length
in
let losts = (stats |> List.length) - wins
in
div [] [ text ("(" ++ (toString wins) ++ ":" ++ (toString losts) ++ ")") ]
status : Status -> Html a
status st =
let ( color, txt ) =
case st of
Right s -> ( "green", "Right. " ++ s )
Wrong s -> ( "red", "Wrong! " ++ s )
None -> ( "", "" )
in
div [ style [ ( "color", color ) ] ] [ text txt ]
list : List Status -> Html a
list items =
let
f = \it ->
case it of
Right txt -> ( Lists.icon "done" [], txt )
Wrong txt -> ( Lists.icon "error outline" [], txt)
None -> ( Lists.icon "" [], "" )
in
Lists.ul []
(items
|> List.map f
|> List.map (\( icon, txt ) -> Lists.li [] [ Lists.content [] [ icon, text txt ] ])
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Answer a ->
{ model | answer = a, status = None } ! []
Check ->
let message = "The capital of " ++ model.quiz.country ++ " is " ++ model.quiz.capital
in
let checkStatus =
if toUpper model.answer == toUpper model.quiz.capital then
Right message
else
Wrong message
in
{ model | statistics = checkStatus :: model.statistics, status = checkStatus } ! [ getNewQuiz ]
FetchSucceed q ->
if q.capital == "" then
model ! [ getNewQuiz ]
else
{ model | quiz = q, answer = "" } ! []
FetchFail e ->
model ! []
Mdl msg' ->
Material.update msg' model
getNewQuiz : Cmd Msg
getNewQuiz =
Task.perform FetchFail FetchSucceed (Http.get decode "https://funcxz.azurewebsites.net/api/capitalsquiz")
decode : Decoder Quiz
decode =
object2 Quiz ("Country" := string) ("Capital" := string)
model : Model
model =
{ quiz = (Quiz "" "")
, answer = ""
, statistics = []
, mdl = Material.model
, status = None
}
main : Program Never
main =
App.program
{ init = ( model, getNewQuiz )
, view = view
, subscriptions = always Sub.none
, update = update
}