dashboard/src/Modules/View.elm

67 lines
2.3 KiB
Elm
Raw Normal View History

module Modules.View exposing (..)
2020-11-28 17:59:09 +03:00
import Dict exposing (Dict)
2020-12-03 10:03:33 +03:00
import Html exposing (Html, a, div, p, text)
2020-11-30 19:56:11 +03:00
import Html.Attributes exposing (attribute)
2020-12-01 14:51:12 +03:00
import Maybe.Extra
2020-11-28 17:59:09 +03:00
import Model exposing (Model, PeerData)
2020-12-01 14:03:25 +03:00
import Modules.Model exposing (Module, ModuleShortInfo)
import Palette exposing (classes)
import Utils.Utils exposing (instancesText)
2020-11-25 19:51:53 +03:00
2020-11-30 14:31:03 +03:00
2020-11-28 17:59:09 +03:00
getModuleShortInfo : Model -> List ModuleShortInfo
getModuleShortInfo model =
2020-12-02 03:21:02 +03:00
getAllModules model.modules model.discoveredPeers |> Dict.toList |> List.map (\( _, ( moduleInfo, peers ) ) -> { moduleInfo = moduleInfo, instanceNumber = List.length peers })
2020-11-30 14:31:03 +03:00
2020-11-25 19:51:53 +03:00
2020-12-01 14:51:12 +03:00
getAllModules : Dict String Module -> Dict String PeerData -> Dict String ( Module, List String )
getAllModules modules peerData =
2020-11-28 17:59:09 +03:00
let
2020-11-30 14:31:03 +03:00
peerDatas =
Dict.toList peerData
2020-12-01 14:51:12 +03:00
allModulesByPeers =
2020-11-30 14:31:03 +03:00
peerDatas |> List.map (\( peer, pd ) -> pd.modules |> List.map (\ms -> ( peer, ms ))) |> List.concat
peersByModuleName =
2020-12-01 14:51:12 +03:00
allModulesByPeers |> List.foldr (updateDict modules) Dict.empty
2020-11-28 17:59:09 +03:00
in
2020-11-30 14:31:03 +03:00
peersByModuleName
2020-11-28 17:59:09 +03:00
2020-12-01 14:03:25 +03:00
-- group by module name and append peers
2020-12-01 14:51:12 +03:00
updateDict : Dict String Module -> ( String, String ) -> Dict String ( Module, List String ) -> Dict String ( Module, List String )
updateDict modules ( peer, moduleName ) dict =
2020-12-01 14:03:25 +03:00
dict
2020-12-01 14:51:12 +03:00
|> Dict.update moduleName
2020-12-01 14:03:25 +03:00
(\oldM ->
2020-12-01 14:51:12 +03:00
Maybe.Extra.or
(oldM |> Maybe.map (\( info, peers ) -> ( info, List.append [ peer ] peers )))
(Dict.get moduleName modules |> Maybe.map (\m -> ( m, [ peer ] )))
2020-12-01 14:03:25 +03:00
)
2020-11-28 17:59:09 +03:00
view : Model -> Html msg
view modules =
let
2020-12-01 14:51:12 +03:00
info =
getModuleShortInfo modules
2020-11-25 19:51:53 +03:00
modulesView =
2020-12-01 14:51:12 +03:00
List.map viewService info
in
2020-12-03 11:32:26 +03:00
div [ classes "cf" ] modulesView
2020-11-25 19:51:53 +03:00
2020-11-25 22:11:11 +03:00
viewService : ModuleShortInfo -> Html msg
2020-12-01 14:03:25 +03:00
viewService moduleInfo =
div [ classes "fl w-third pr3" ]
2020-12-03 15:45:37 +03:00
[ a [ attribute "href" ("/module/" ++ moduleInfo.moduleInfo.name), classes "fl w-100 bg-white black mw6 mr2 mb3 ph4 hide-child pa2 br3 element-box ba b--white bw1" ]
2020-12-03 13:02:23 +03:00
[ p [ classes "tl di" ] [ div [ classes "fl b w-100 mb1" ] [ text moduleInfo.moduleInfo.name ], div [ classes "fl w-100 mt1 lucida gray" ] [ instancesText moduleInfo.instanceNumber ] ]
2020-11-25 19:51:53 +03:00
]
]