From 676bf9936b9b51e24979657d50d8f019b2f64ac2 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Wed, 29 Jul 2020 17:19:07 +0200 Subject: Parse room ID --- client/elm.json | 4 ++-- client/src/Client.elm | 35 ++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) (limited to 'client') diff --git a/client/elm.json b/client/elm.json index dea3450..4fdc946 100644 --- a/client/elm.json +++ b/client/elm.json @@ -8,12 +8,12 @@ "direct": { "elm/browser": "1.0.2", "elm/core": "1.0.5", - "elm/html": "1.0.0" + "elm/html": "1.0.0", + "elm/url": "1.0.0" }, "indirect": { "elm/json": "1.1.3", "elm/time": "1.0.0", - "elm/url": "1.0.0", "elm/virtual-dom": "1.0.2" } }, diff --git a/client/src/Client.elm b/client/src/Client.elm index 77168c1..cc144e2 100644 --- a/client/src/Client.elm +++ b/client/src/Client.elm @@ -1,20 +1,41 @@ module Client exposing (main) import Browser -import Html +import Html exposing (Html) +import Url exposing (Url) type Message = Ignore -main : Program () () Message +type Model + = Error String + | JoinRoom + { id : String + } + +parseRoomId : Url -> Result String String +parseRoomId url = case String.split "/" url.path of + _ :: "rooms" :: roomId :: _ -> Ok roomId + _ -> Err <| "Invalid path: " ++ url.path + +view : Model -> List (Html Message) +view model = case model of + Error str -> + [ Html.h1 [] [Html.text "Error"] + , Html.p [] [Html.text str] + ] + JoinRoom room -> + [ Html.h1 [] [Html.text <| "Room " ++ room.id] + ] + +main : Program () Model Message main = Browser.application - { init = \() url key -> ((), Cmd.none) + { init = \() url key -> case parseRoomId url of + Err str -> (Error <| "Could not parse room ID: " ++ str, Cmd.none) + Ok roomId -> (JoinRoom {id = roomId}, Cmd.none) , update = \_ model -> (model, Cmd.none) , subscriptions = \_ -> Sub.none - , view = \model -> - { title = "Client" - , body = [Html.h1 [] [Html.text "Hi"]] - } + , view = \model -> {title = "Client", body = view model} , onUrlChange = \url -> Ignore , onUrlRequest = \urlRequest -> Ignore } -- cgit v1.2.3