From c084ee8e0abe989d8e7caa2727d514b41228f74b Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Wed, 14 Jul 2021 19:43:50 +0200 Subject: [PATCH] participant --- frontend/src/Main.elm | 23 +++++++++++++++++++---- frontend/src/Model.elm | 13 +++++++------ frontend/src/Participant.elm | 26 ++++++++++++++++++++++++++ frontend/src/WsMessage.elm | 22 ++++++---------------- 4 files changed, 58 insertions(+), 26 deletions(-) create mode 100644 frontend/src/Participant.elm diff --git a/frontend/src/Main.elm b/frontend/src/Main.elm index ecece82..3693547 100644 --- a/frontend/src/Main.elm +++ b/frontend/src/Main.elm @@ -158,10 +158,24 @@ update msg model = -- INIT NameChanged name -> - ( { model | userName = name }, Cmd.none ) + let + me = + model.me + + newme = + { me | name = name } + in + ( { model | me = newme }, Cmd.none ) NameGenerated scrambled -> - ( { model | userName = scrambled.prefix ++ " " ++ scrambled.suffix }, Cmd.none ) + let + me = + model.me + + newme = + { me | name = scrambled.prefix ++ " " ++ scrambled.suffix } + in + ( { model | me = newme }, Cmd.none ) RandomizeName -> ( model, randomUserName ) @@ -202,7 +216,7 @@ view model = [ h1 [ class "app-title" ] [ text "Open-Retro" ] , ul [] [ li [] - [ input [ onInput NameChanged, value model.userName ] + [ input [ onInput NameChanged, value model.me.name ] [] ] , li [] @@ -245,4 +259,5 @@ homePage _ = pokerPage : Model -> List (Html Msg) pokerPage _ = - [] + [ ul [] [] + ] diff --git a/frontend/src/Model.elm b/frontend/src/Model.elm index 70ec89f..0b342ce 100644 --- a/frontend/src/Model.elm +++ b/frontend/src/Model.elm @@ -3,6 +3,7 @@ module Model exposing (InitInfo, Model, initialModel) import Browser.Navigation as Nav import Http import Navigation +import Participant exposing (Participant) import Url exposing (Url) @@ -15,9 +16,9 @@ type alias Model = , apiHost : String , route : Navigation.Route , httpError : Maybe Http.Error - , userUuid : Uuid - , userName : String + , me : Participant , session : Maybe String + , participants : List Participant } @@ -37,12 +38,12 @@ initialModel init = init.route -- httpError Maybe.Nothing - -- userUuid - init.userUuid - -- userName - "" + -- me + (Participant init.userUuid "") -- session Maybe.Nothing + -- participants + [] urlToPort : Url -> String diff --git a/frontend/src/Participant.elm b/frontend/src/Participant.elm new file mode 100644 index 0000000..6f10c91 --- /dev/null +++ b/frontend/src/Participant.elm @@ -0,0 +1,26 @@ +module Participant exposing (Participant, decoder) + +import Json.Decode exposing (Decoder, field, map2, string) +import Json.Encode as Enc exposing (object) + + +type alias Participant = + { id : String + , name : String + } + + +decoder : Decoder Participant +decoder = + map2 Participant + (field "id" string) + (field "name" string) + + +encode : Participant -> String +encode participant = + object + [ ( "id", Enc.string participant.id ) + , ( "name", Enc.string participant.name ) + ] + |> Enc.encode 0 diff --git a/frontend/src/WsMessage.elm b/frontend/src/WsMessage.elm index 13b36bd..c781a3e 100644 --- a/frontend/src/WsMessage.elm +++ b/frontend/src/WsMessage.elm @@ -1,9 +1,10 @@ -module WsMessage exposing (Action(..), Publisher, WsMessage, createSession, decode, msgDecoder, strToAction) +module WsMessage exposing (Action(..), WsMessage, createSession, decode, msgDecoder, strToAction) import Dict exposing (Dict) import Json.Decode exposing (Decoder, andThen, decodeString, fail, field, map2, map3, string, succeed) import Json.Encode as Enc import Model exposing (Model) +import Participant as Participant exposing (Participant) type Action @@ -39,15 +40,9 @@ strToAction str = fail ("invalid action '" ++ str ++ "'") -type alias Publisher = - { uuid : String - , name : String - } - - type alias WsMessage = { action : Action - , publisher : Publisher + , publisher : Participant , payload : String } @@ -60,12 +55,7 @@ msgDecoder : Decoder WsMessage msgDecoder = map3 WsMessage (field "action" string |> andThen strToAction) - (field "publisher" - (map2 Publisher - (field "uuid" string) - (field "name" string) - ) - ) + (field "publisher" Participant.decoder) (field "payload" string) @@ -89,8 +79,8 @@ createSession model = [ ( "action", Enc.string (actionToString CreateSession) ) , ( "publisher" , Enc.object - [ ( "uuid", Enc.string model.userUuid ) - , ( "name", Enc.string model.userName ) + [ ( "id", Enc.string model.me.id ) + , ( "name", Enc.string model.me.name ) ] ) , ( "payload", Enc.string "" )