dashboard/src/Update.elm

180 lines
5.5 KiB
Elm
Raw Normal View History

2020-11-23 11:07:07 +03:00
module Update exposing (update)
{-| Copyright 2020 Fluence Labs Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}
2020-11-23 15:44:45 +03:00
import Browser
import Browser.Navigation as Nav
2020-11-23 17:39:32 +03:00
import Dict
import Json.Decode exposing (decodeValue, list, string)
import Json.Encode exposing (Value)
import Maybe exposing (withDefault)
import Model exposing (Model, emptyPeerData)
2020-11-26 16:53:31 +03:00
import Modules.Air
2020-11-23 11:07:07 +03:00
import Msg exposing (..)
2020-11-26 16:53:31 +03:00
import Nodes.Air
2020-11-23 14:27:33 +03:00
import Port exposing (sendAir)
2020-11-23 15:44:45 +03:00
import Route
import Services.Air
2020-11-23 15:44:45 +03:00
import Url
2020-11-23 11:07:07 +03:00
2020-11-25 19:51:53 +03:00
maybeValueToString : Maybe Value -> String
maybeValueToString mv =
case mv of
Just v ->
2020-11-25 19:51:53 +03:00
case decodeValue string v of
Ok value ->
value
Err error ->
"error"
Nothing ->
""
2020-11-25 19:51:53 +03:00
-- list of lists of strings in json to list of strings from first element if it is an array
2020-11-25 19:51:53 +03:00
maybeValueToListString : Maybe Value -> List String
maybeValueToListString mv =
case mv of
Just v ->
2020-11-25 19:51:53 +03:00
case decodeValue (list (list string)) v of
Ok value ->
Maybe.withDefault [] (List.head value)
Err error ->
let
2020-11-25 19:51:53 +03:00
_ =
Debug.log "error" error
in
2020-11-25 19:51:53 +03:00
case decodeValue (list string) v of
Ok value ->
value
Err err ->
let
_ =
Debug.log "err" err
in
[ "error" ]
Nothing ->
[]
2020-11-23 11:07:07 +03:00
2020-11-25 19:51:53 +03:00
2020-11-23 11:07:07 +03:00
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model, Cmd.none )
2020-11-23 15:44:45 +03:00
UrlChanged url ->
let
route =
2020-11-23 16:31:31 +03:00
Route.parse url
2020-11-23 14:27:33 +03:00
2020-11-23 15:44:45 +03:00
cmd =
Route.routeCommand model route
in
( { model | url = url }, cmd )
LinkClicked urlRequest ->
case urlRequest of
Browser.Internal url ->
( model, Nav.pushUrl model.key (Url.toString url) )
Browser.External href ->
( model, Nav.load href )
2020-11-23 14:27:33 +03:00
AquamarineEvent { name, peer, peers, services, modules } ->
2020-11-23 17:39:32 +03:00
case name of
"peers_discovered" ->
let
2020-11-25 19:51:53 +03:00
peersMap =
List.map (\p -> Tuple.pair p emptyPeerData) (withDefault [] peers)
newDict =
Dict.fromList peersMap
updatedDict =
Dict.union model.discoveredPeers newDict
2020-11-23 17:39:32 +03:00
in
( { model | discoveredPeers = updatedDict }, Cmd.none )
2020-11-23 17:39:32 +03:00
"services_discovered" ->
let
2020-11-25 19:51:53 +03:00
newServices =
Maybe.withDefault [] services
empty =
emptyPeerData
up =
\old -> Just (Maybe.withDefault { empty | services = newServices } (Maybe.map (\o -> { o | services = newServices }) old))
updatedDict =
Dict.update peer up model.discoveredPeers
in
2020-11-25 19:51:53 +03:00
( { model | discoveredPeers = updatedDict }, Cmd.none )
"modules_discovered" ->
let
2020-11-25 19:51:53 +03:00
newModules =
Maybe.withDefault [] modules
empty =
emptyPeerData
up =
\old -> Just (Maybe.withDefault { empty | modules = newModules } (Maybe.map (\o -> { o | modules = newModules }) old))
updatedDict =
Dict.update peer up model.discoveredPeers
in
2020-11-25 19:51:53 +03:00
( { model | discoveredPeers = updatedDict }, Cmd.none )
2020-11-23 17:39:32 +03:00
_ ->
let
2020-11-25 19:51:53 +03:00
_ =
Debug.log "event in ELM" name
2020-11-23 17:39:32 +03:00
in
( model, Cmd.none )
2020-11-23 11:07:07 +03:00
2020-11-26 16:53:31 +03:00
Click command ->
case command of
"get_services" ->
( model
, sendAir (Services.Air.air model.peerId model.relayId (Dict.keys model.discoveredPeers))
)
"get_modules" ->
( model
, sendAir (Modules.Air.air model.peerId model.relayId (Dict.keys model.discoveredPeers))
)
"get_identify" ->
( model
, sendAir (Nodes.Air.air model.peerId model.relayId (Dict.keys model.discoveredPeers))
)
_ ->
(model, Cmd.none)
2020-11-23 11:07:07 +03:00
2020-11-23 14:27:33 +03:00
RelayChanged relayId ->
( { model | relayId = relayId }, Cmd.none )