From 7c095a84a0ba75aa8b8cf0367b393197313b8361 Mon Sep 17 00:00:00 2001 From: aqquq <jannik.wurche@student.uni-halle.de> Date: Tue, 2 Jul 2024 18:47:02 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9C=20add=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Main.elm | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/Main.elm b/src/Main.elm index 083c8cd..ff47bff 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -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 -- GitLab