From 2c7b72fba6e2a06aa0ace39de7128747d4f3f528 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 13 Dec 2018 14:06:15 -0800 Subject: [PATCH] Open room on notification click. --- src/Main.elm | 3 ++- src/Scylla/Model.elm | 8 ++++++++ src/Scylla/Notification.elm | 2 +- src/Scylla/Views.elm | 2 +- static/js/notifications.js | 5 ++--- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Main.elm b/src/Main.elm index 9a5dbfb..041cce4 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -59,6 +59,7 @@ update msg model = case msg of ChangeLoginPassword p -> ({ model | loginPassword = p }, Cmd.none) AttemptLogin -> (model, Scylla.Http.login model.apiUrl model.loginUsername model.loginPassword) -- TODO TryUrl urlRequest -> updateTryUrl model urlRequest + OpenRoom s -> (model, Nav.pushUrl model.key <| roomUrl s) ChangeRoute r -> ({ model | route = r }, Cmd.none) ReceiveLoginResponse r -> updateLoginResponse model r ReceiveFirstSyncResponse r -> updateSyncResponse model r False @@ -129,7 +130,7 @@ updateSyncResponse model r notify = _ -> (model, syncCmd) subscriptions : Model -> Sub Msg -subscriptions m = Sub.none +subscriptions m = onNotificationClickPort OpenRoom onUrlRequest : Browser.UrlRequest -> Msg onUrlRequest = TryUrl diff --git a/src/Scylla/Model.elm b/src/Scylla/Model.elm index bb8e26a..574a2a6 100644 --- a/src/Scylla/Model.elm +++ b/src/Scylla/Model.elm @@ -5,6 +5,7 @@ import Scylla.Login exposing (LoginResponse, Username, Password) import Scylla.UserData exposing (UserData) import Scylla.Route exposing (Route) import Browser.Navigation as Nav +import Url.Builder import Dict exposing (Dict) import Browser import Http @@ -30,6 +31,7 @@ type Msg = | ChangeLoginPassword Password -- During login screen: the password | AttemptLogin -- During login screen, login button presed | TryUrl Browser.UrlRequest -- User attempts to change URL + | OpenRoom String -- We try open a room | ChangeRoute Route -- URL changes | ChangeRoomText String String -- Change to a room's input text | SendRoomText String -- Sends a message typed into a given room's input @@ -41,3 +43,9 @@ type Msg = displayName : Model -> Username -> String displayName m s = Maybe.withDefault (senderName s) <| Maybe.andThen .displayName <| Dict.get s m.userData + +roomUrl : String -> String +roomUrl s = Url.Builder.absolute [ "room", s ] [] + +loginUrl : String +loginUrl = Url.Builder.absolute [ "login" ] [] diff --git a/src/Scylla/Notification.elm b/src/Scylla/Notification.elm index d4bae8b..0b33aad 100644 --- a/src/Scylla/Notification.elm +++ b/src/Scylla/Notification.elm @@ -8,4 +8,4 @@ type alias Notification = } port sendNotificationPort : Notification -> Cmd msg -port onNotificationClickPort : (Json.Decode.Value -> msg) -> Sub msg +port onNotificationClickPort : (String -> msg) -> Sub msg diff --git a/src/Scylla/Views.elm b/src/Scylla/Views.elm index 03bfcdf..6d754d3 100644 --- a/src/Scylla/Views.elm +++ b/src/Scylla/Views.elm @@ -69,7 +69,7 @@ roomListElementView s jr = let name = Maybe.withDefault "" <| roomName jr in - a [ href <| Url.Builder.absolute [ "room", s ] [] ] [ text name ] + a [ href <| roomUrl s ] [ text name ] loginView : Model -> Html Msg loginView m = div [ class "login-wrapper" ] diff --git a/static/js/notifications.js b/static/js/notifications.js index 8bdd01f..01a52de 100644 --- a/static/js/notifications.js +++ b/static/js/notifications.js @@ -9,9 +9,8 @@ function setupNotificationPorts(app) { } var n = new Notification(data.name, options) n.onclick = function() { - app.ports.onNotificationClickPort.send({ - "room" : data.room - }); + app.ports.onNotificationClickPort.send(data.room); + n.close(); } }) }