From a0ed73d09b6997c7b96edcc304cb5c75c7da4ba2 Mon Sep 17 00:00:00 2001 From: Darko Draskovic Date: Wed, 13 Mar 2019 13:58:03 +0100 Subject: [PATCH] Replace crossOrigin with relative path and fix messaging bug (#645) Signed-off-by: Darko Draskovic --- ui/README.md | 2 +- ui/src/Channel.elm | 43 ++++++++++--------------------------------- ui/src/Connection.elm | 13 ++++--------- ui/src/HttpMF.elm | 12 +++++++++++- ui/src/Message.elm | 19 +++++-------------- ui/src/Thing.elm | 25 ++++++++----------------- ui/src/User.elm | 13 +++---------- ui/src/Version.elm | 9 ++------- 8 files changed, 44 insertions(+), 92 deletions(-) diff --git a/ui/README.md b/ui/README.md index d9e3bbd6..a83013b1 100644 --- a/ui/README.md +++ b/ui/README.md @@ -54,7 +54,7 @@ if you want to launch just GUI. ### Contribute to the GUI development -Install GUI as a part of Mainflux build or as a a standalone native GUI and run +Install GUI as a part of Mainflux build or as a standalone native GUI and run it. Launch Mainflux without ui service, either natively or as a Docker composition. Follow the guidelines for Mainflux contributors found here https://mainflux.readthedocs.io/en/latest/CONTRIBUTING/. diff --git a/ui/src/Channel.elm b/ui/src/Channel.elm index ebdb7625..6fd12bba 100644 --- a/ui/src/Channel.elm +++ b/ui/src/Channel.elm @@ -24,20 +24,13 @@ import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (onClick) import Http -import HttpMF +import HttpMF exposing (path) import Json.Decode as D import Json.Encode as E import ModalMF import Url.Builder as B -url = - { base = "http://localhost" - , thingsPath = [ "things" ] - , channelsPath = [ "channels" ] - } - - query = { offset = 0 , limit = 10 @@ -130,7 +123,7 @@ update msg model token = ProvisionChannel -> ( resetEdit model , HttpMF.provision - (B.crossOrigin url.base url.channelsPath []) + (B.relative [ path.channels ] []) token { emptyChannel | name = Just model.name @@ -167,7 +160,7 @@ update msg model token = UpdateChannel -> ( resetEdit { model | editMode = False } , HttpMF.update - (B.crossOrigin url.base (List.append url.channelsPath [ model.channel.id ]) []) + (B.relative [ path.channels, model.channel.id ] []) token { emptyChannel | name = Just model.name @@ -188,7 +181,7 @@ update msg model token = RetrieveChannel channelid -> ( model , HttpMF.retrieve - (B.crossOrigin url.base (List.append url.channelsPath [ channelid ]) []) + (B.relative [ path.channels, channelid ] []) token RetrievedChannel channelDecoder @@ -205,10 +198,7 @@ update msg model token = RetrieveChannels -> ( model , HttpMF.retrieve - (B.crossOrigin url.base - url.channelsPath - (Helpers.buildQueryParamList model.offset model.limit) - ) + (B.relative [ path.channels ] (Helpers.buildQueryParamList model.offset model.limit)) token RetrievedChannels channelsDecoder @@ -217,10 +207,7 @@ update msg model token = RetrieveChannelsForThing thingid -> ( model , HttpMF.retrieve - (B.crossOrigin url.base - (url.thingsPath ++ [ thingid ] ++ url.channelsPath) - (Helpers.buildQueryParamList model.offset model.limit) - ) + (B.relative [ path.things, thingid, path.channels ] (Helpers.buildQueryParamList model.offset model.limit)) token RetrievedChannels channelsDecoder @@ -237,7 +224,7 @@ update msg model token = RemoveChannel id -> ( resetEdit model , HttpMF.remove - (B.crossOrigin url.base (List.append url.channelsPath [ id ]) []) + (B.relative [ path.channels, id ] []) token RemovedChannel ) @@ -446,15 +433,12 @@ updateChannelList model token = ( model , Cmd.batch [ HttpMF.retrieve - (B.crossOrigin url.base - url.channelsPath - (Helpers.buildQueryParamList model.offset model.limit) - ) + (B.relative [ path.channels ] (Helpers.buildQueryParamList model.offset model.limit)) token RetrievedChannels channelsDecoder , HttpMF.retrieve - (B.crossOrigin url.base (List.append url.channelsPath [ model.channel.id ]) []) + (B.relative [ path.channels, model.channel.id ] []) token RetrievedChannel channelDecoder @@ -466,15 +450,8 @@ updateChannelListForThing : Model -> String -> String -> ( Model, Cmd Msg ) updateChannelListForThing model token thingid = ( model , HttpMF.retrieve - (buildUrl (url.thingsPath ++ [ thingid ] ++ url.channelsPath) model.offset model.limit) + (B.relative [ path.things, thingid, path.channels ] (Helpers.buildQueryParamList model.offset model.limit)) token RetrievedChannels channelsDecoder ) - - -buildUrl : List String -> Int -> Int -> String -buildUrl path offset limit = - B.crossOrigin url.base - path - (Helpers.buildQueryParamList offset limit) diff --git a/ui/src/Connection.elm b/ui/src/Connection.elm index c0ace785..090bd66a 100644 --- a/ui/src/Connection.elm +++ b/ui/src/Connection.elm @@ -24,17 +24,12 @@ import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (onClick) import Http -import HttpMF +import HttpMF exposing (path) import List.Extra import Thing import Url.Builder as B -url = - { base = "http://localhost" - } - - type alias Model = { response : String , things : Thing.Model @@ -208,11 +203,11 @@ connect checkedThingsIds checkedChannelsIds method token = List.foldr (++) [] (List.map - (\thingId -> + (\thingid -> List.map - (\channelId -> + (\channelid -> HttpMF.request - (B.crossOrigin url.base [ "channels", channelId, "things", thingId ] []) + (B.relative [ path.channels, channelid, path.things, thingid ] []) method token Http.emptyBody diff --git a/ui/src/HttpMF.elm b/ui/src/HttpMF.elm index b2e0f28b..9dd9f73d 100644 --- a/ui/src/HttpMF.elm +++ b/ui/src/HttpMF.elm @@ -4,7 +4,7 @@ -- SPDX-License-Identifier: Apache-2.0 -module HttpMF exposing (expectID, expectRetrieve, expectStatus, provision, remove, request, retrieve, update, url) +module HttpMF exposing (expectID, expectRetrieve, expectStatus, path, provision, remove, request, retrieve, update, url) import Dict import Helpers @@ -19,6 +19,16 @@ url = } +path = + { users = "users" + , tokens = "tokens" + , things = "things" + , channels = "channels" + , messages = "messages" + , version = "version" + } + + -- EXPECT diff --git a/ui/src/Message.elm b/ui/src/Message.elm index 212aad18..8932ab8a 100644 --- a/ui/src/Message.elm +++ b/ui/src/Message.elm @@ -23,21 +23,12 @@ import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (onClick) import Http -import HttpMF +import HttpMF exposing (path) import List.Extra import Thing import Url.Builder as B -url = - { base = "http://localhost" - , httpPath = [ "http" ] - , thingsPath = [ "things" ] - , channelsPath = [ "channels" ] - , messagesPath = [ "messages" ] - } - - type alias Model = { message : String , thingkey : String @@ -81,7 +72,7 @@ update msg model token = ( { model | message = "", thingkey = "", response = "", thingid = "" } , Cmd.batch (List.map - (\channelId -> send channelId token model.message) + (\channelid -> send channelid model.thingkey model.message) model.checkedChannelsIds ) ) @@ -224,10 +215,10 @@ genChannelRows checkedChannelsIds channels = send : String -> String -> String -> Cmd Msg -send channelId token message = +send channelid thingkey message = HttpMF.request - (B.crossOrigin url.base (url.httpPath ++ url.channelsPath ++ [ channelId ] ++ url.messagesPath) []) + (B.relative [ "http", path.channels, channelid, path.messages ] []) "POST" - token + thingkey (Http.stringBody "application/json" message) SentMessage diff --git a/ui/src/Thing.elm b/ui/src/Thing.elm index aa43108e..b2fcd34b 100644 --- a/ui/src/Thing.elm +++ b/ui/src/Thing.elm @@ -27,7 +27,7 @@ import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (onClick) import Http -import HttpMF +import HttpMF exposing (path) import Json.Decode as D import Json.Encode as E import ModalMF @@ -40,12 +40,6 @@ query = } -url = - { base = "http://localhost" - , path = [ "things" ] - } - - defaultType = "app" @@ -149,7 +143,7 @@ update msg model token = ProvisionThing -> ( resetEdit model , HttpMF.provision - (B.crossOrigin url.base url.path []) + (B.relative [ path.things ] []) token { emptyThing | name = Just model.name @@ -187,7 +181,7 @@ update msg model token = UpdateThing -> ( resetEdit { model | editMode = False } , HttpMF.update - (B.crossOrigin url.base (List.append url.path [ model.thing.id ]) []) + (B.relative [ path.things, model.thing.id ] []) token { emptyThing | name = Just model.name @@ -209,7 +203,7 @@ update msg model token = RetrieveThing thingid -> ( model , HttpMF.retrieve - (B.crossOrigin url.base (List.append url.path [ thingid ]) []) + (B.relative [ path.things, thingid ] []) token RetrievedThing thingDecoder @@ -226,7 +220,7 @@ update msg model token = RetrieveThings -> ( model , HttpMF.retrieve - (B.crossOrigin url.base url.path (Helpers.buildQueryParamList model.offset model.limit)) + (B.relative [ path.things ] (Helpers.buildQueryParamList model.offset model.limit)) token RetrievedThings thingsDecoder @@ -243,7 +237,7 @@ update msg model token = RemoveThing id -> ( model , HttpMF.remove - (B.crossOrigin url.base (List.append url.path [ id ]) []) + (B.relative [ path.things, id ] []) token RemovedThing ) @@ -497,15 +491,12 @@ updateThingList model token = ( model , Cmd.batch [ HttpMF.retrieve - (B.crossOrigin url.base - url.path - (Helpers.buildQueryParamList model.offset model.limit) - ) + (B.relative [ path.things ] (Helpers.buildQueryParamList model.offset model.limit)) token RetrievedThings thingsDecoder , HttpMF.retrieve - (B.crossOrigin url.base (List.append url.path [ model.thing.id ]) []) + (B.relative [ path.things, model.thing.id ] []) token RetrievedThing thingDecoder diff --git a/ui/src/User.elm b/ui/src/User.elm index d99030cd..667b9a45 100644 --- a/ui/src/User.elm +++ b/ui/src/User.elm @@ -19,19 +19,12 @@ import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (onClick) import Http -import HttpMF +import HttpMF exposing (path) import Json.Decode as D import Json.Encode as E import Url.Builder as B -url = - { base = "http://localhost" - , usersPath = [ "users" ] - , tokensPath = [ "tokens" ] - } - - type alias Model = { email : String , password : String @@ -76,7 +69,7 @@ update msg model = , create model.email model.password - (B.crossOrigin url.base url.usersPath []) + (B.relative [ path.users ] []) ) Created result -> @@ -92,7 +85,7 @@ update msg model = , getToken model.email model.password - (B.crossOrigin url.base url.tokensPath []) + (B.relative [ path.tokens ] []) ) GotToken result -> diff --git a/ui/src/Version.elm b/ui/src/Version.elm index 79ea9852..bcfba25f 100644 --- a/ui/src/Version.elm +++ b/ui/src/Version.elm @@ -10,17 +10,12 @@ import Error import Html exposing (..) import Html.Attributes exposing (..) import Http +import HttpMF exposing (path) import Json.Decode as D import Json.Encode as E import Url.Builder as B -url = - { base = "http://localhost" - , path = [ "version" ] - } - - type alias Model = { version : String } @@ -41,7 +36,7 @@ update msg model = GetVersion -> ( model , Http.get - { url = B.crossOrigin url.base url.path [] + { url = B.relative [ path.version ] [] , expect = Http.expectJson GotVersion (D.field "version" D.string) } )