add support for '/module/id' and '/service/id' pages

This commit is contained in:
DieMyst 2020-11-28 18:16:57 +03:00
parent 5146543311
commit 856ead4b7a
3 changed files with 69 additions and 55 deletions

View File

@ -26,6 +26,8 @@ import Url
type Route
= Page String
| Service String
| Module String
| Peer String

View File

@ -15,6 +15,8 @@ routeParser : Parser (Route -> a) a
routeParser =
oneOf
[ map Peer (s "peer" </> string)
, map Module (s "module" </> string)
, map Service (s "service" </> string)
, map Page string
]
@ -33,31 +35,6 @@ routeView model route =
"hub" ->
HubPage.view model
"module" ->
let
up =
\( pid, p ) -> Maybe.map (\a -> Tuple.pair pid a) (List.head (List.drop 0 p.services))
el =
List.head (List.drop 3 (Dict.toList model.discoveredPeers))
in
case Maybe.andThen up el of
Just ( peerId, service ) ->
let
example =
{ name = "Chat"
, id = service.service_id
, author = "Fluence Labs"
, authorPeerId = peerId
, description = "Cool service"
, website = "https://github.com/fluencelabs/chat"
, service = service
}
in
ModulePage.view example
Nothing ->
Html.text "alala"
_ ->
Html.text ("undefined page: " ++ page)
@ -65,37 +42,74 @@ routeView model route =
Peer peer ->
Html.text peer
Service serviceId ->
Html.text serviceId
Module moduleName ->
let
up =
\( pid, p ) -> Maybe.map (\a -> Tuple.pair pid a) (List.head (List.drop 0 p.services))
el =
List.head (List.drop 2 (Dict.toList model.discoveredPeers))
in
case Maybe.andThen up el of
Just ( peerId, service ) ->
let
example =
{ name = moduleName
, id = service.service_id
, author = "Fluence Labs"
, authorPeerId = peerId
, description = "Cool service"
, website = "https://github.com/fluencelabs/chat"
, service = service
}
in
ModulePage.view example
Nothing ->
Html.text moduleName
getPeers : Model -> Cmd msg
getPeers m =
let
clientId =
set "clientId" <| Encode.string m.peerId
relayId =
set "relayId" <| Encode.string m.relayId
air =
seq
(callBI "relayId" ( "dht", "neighborhood" ) [ "clientId" ] (Just "peers"))
(par
(relayEvent "peers_discovered" [ "relayId", "peers" ])
(fold "peers" "p" <|
par
(seq
(callBI "p" ( "dht", "neighborhood" ) [ "clientId" ] (Just "morePeers"))
(relayEvent "peers_discovered" [ "p", "morePeers" ])
)
(next "p")
)
)
in
sendAir (relayId <| clientId <| air)
routeCommand : Model -> Route -> Cmd msg
routeCommand m r =
case r of
Page s ->
let
_ =
Debug.log "page" s
clientId =
set "clientId" <| Encode.string m.peerId
relayId =
set "relayId" <| Encode.string m.relayId
air =
seq
(callBI "relayId" ( "dht", "neighborhood" ) [ "clientId" ] (Just "peers"))
(par
(relayEvent "peers_discovered" [ "relayId", "peers" ])
(fold "peers" "p" <|
par
(seq
(callBI "p" ( "dht", "neighborhood" ) [ "clientId" ] (Just "morePeers"))
(relayEvent "peers_discovered" [ "p", "morePeers" ])
)
(next "p")
)
)
in
sendAir (relayId <| clientId <| air)
getPeers m
Peer _ ->
Cmd.none
getPeers m
Service string ->
getPeers m
Module string ->
getPeers m

View File

@ -40,9 +40,7 @@ body model =
layout <|
List.concat
[ [ header [ classes "w-100 bt bb b--black-10" ] [ routeView model model.page ] ]
++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_all") ] [ text "GET SERVICES" ] ]
++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_modules") ] [ text "GET MODULES" ] ]
++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_identify") ] [ text "GET IDENTIFY" ] ]
++ [ header [ classes "w-100 bt bb b--black-10", onClick (Click "get_all") ] [ text "GET ALL" ] ]
]