Commit 61044f769c6cf5ce1fe7c15c6778ab62cfd30313

  • avatar
  • Ville Tirronen <ville.tirronen @j…u.fi> (Committer)
  • Sat Apr 27 10:15:44 EEST 2019
  • avatar
  • Ville Tirronen <ville.tirronen @j…u.fi> (Author)
  • Sat Apr 27 10:15:23 EEST 2019
Made LecturePolls compile with elm 0.19
LecturePolls/Polls.elm
(51 / 91)
  
1010import Json.Encode as Encode
1111import Http
1212import Platform.Sub as Sub
13import Time exposing (Time,second)
13import Time
14import Browser
1415
1516port alert : String -> Cmd msg
1617port gong : Int -> Cmd msg
1919
2020
2121main =
22 Html.programWithFlags
22 Browser.element
2323 { view = view
2424 , update = update
25 , subscriptions = \_ -> Sub.batch [Time.every second (\_ -> Tick)
26 ,Time.every (10*second) (\_ -> UpdateTick)]
25 , subscriptions = \_ -> Sub.batch [Time.every 1000 (\_ -> Tick)
26 ,Time.every (10*1000) (\_ -> UpdateTick)]
2727 , init =
2828 init
2929 -- defFlags
5959
6060init : Flags -> ( Model, Cmd Msg )
6161init =
62 \flags -> Model
62 \flags -> (Model
6363 flags.pollServer
6464 flags.pollName
6565 (case flags.pollQuestion of
7070 False (Array.empty) NotLoggedIn
7171 0 False flags.pollTime
7272 0
73<<<<<<< HEAD
7473 ""
75=======
76>>>>>>> 6b283e1209f68ac38686e058c28dd09aade3d498
77 ! [getUserType SetUserType flags.pollServer
74 , Cmd.batch [getUserType SetUserType flags.pollServer
7875 ,getUserCount SetUserCount flags.pollServer flags.pollName
79 ]
76 ])
8077
8178
8279
9393update : Msg -> Model -> ( Model, Cmd Msg )
9494update msg model =
9595 case msg of
96 StopTimer -> {model|timerOn = False }![]
96 StopTimer -> ({model|timerOn = False },Cmd.none)
9797 ToggleTimer n -> if model.timerOn
98 then {model|timer = 0, timerOn = False}![]
99 else {model|timer = n, timerOn = True }![]
98 then ({model|timer = 0, timerOn = False},Cmd.none)
99 else ({model|timer = n, timerOn = True },Cmd.none)
100100 UpdateTick ->
101101 if model.userType == Lecturer
102 then model![getUserCount SetUserCount model.pollServer model.pollName] else model![]
103 Tick -> {model|timer = if model.timer > 0 && model.timerOn
104 then model.timer - 1 else 0 }!
102 then (model,Cmd.batch [getUserCount SetUserCount model.pollServer model.pollName]) else (model,Cmd.none)
103 Tick -> ({model|timer = if model.timer > 0 && model.timerOn
104 then model.timer - 1 else 0 },
105105 if done model
106 then [gong 1
106 then Cmd.batch [gong 1
107107 , send StopTimer
108108 , send GetTotals ]
109109 else (if critical model
110 then [tick 1]
111 else [])
112 SetUserCount (Result.Ok uc) -> {model|userCount=uc}![]
113<<<<<<< HEAD
114 SetUserCount (Result.Err e) -> {model|message = "Error setting user count"++toString e}![] -- {model|userCount=uc}![syncReveal ""]
115 SetUserType (Result.Ok ut) -> {model|userType=ut}![]
116 SetUserType (Result.Err e) -> {model|message = "Error setting user type "++toString e}![]
117=======
118 SetUserCount (Result.Err e) -> model![] -- {model|userCount=uc}![syncReveal ""]
119 SetUserType (Result.Ok ut) -> {model|userType=ut}![]
120 SetUserType (Result.Err e) -> model![alert ("Error setting user type: "++toString e)]
121>>>>>>> 6b283e1209f68ac38686e058c28dd09aade3d498
110 then tick 1
111 else Cmd.none))
112 SetUserCount (Result.Ok uc) -> ({model|userCount=uc},Cmd.none)
113 SetUserCount (Result.Err e) -> ({model|message = "Error setting user count"++Debug.toString e},Cmd.none)
114 SetUserType (Result.Ok ut) -> ({model|userType=ut},Cmd.none )
115 SetUserType (Result.Err e) -> ({model|message = "Error setting user type "++Debug.toString e},Cmd.none)
122116 Agree idx ->
123 { model | items = setAnswer model.items idx (Just True) } ! []
117 ({ model | items = setAnswer model.items idx (Just True) } , Cmd.none)
124118
125119 Disagree idx ->
126 { model | items = setAnswer model.items idx (Just False) } ! []
120 ({ model | items = setAnswer model.items idx (Just False) } , Cmd.none)
127121
128122 Dunno idx ->
129 { model | items = setAnswer model.items idx (Nothing) } ! []
123 ({ model | items = setAnswer model.items idx (Nothing) } , Cmd.none)
130124
131125 Submit ->
132 model ! [ doSubmit SubmitResult model.pollServer model.pollName (toVotes model.items) ]
126 (model , Cmd.batch [ doSubmit SubmitResult model.pollServer model.pollName (toVotes model.items) ])
133127
134<<<<<<< HEAD
135 SubmitResult (Err x) -> {model|submitted=False,message="Error while submitting answers! Your answers are not recorded ("
136 ++ toString x++")"}![]
137 SubmitResult (Ok ()) -> {model|submitted=True}![]
138 GetTotals -> model![getTotals SetTotals model.pollServer model.pollName]
139 SetTotals (Result.Ok ts) -> {model|totals=ts}![]
140 SetTotals (Result.Err e) -> {model|message= "Error getting totals: "++toString e }![]
141=======
142 SubmitResult (Err x) -> {model|submitted=False}![alert "Something went wrong. Perhaps you need to log in?"]
143 SubmitResult (Ok ()) -> {model|submitted=True}![]
144 GetTotals -> model![getTotals SetTotals model.pollServer model.pollName]
145 SetTotals (Result.Ok ts) -> {model|totals=ts}![]
146 SetTotals (Result.Err e) -> model![alert ("Error getting totals: "++toString e)]
147>>>>>>> 6b283e1209f68ac38686e058c28dd09aade3d498
128 SubmitResult (Err x) -> ({model|submitted=False,message="Error while submitting answers! Your answers are not recorded ("
129 ++ Debug.toString x++")"},Cmd.none)
130 SubmitResult (Ok ()) -> ({model|submitted=True},Cmd.none)
131 GetTotals -> (model,Cmd.batch [getTotals SetTotals model.pollServer model.pollName])
132 SetTotals (Result.Ok ts) -> ({model|totals=ts},Cmd.none)
133 SetTotals (Result.Err e) -> ({model|message= "Error getting totals: "++Debug.toString e },Cmd.none)
148134
149135
150136toVotes =
189189viewLecturer : Model -> Html Msg
190190viewLecturer model =
191191 Html.div [ class "pollSurround"] (
192<<<<<<< HEAD
193192 (if model.message == "" then [] else [Html.span [class "alert"] [text model.message]])
194193 ++
195194 [Markdown.toHtml [class "pollQuestion"] model.pollQuestion
196195 ,Html.div [ class "poll" ]
197=======
198 [Markdown.toHtml [class "pollQuestion"] model.pollQuestion]
199 ++[Html.div [ class "poll" ]
200>>>>>>> 6b283e1209f68ac38686e058c28dd09aade3d498
201196 (
202197 Array.toList (Array.indexedMap (viewItemCounts model.totals) model.items)
203198 ++ [ Html.span [if model.timer<15 && model.timer>0 then class "timer alert" else class "timer"
204 , onClick (ToggleTimer model.pollTime )] [text (toString model.timer++"s")]
205 , Html.span [class "userCount"] [text (toString model.userCount)]
199 , onClick (ToggleTimer model.pollTime )] [text (String.fromInt model.timer++"s")]
200 , Html.span [class "userCount"] [text (String.fromInt model.userCount)]
206201 , Html.button [ class "submitButton", onClick GetTotals ] [ text "Näytä" ] ]
207202 )])
208203
205205 Just (a,b,c) -> viewBar item a b c
206206 Nothing -> Html.div [] [Markdown.toHtml [] item.choice]
207207
208viewBar t a b c = let
209 ratio n = toString (round (95* (toFloat n/(toFloat a+toFloat b+toFloat c))))++"%"
210 in Html.div [class "Bar"]
208viewBar t a b c =
209 let
210 ratio n = String.fromInt (round (95* (toFloat n/(toFloat a+toFloat b+toFloat c)))) ++"%"
211 in Html.div [class "Bar"]
211212 [
212213 Markdown.toHtml [] t.choice
213214 ,Html.span [class "BarComponent isTrue",attribute "style" ("width:"++ratio a++";")]
214 [unless (a==0) (text (toString a))]
215 [unless (a==0) (text (String.fromInt a))]
215216 ,Html.span [class "BarComponent dunno",attribute "style" ("width:"++ratio b++";")]
216 [unless (b==0) (text (toString b))]
217 [unless (b==0) (text (String.fromInt b))]
217218 ,Html.span [class "BarComponent isFalse",attribute "style" ("width:"++ratio c++";")]
218 [unless (c==0) (text (toString c))]
219 [unless (c==0) (text (String.fromInt c))]
219220 ]
220221
221222
222223viewStudent : Model -> Html Msg
223224viewStudent model =
224225 Html.div [ class "pollSurround"] (
225<<<<<<< HEAD
226226 (if model.message == "" then [] else [Html.span [class "alert"] [text model.message]])
227227 ++ [Markdown.toHtml [class "pollQuestion"] model.pollQuestion]
228=======
229 [Markdown.toHtml [class "pollQuestion"] model.pollQuestion]
230>>>>>>> 6b283e1209f68ac38686e058c28dd09aade3d498
231228 ++ [Html.div [ class "poll" ]
232229 (Array.toList (Array.indexedMap (viewItems model.submitted) model.items)
233230 ++ [ Html.button [ class "submitButton", onClick Submit ] [ text "Lähetä vastaus" ] ]
260260
261261
262262getTotals msg pollservant poll =
263 let
264 req =
265 Http.get (pollservant ++ "/totals/"++poll)
266 (Decode.array (Decode.map3 (\a b c -> (a,b,c))
263 Http.get {url = (pollservant ++ "/totals/"++poll)
264 ,expect = Http.expectJson msg (Decode.array (Decode.map3 (\a b c -> (a,b,c))
267265 (Decode.index 0 Decode.int)
268266 (Decode.index 1 Decode.int)
269 (Decode.index 2 Decode.int)))
270 in
271 Http.send msg req
267 (Decode.index 2 Decode.int)))}
272268
273269getUserCount msg pollservant poll =
274 let
275 req =
276 Http.get (pollservant ++ "/answercount/"++poll)
277 (Decode.int)
278 in
279 Http.send msg req
270 Http.get {url = (pollservant ++ "/answercount/"++poll)
271 ,expect = Http.expectJson msg (Decode.int)}
280272
281273
282274getUserType msg pollservant =
283 let
284 req =
285 Http.get (pollservant ++ "/usertype/")
286 (Decode.string |> Decode.andThen (\x -> Decode.succeed (case x of
275 Http.get {url= (pollservant ++ "/usertype/")
276 ,expect = Http.expectJson msg (Decode.string |> Decode.andThen (\x -> Decode.succeed (case x of
287277 "Lecturer" -> Lecturer
288278 "Student" -> Student
289 _ -> NotLoggedIn)))
290 in
291 Http.send msg req
279 _ -> NotLoggedIn)))}
292280
293281
294282doSubmit msg pollservant poll votes =
295 let
296 req =
297 Http.post (pollservant ++ "/submit/" ++ poll)
298 (Http.jsonBody (Encode.array (Array.map Encode.int votes)))
299 (Decode.succeed ())
300 in
301 Http.send msg req
283 Http.post {url = pollservant ++ "/submit/" ++ poll
284 ,body = Http.jsonBody (Encode.array Encode.int votes)
285 ,expect = Http.expectJson msg (Decode.succeed ())}
LecturePolls/elm-package.json
(0 / 20)
  
1{
2 "version": "1.0.0",
3 "summary": "helpful summary of your project, less than 80 characters",
4 "repository": "https://github.com/user/project.git",
5 "license": "BSD3",
6 "source-directories": [
7 "."
8 ],
9 "exposed-modules": [],
10 "dependencies": {
11 "NoRedInk/elm-decode-pipeline": "3.0.0 <= v < 4.0.0",
12 "bartavelle/json-helpers": "1.3.0 <= v < 2.0.0",
13 "elm-lang/core": "5.1.1 <= v < 6.0.0",
14 "elm-lang/dom": "1.1.1 <= v < 2.0.0",
15 "elm-lang/html": "2.0.0 <= v < 3.0.0",
16 "elm-lang/http": "1.0.0 <= v < 2.0.0",
17 "evancz/elm-markdown": "3.0.2 <= v < 4.0.0"
18 },
19 "elm-version": "0.18.0 <= v < 0.19.0"
20}
LecturePolls/elm.json
(30 / 0)
  
1{
2 "type": "application",
3 "source-directories": [
4 "."
5 ],
6 "elm-version": "0.19.0",
7 "dependencies": {
8 "direct": {
9 "NoRedInk/elm-json-decode-pipeline": "1.0.0",
10 "bartavelle/json-helpers": "2.0.2",
11 "elm/browser": "1.0.1",
12 "elm/core": "1.0.2",
13 "elm/html": "1.0.0",
14 "elm/http": "2.0.0",
15 "elm/json": "1.1.3",
16 "elm/time": "1.0.0",
17 "elm-explorations/markdown": "1.0.0"
18 },
19 "indirect": {
20 "elm/bytes": "1.0.8",
21 "elm/file": "1.0.5",
22 "elm/url": "1.0.0",
23 "elm/virtual-dom": "1.0.2"
24 }
25 },
26 "test-dependencies": {
27 "direct": {},
28 "indirect": {}
29 }
30}