Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Math Tutor
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jannik Wurche
Math Tutor
Commits
7c095a84
Commit
7c095a84
authored
9 months ago
by
Jannik Wurche
Browse files
Options
Downloads
Patches
Plain Diff
add comments
parent
46bf1c9d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Main.elm
+24
-17
24 additions, 17 deletions
src/Main.elm
with
24 additions
and
17 deletions
src/Main.elm
+
24
−
17
View file @
7c095a84
...
...
@@ -13,10 +13,12 @@ import Time
import
View
-- Ports for communication with JavaScript for Firestore requests and responses.
port
requestFromFirestore
:
String
->
Cmd
msg
port
receiveFromFirestore
:
(
Json
.
Decode
.
Value
->
msg
)
->
Sub
msg
port
receiveFirestoreError
:
(
String
->
msg
)
->
Sub
msg
-- Main entry point for the Elm application.
main
:
Program
()
Model
Msg
main
=
Browser
.
application
...
...
@@ -28,6 +30,7 @@ main =
,
onUrlRequest
=
LinkClicked
}
-- Initialize the model and set up initial state.
init
:
()
->
Url
.
Url
->
Nav
.
Key
->
(
Model
,
Cmd
Msg
)
init
_
url
key
=
(
{
key
=
key
...
...
@@ -40,30 +43,25 @@ init _ url key =
,
runTime
=
False
}
,
Cmd
.
none
)
-- Set up subscriptions to receive messages from Firestore and handle timer ticks.
subscriptions
:
Model
->
Sub
Msg
subscriptions
model
=
if
model
.
runTime
then
Time
.
every
60000
Tick
Time
.
every
60000
Tick
else
Sub
.
batch
[
receiveFromFirestore
(
Json
.
Decode
.
decodeValue
partialQuizDecoder
>>
ReceivePartialQuiz
)
,
receiveFirestoreError
ReceiveError
]
-- Handle the messages and update the model accordingly.
update
:
Msg
->
Model
->
(
Model
,
Cmd
Msg
)
update
msg
model
=
case
msg
of
-- Handle sending a quiz request to Firestore.
SendQuizRequest
->
(
model
,
requestFromFirestore
model
.
quizId
)
ReceiveQuiz
(
Ok
quiz
)
->
(
{
model
|
quiz
=
quiz
,
curTime
=
0
}
,
Nav
.
pushUrl
model
.
key
"
/quiz"
)
ReceiveQuiz
_
->
Debug
.
log
"
Error getting data from Firebase!"
(
model
,
Cmd
.
none
)
-- Handle receiving a partial quiz from Firestore.
ReceivePartialQuiz
(
Ok
partialQuiz
)
->
let
quiz
=
transformPartialQuiz
partialQuiz
...
...
@@ -75,10 +73,12 @@ update msg model =
Debug
.
log
"
Error getting data from Firebase!"
(
model
,
Cmd
.
none
)
-- Handle receiving an error from Firestore.
ReceiveError
errorMessage
->
(
{
model
|
error
=
errorMessage
,
quizId
=
"
"
}
,
Cmd
.
none
)
-- Handle URL navigation within the app.
LinkClicked
urlRequest
->
case
urlRequest
of
Browser
.
Internal
url
->
...
...
@@ -87,31 +87,32 @@ update msg model =
Browser
.
External
href
->
(
model
,
Nav
.
load
href
)
-- Handle URL changes and update the route in the model.
UrlChanged
url
->
let
newRoute
=
getRoute
url
startRunTime
=
case
newRoute
of
QuestionRoute
_
->
True
_
->
False
QuestionRoute
_
->
True
_
->
False
in
(
{
model
|
url
=
url
,
runTime
=
startRunTime
,
route
=
newRoute
}
,
Cmd
.
none
)
-- Handle updating the quiz ID.
UpdateQuizId
id
->
(
{
model
|
quizId
=
id
}
,
Cmd
.
none
)
-- Handle answering a question and updating the model with the new answer.
AnswerQuestion
qid
answer
->
let
q
=
model
.
quiz
newQuestions
=
updateQuestionsAt
qid
answer
model
.
quiz
.
questions
newQuiz
=
{
q
|
questions
=
newQuestions
}
in
(
{
model
|
quiz
=
newQuiz
}
,
Cmd
.
none
)
-- Handle timer tick and update the current time, pushing to result view if time is up.
Tick
_
->
let
newTime
=
model
.
curTime
+
1
...
...
@@ -121,14 +122,16 @@ update msg model =
else
Cmd
.
none
_
->
Cmd
.
none
in
(
{
model
|
curTime
=
(
model
.
curTime
+
1
)
}
,
cmd
)
(
{
model
|
curTime
=
newTime
}
,
cmd
)
-- Handle navigation to a specific question.
GoToQuestion
n
->
let
cmd
=
Nav
.
pushUrl
model
.
key
(
"
/question/"
++
String
.
fromInt
n
)
in
(
model
,
cmd
)
-- Handle navigation to the next question.
GoToNextQuestion
->
let
cmd
=
case
model
.
route
of
...
...
@@ -137,6 +140,7 @@ update msg model =
in
(
model
,
cmd
)
-- Handle navigation to the previous question.
GoToPrevQuestion
->
let
cmd
=
case
model
.
route
of
...
...
@@ -145,12 +149,15 @@ update msg model =
in
(
model
,
cmd
)
-- Handle resetting the quiz ID.
ResetQuizID
->
(
{
model
|
quizId
=
"
"
}
,
Cmd
.
none
)
-- Handle resetting the current time.
ResetCurTime
->
(
{
model
|
curTime
=
0
}
,
Cmd
.
none
)
-- Render the view based on the current model state.
view
:
Model
->
Browser
.
Document
Msg
view
model
=
View
.
view
model
\ No newline at end of file
View
.
view
model
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment