From f30f25160ec5940721ea6ee2a55926094bb0237c Mon Sep 17 00:00:00 2001 From: 3wc <3wc.git@doesthisthing.work> Date: Sat, 24 Apr 2021 14:10:32 +0200 Subject: [PATCH] Look up README using default_branch, + website --- elm.json | 5 ++- src/Pages/App_String.elm | 95 +++++++++++++++++++++++++++------------- src/Pages/Top.elm | 35 ++++++++++++--- 3 files changed, 96 insertions(+), 39 deletions(-) diff --git a/elm.json b/elm.json index e572758..e114016 100644 --- a/elm.json +++ b/elm.json @@ -13,13 +13,16 @@ "elm/json": "1.1.3", "elm/regex": "1.0.0", "elm/url": "1.0.0", + "elm-community/json-extra": "4.3.0", "pablohirafuji/elm-markdown": "2.0.5" }, "indirect": { "elm/bytes": "1.0.8", "elm/file": "1.0.5", + "elm/parser": "1.1.0", "elm/time": "1.0.0", - "elm/virtual-dom": "1.0.2" + "elm/virtual-dom": "1.0.2", + "rtfeldman/elm-iso8601-date-strings": "1.1.3" } }, "test-dependencies": { diff --git a/src/Pages/App_String.elm b/src/Pages/App_String.elm index c1d3cf8..1c613e0 100644 --- a/src/Pages/App_String.elm +++ b/src/Pages/App_String.elm @@ -7,7 +7,9 @@ import Html.Events exposing (onClick) import Http import Markdown import Json.Decode as Decode +import Json.Decode.Extra as Decode exposing (andMap) import Spa.Document exposing (Document) +import Spa.Generated.Route as Route import Spa.Page as Page exposing (Page) import Spa.Url as Url exposing (Url) @@ -35,6 +37,9 @@ type alias App = , versions : Maybe (List String) , icon : Maybe String , status : String + , slug : String + , default_branch : String + , website : Maybe String } @@ -53,7 +58,7 @@ type Status init : Url Params -> ( Model, Cmd Msg ) init url = - ( { url = url, status = Loading, readme = "" }, loadData ) + ( { url = url, status = Loading, readme = "" }, loadApp ) default_image : String @@ -74,20 +79,20 @@ update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case msg of MorePlease -> - ( { model | status = Loading }, loadData ) + ( { model | status = Loading }, loadApp ) GotApps result -> case result of Ok apps -> let -- TODO better way of getting a single app? - apps_filtered = List.filter (\app -> app.name == model.url.params.app) apps + apps_filtered = List.filter (\app -> app.slug == model.url.params.app) apps in case List.head apps_filtered of Nothing -> - ( { model | status = Failure }, Cmd.none ) + ( { model | status = Failure }, Cmd.none ) Just item -> - ( { model | status = Success (item) }, Cmd.none ) + ( { model | status = Success (item) }, loadREADME item) Err _ -> ( { model | status = Failure }, Cmd.none ) @@ -194,17 +199,30 @@ viewApp app readme = , text "code" ] Nothing -> text "" + website_link = + case app.website of + Just link -> + case link of + "" -> + text "" + _ -> + a [ class "card-link", href link ] + [ i [ class "fas fa-home" ] [] + , text "homepage" ] + Nothing -> + text "" in div [ class "col-md-6 col-sm-10 mb-3 offset-md-3 offset-sm-1" ] [ div [ class "card" ] [ div [ class "card-header" ] - [ span [ class "card-link badge badge-secondary" ] [ text app.category ] + [ a + [ class "btn btn-sm border border-secondary card-link" + , href (Route.toString Route.Top)] + [ text "← back" ] + , span [ class "card-link badge badge-secondary" ] [ text app.category ] , viewStatusBadge app , repository_link - , a [ class "card-link", href "http://example.com" ] - [ i [ class "fas fa-home" ] [] - , text "homepage" ] - -- FIXME: add actual homepage link to apps.json and render here + , website_link ] , img [ class "card-img-top", src icon_url, alt ("icon for " ++ app.name) ] [] , div [ class "card-body" ] @@ -219,19 +237,32 @@ viewApp app readme = -- HTTP -loadData : Cmd Msg -loadData = - -- fetch app JSON and README in parallel - Cmd.batch - [ Http.get - -- FIXME: change to absolute URL, if this works? - { url = "http://localhost:8000/abra-apps-list.json" - , expect = Http.expectJson GotApps appListDecoder } - , Http.get - -- FIXME add branch name to apps.json, load actual README - { url = "http://localhost:1234/coop-cloud/adapt_authoring/raw/branch/main/README.md" - , expect = Http.expectString GotText } - ] +loadApp : Cmd Msg +loadApp = + -- fetch app JSON and README in parallel + Http.get + -- FIXME: change to absolute URL, if this works? + { url = "http://localhost:8000/abra-apps-list.json" + , expect = Http.expectJson GotApps appListDecoder } + + +loadREADME : App -> Cmd Msg +loadREADME app = + let + repository_link = + case app.repository of + Just link -> + a [ class "card-link", href link ] + [ i [ class "fab fa-git-alt" ] [] + , text "code" ] + Nothing -> + text "" + in + Http.get + -- FIXME add branch name to apps.json, load actual README + { url = "http://localhost:1234/coop-cloud/" ++ app.slug ++ "/raw/branch/" ++ app.default_branch ++ "/README.md" + , expect = Http.expectString GotText } + featuresDecoder = -- get features.status if it's there @@ -244,14 +275,16 @@ featuresDecoder = appDecoder : Decode.Decoder App appDecoder = - Decode.map6 - App - (Decode.field "name" Decode.string) - (Decode.field "category" Decode.string) - (Decode.maybe (Decode.field "repository" Decode.string)) - (Decode.succeed Nothing) - (Decode.maybe (Decode.field "icon" Decode.string)) - (Decode.at [ "features" ] featuresDecoder) + Decode.succeed App + |> andMap (Decode.field "name" Decode.string) + |> andMap (Decode.field "category" Decode.string) + |> andMap (Decode.maybe (Decode.field "repository" Decode.string)) + |> andMap (Decode.succeed Nothing) + |> andMap (Decode.maybe (Decode.field "icon" Decode.string)) + |> andMap (Decode.at [ "features" ] featuresDecoder) + |> andMap (Decode.field "slug" Decode.string) + |> andMap (Decode.field "default_branch" Decode.string) + |> andMap (Decode.maybe (Decode.field "website" Decode.string)) appListDecoder : Decode.Decoder (List App) diff --git a/src/Pages/Top.elm b/src/Pages/Top.elm index 73cd605..e1ad8b4 100644 --- a/src/Pages/Top.elm +++ b/src/Pages/Top.elm @@ -33,6 +33,8 @@ type alias App = , versions : Maybe (List String) , icon : Maybe String , status : String + , slug : String + , website : Maybe String } @@ -111,9 +113,16 @@ viewStatusBadge app = "badge-danger" _ -> "badge-dark" + status_score = + case app.status of + "" -> + "?" + score -> + score + in span [ class ("card-link badge " ++ status_class) ] - [ text ("Score: " ++ app.status) ] + [ text ("Score: " ++ status_score) ] viewApp : App -> Html Msg viewApp app = @@ -136,7 +145,19 @@ viewApp app = ] Nothing -> text "" - app_href = Route.toString <| Route.App_String { app = app.name } + website_link = + case app.website of + Just link -> + case link of + "" -> + text "" + _ -> + a [ class "card-link", href link ] + [ i [ class "fas fa-home" ] [] + , text "homepage" ] + Nothing -> + text "" + app_href = Route.toString <| Route.App_String { app = app.slug } in div [ class "col-4 mb-3" ] [ div [ class "card" ] @@ -145,9 +166,7 @@ viewApp app = [ h5 [ class "card-title" ] [ a [ href app_href ] [ text app.name ] ] , repository_link - , a [ class "card-link", href "http://example.com" ] - [ i [ class "fas fa-home" ] [] - , text "homepage" ] + , website_link , a [ class "card-link", href app_href ] [ i [ class "fas fa-book" ] [] , text "docs" ] @@ -174,7 +193,7 @@ viewApps model = Success apps -> div [] [ div [ class "row" ] - (List.map viewApp (List.sortBy .name apps)) + (List.map viewApp (List.sortBy (String.toLower << .name) apps)) ] -- HTTP @@ -197,7 +216,7 @@ featuresDecoder = appDecoder : Decode.Decoder App appDecoder = - Decode.map6 + Decode.map8 App (Decode.field "name" Decode.string) (Decode.field "category" Decode.string) @@ -205,6 +224,8 @@ appDecoder = (Decode.succeed Nothing) (Decode.maybe (Decode.field "icon" Decode.string)) (Decode.at [ "features" ] featuresDecoder) + (Decode.field "slug" Decode.string) + (Decode.maybe (Decode.field "website" Decode.string)) appListDecoder : Decode.Decoder (List App) appListDecoder =