-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.elm
183 lines (129 loc) · 3.52 KB
/
Main.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
module Main exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Http exposing (..)
import Json.Decode as Json
main : Program Never Model Msg
main =
program
{ init = init
, update = update
, view = view
, subscriptions = \_ -> Sub.none
}
init : ( Model, Cmd Msg )
init =
( initModel, Cmd.none )
-- MODEL
type alias Model =
{ score : Int
, username : String
, view : Page
, quizData : List QuizData
, error : Bool
}
type Page
= StartPage
| QuestionPage
| GameOverPage
type alias QuizData =
{ question : String
, correct_answer : String
, incorrect_answers : List String
}
type Msg
= Username String
| Score Int
| UpdatePage
| GetApiData
| ApiResponse (Result Http.Error (List QuizData))
initModel : Model
initModel =
{ score = 0
, username = ""
, view = StartPage
, quizData =
[ { question = ""
, correct_answer = ""
, incorrect_answers = []
}
]
, error = False
}
-- UPDATE
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Username username ->
( { model | username = username }, Cmd.none )
Score score ->
( { model | score = score }, Cmd.none )
UpdatePage ->
( { model | view = QuestionPage }, Cmd.none )
GetApiData ->
( model, sendHttpRequest )
ApiResponse (Ok quizData) ->
let
log =
Debug.log "Quiz api data: " quizData
in
( { model | quizData = quizData }, Cmd.none )
ApiResponse (Err err) ->
let
log =
Debug.log "Quiz api error:" err
in
( { model | error = True }, Cmd.none )
sendHttpRequest : Cmd Msg
sendHttpRequest =
Http.send ApiResponse <| getRequest
getRequest : Request (List QuizData)
getRequest =
let
url =
"https://opentdb.com/api.php?amount=10&category=9&difficulty=easy&type=multiple"
in
Http.get url apiDecodeList
-- API call returns an object. This function decodes the object accessing the results list
apiDecodeList : Json.Decoder (List QuizData)
apiDecodeList =
Json.at [ "results" ] <|
Json.list apiDecoder
-- Decoding the individual quiz Q&As records from the API call, located inside the results list
apiDecoder : Json.Decoder QuizData
apiDecoder =
Json.map3
QuizData
(Json.at [ "question" ] Json.string)
(Json.at [ "correct_answer" ] Json.string)
(Json.at [ "incorrect_answers" ] (Json.list Json.string))
-- function to switch between pages
changePage : Model -> Html Msg
changePage model =
case model.view of
StartPage ->
section []
[ input [ placeholder "Enter your name", onInput Username ]
[]
, button
[ onClick GetApiData ]
[ text "Play" ]
]
QuestionPage ->
section []
[ p [] [ text "This is the question page" ]
]
GameOverPage ->
section []
[ p [] [ text "This is the gameover page" ]
]
-- VIEW
view : Model -> Html Msg
view model =
div []
[ Html.header []
[ h1 [] [ text "ElmQuiz" ]
]
, (changePage model)
]