elm-format

This commit is contained in:
3wc 2021-05-04 18:27:42 +02:00
parent dfba28c5f8
commit e22980281a
2 changed files with 183 additions and 117 deletions

View File

@ -1,19 +1,20 @@
module Pages.App_String exposing (Model, Msg, Params, page) module Pages.App_String exposing (Model, Msg, Params, page)
import Regex import Html exposing (Html, a, button, div, h2, h5, i, img, li, p, span, text, ul)
import Html exposing (Html, button, div, h2, h5, img, text, ul, li, a, p, span, i) import Html.Attributes exposing (alt, class, href, src, style)
import Html.Attributes exposing (src, style, class, alt, href)
import Html.Events exposing (onClick) import Html.Events exposing (onClick)
import Http import Http
import Markdown
import Json.Decode as Decode import Json.Decode as Decode
import Json.Decode.Extra as Decode exposing (andMap) import Json.Decode.Extra as Decode exposing (andMap)
import Markdown
import Regex
import Spa.Document exposing (Document) import Spa.Document exposing (Document)
import Spa.Generated.Route as Route import Spa.Generated.Route as Route
import Spa.Page as Page exposing (Page) import Spa.Page as Page exposing (Page)
import Spa.Url as Url exposing (Url) import Spa.Url as Url exposing (Url)
-- INIT -- INIT
@ -26,9 +27,11 @@ page =
, subscriptions = subscriptions , subscriptions = subscriptions
} }
type alias Params = type alias Params =
{ app : String { app : String
} }
type alias App = type alias App =
{ name : String { name : String
@ -43,7 +46,8 @@ type alias App =
, description : Maybe String , description : Maybe String
} }
type alias Model =
type alias Model =
{ url : Url Params { url : Url Params
, status : Status , status : Status
, readme : String , readme : String
@ -62,8 +66,15 @@ init url =
default_image : String default_image : String
-- FIXME: change to absolute URL, if this works? -- FIXME: change to absolute URL, if this works?
default_image = "/logo.png"
default_image =
"/logo.png"
-- UPDATE -- UPDATE
@ -84,18 +95,20 @@ update msg model =
GotApps result -> GotApps result ->
case result of case result of
Ok apps -> Ok apps ->
let let
-- TODO better way of getting a single app? -- TODO better way of getting a single app?
apps_filtered = List.filter (\app -> app.slug == model.url.params.app) apps apps_filtered =
in List.filter (\app -> app.slug == model.url.params.app) apps
case List.head apps_filtered of in
Nothing -> case List.head apps_filtered of
( { model | status = Failure }, Cmd.none ) Nothing ->
Just item -> ( { model | status = Failure }, Cmd.none )
( { model | status = Success (item) }, loadREADME item)
Just item ->
( { model | status = Success item }, loadREADME item )
Err _ -> Err _ ->
( { model | status = Failure } , Cmd.none ) ( { model | status = Failure }, Cmd.none )
GotText result -> GotText result ->
case result of case result of
@ -103,15 +116,19 @@ update msg model =
-- update model.content with the loaded README -- update model.content with the loaded README
let let
-- remove HTML comments -- remove HTML comments
pattern = "<!--.*-->" pattern =
maybeRegex = Regex.fromString pattern "<!--.*-->"
regex = Maybe.withDefault Regex.never maybeRegex
maybeRegex =
Regex.fromString pattern
regex =
Maybe.withDefault Regex.never maybeRegex
in in
( { model | readme = Regex.replace regex (\_ -> "") content }, Cmd.none ) ( { model | readme = Regex.replace regex (\_ -> "") content }, Cmd.none )
Err _ -> Err _ ->
( { model | status = Failure } , Cmd.none ) ( { model | status = Failure }, Cmd.none )
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
@ -135,8 +152,10 @@ title model =
case model.status of case model.status of
Loading -> Loading ->
"loading abra apps" "loading abra apps"
Failure -> Failure ->
"error - abra apps" "error - abra apps"
Success app -> Success app ->
app.name ++ " abra apps" app.name ++ " abra apps"
@ -154,20 +173,20 @@ body model =
] ]
Loading -> Loading ->
div [ class "d-flex align-items-center", style "height" "89vh" ] div [ class "d-flex align-items-center", style "height" "89vh" ]
[ div [ class "spinner-border m-auto text-light" ] [ div [ class "spinner-border m-auto text-light" ]
[ [ span [ class "sr-only" ] [ text "Loading..." ]
span [ class "sr-only" ] [ text "Loading..." ] ]
] ]
]
Success app -> Success app ->
div [] div []
[ div [ class "row" ] [ div [ class "row" ]
[ viewApp app model.readme ] [ viewApp app model.readme ]
] ]
] ]
viewStatusBadge : App -> Html Msg viewStatusBadge : App -> Html Msg
viewStatusBadge app = viewStatusBadge app =
let let
@ -175,17 +194,22 @@ viewStatusBadge app =
case app.status of case app.status of
"1" -> "1" ->
"badge-success" "badge-success"
"2" -> "2" ->
"badge-info" "badge-info"
"3" -> "3" ->
"badge-warning" "badge-warning"
"4" ->
"4" ->
"badge-danger" "badge-danger"
_ -> _ ->
"badge-dark" "badge-dark"
in in
span [ class ("card-link badge " ++ status_class) ] span [ class ("card-link badge " ++ status_class) ]
[ text ("Score: " ++ app.status) ] [ text ("Score: " ++ app.status) ]
viewApp : App -> String -> Html Msg viewApp : App -> String -> Html Msg
viewApp app readme = viewApp app readme =
@ -194,89 +218,104 @@ viewApp app readme =
case app.icon of case app.icon of
Just "" -> Just "" ->
default_image default_image
Just i -> Just i ->
i i
Nothing -> Nothing ->
default_image default_image
repository_link =
repository_link =
case app.repository of case app.repository of
Just link -> Just link ->
a [ class "card-link", href link ] a [ class "card-link", href link ]
[ i [ class "fab fa-git-alt" ] [] [ i [ class "fab fa-git-alt" ] []
, text "code" ] , text "code"
]
Nothing -> Nothing ->
text "" text ""
website_link =
website_link =
case app.website of case app.website of
Just link -> Just link ->
case link of case link of
"" -> "" ->
text "" text ""
_ -> _ ->
a [ class "card-link", href link ] a [ class "card-link", href link ]
[ i [ class "fas fa-home" ] [] [ i [ class "fas fa-home" ] []
, text "homepage" ] , text "homepage"
]
Nothing -> Nothing ->
text "" text ""
in in
div [ class "col-md-6 col-sm-10 mb-3 offset-md-3 offset-sm-1" ] div [ class "col-md-6 col-sm-10 mb-3 offset-md-3 offset-sm-1" ]
[ div [ class "card" ] [ div [ class "card" ]
[ div [ class "card-header" ] [ div [ class "card-header" ]
[ a [ a
[ class "btn btn-sm border border-secondary card-link" [ class "btn btn-sm border border-secondary card-link"
, href (Route.toString Route.Top)] , href (Route.toString Route.Top)
[ text " back" ]
, span [ class "card-link badge badge-secondary" ] [ text app.category ]
, viewStatusBadge app
, repository_link
, website_link
] ]
, img [ class "card-img-top", src icon_url, alt ("icon for " ++ app.name) ] [] [ text " back" ]
, div [ class "card-body" ] , span [ class "card-link badge badge-secondary" ] [ text app.category ]
-- render Markdown with no special options , viewStatusBadge app
[ div [] (Markdown.toHtml Nothing readme) , repository_link
] , website_link
, div [ class "card-footer" ]
[]
] ]
, img [ class "card-img-top", src icon_url, alt ("icon for " ++ app.name) ] []
, div [ class "card-body" ]
-- render Markdown with no special options
[ div [] (Markdown.toHtml Nothing readme)
]
, div [ class "card-footer" ]
[]
] ]
]
-- HTTP -- HTTP
loadApp : Cmd Msg loadApp : Cmd Msg
loadApp = loadApp =
Http.get Http.get
{ url = "/abra-apps.json" { url = "/abra-apps.json"
, expect = Http.expectJson GotApps appListDecoder } , expect = Http.expectJson GotApps appListDecoder
}
loadREADME : App -> Cmd Msg loadREADME : App -> Cmd Msg
loadREADME app = loadREADME app =
let let
repository_link = repository_link =
case app.repository of case app.repository of
Just link -> Just link ->
a [ class "card-link", href link ] a [ class "card-link", href link ]
[ i [ class "fab fa-git-alt" ] [] [ i [ class "fab fa-git-alt" ] []
, text "code" ] , text "code"
]
Nothing -> Nothing ->
text "" text ""
in in
Http.get Http.get
-- FIXME use live Gitea link -- FIXME use live Gitea link
{ url = "https://cors-container.herokuapp.com/https://git.autonomic.zone/coop-cloud/" ++ app.slug ++ "/raw/branch/" ++ app.default_branch ++ "/README.md" { url = "https://cors-container.herokuapp.com/https://git.autonomic.zone/coop-cloud/" ++ app.slug ++ "/raw/branch/" ++ app.default_branch ++ "/README.md"
, expect = Http.expectString GotText } , expect = Http.expectString GotText
}
featuresDecoder : Decode.Decoder String featuresDecoder : Decode.Decoder String
featuresDecoder = featuresDecoder =
-- get features.status if it's there -- get features.status if it's there
(Decode.oneOf Decode.oneOf
[ Decode.at [ "status" ] Decode.string [ Decode.at [ "status" ] Decode.string
, Decode.succeed "" , Decode.succeed ""
] ]
)
appDecoder : Decode.Decoder App appDecoder : Decode.Decoder App
@ -300,6 +339,6 @@ appListDecoder =
|> Decode.map buildApp |> Decode.map buildApp
buildApp : List (String, App) -> (List App) buildApp : List ( String, App ) -> List App
buildApp apps = buildApp apps =
List.map (\(slug, app) -> { app | slug = slug}) apps List.map (\( slug, app ) -> { app | slug = slug }) apps

View File

@ -1,17 +1,17 @@
module Pages.Top exposing (Model, Msg, Params, page) module Pages.Top exposing (Model, Msg, Params, page)
import Html exposing (Html, button, div, h2, h5, img, text, ul, li, a, p, span, i) import Html exposing (Html, a, button, div, h2, h5, i, img, li, p, span, text, ul)
import Html.Attributes exposing (src, style, class, alt, href) import Html.Attributes exposing (alt, class, href, src, style)
import Html.Events exposing (onClick) import Html.Events exposing (onClick)
import Http import Http
import Maybe exposing (withDefault)
import Json.Decode as Decode import Json.Decode as Decode
import Json.Decode.Extra as Decode exposing (andMap) import Json.Decode.Extra as Decode exposing (andMap)
import Spa.Generated.Route as Route import Maybe exposing (withDefault)
import Spa.Document exposing (Document) import Spa.Document exposing (Document)
import Spa.Generated.Route as Route
import Spa.Page as Page exposing (Page) import Spa.Page as Page exposing (Page)
import Spa.Url as Url exposing (Url) import Spa.Url as Url exposing (Url)
import Util exposing (by, andThen, Direction(..)) import Util exposing (Direction(..), andThen, by)
page : Page Params Model Msg page : Page Params Model Msg
@ -23,12 +23,15 @@ page =
, subscriptions = subscriptions , subscriptions = subscriptions
} }
-- INIT -- INIT
type alias Params = type alias Params =
() ()
type alias App = type alias App =
{ name : String { name : String
, category : String , category : String
@ -55,7 +58,9 @@ init { params } =
default_image : String default_image : String
default_image = "/logo.png" default_image =
"/logo.png"
-- UPDATE -- UPDATE
@ -103,6 +108,7 @@ body model =
[ viewApps model [ viewApps model
] ]
viewStatusBadge : App -> Html Msg viewStatusBadge : App -> Html Msg
viewStatusBadge app = viewStatusBadge app =
let let
@ -110,17 +116,22 @@ viewStatusBadge app =
case app.status of case app.status of
1 -> 1 ->
"badge-success" "badge-success"
2 -> 2 ->
"badge-info" "badge-info"
3 -> 3 ->
"badge-warning" "badge-warning"
4 -> 4 ->
"badge-danger" "badge-danger"
_ -> _ ->
"badge-dark" "badge-dark"
in in
span [ class ("card-link badge " ++ status_class) ] span [ class ("card-link badge " ++ status_class) ]
[ text ("Score: " ++ String.fromInt app.status) ] [ text ("Score: " ++ String.fromInt app.status) ]
viewApp : App -> Html Msg viewApp : App -> Html Msg
viewApp app = viewApp app =
@ -129,53 +140,63 @@ viewApp app =
case app.icon of case app.icon of
Just "" -> Just "" ->
default_image default_image
Just i -> Just i ->
i i
Nothing -> Nothing ->
default_image default_image
repository_link = repository_link =
case app.repository of case app.repository of
Just link -> Just link ->
a [ class "card-link", href link ] a [ class "card-link", href link ]
[ [ i [ class "fab fa-git-alt" ] []
i [ class "fab fa-git-alt" ] [] , text "code"
, text "code"
] ]
Nothing -> Nothing ->
text "" text ""
website_link = website_link =
case app.website of case app.website of
Just link -> Just link ->
case link of case link of
"" -> "" ->
text "" text ""
_ -> _ ->
a [ class "card-link", href link ] a [ class "card-link", href link ]
[ i [ class "fas fa-home" ] [] [ i [ class "fas fa-home" ] []
, text "homepage" ] , text "homepage"
]
Nothing -> Nothing ->
text "" text ""
app_href = Route.toString <| Route.App_String { app = app.slug }
in app_href =
div [ class "col-md-4 mb-3 col-sm-12" ] Route.toString <| Route.App_String { app = app.slug }
[ div [ class "card" ] in
[ img [ class "card-img-top", src icon_url, alt ("icon for " ++ app.name) ] [] div [ class "col-md-4 mb-3 col-sm-12" ]
, div [ class "card-body" ] [ div [ class "card" ]
[ h5 [ class "card-title" ] [ img [ class "card-img-top", src icon_url, alt ("icon for " ++ app.name) ] []
[ a [ href app_href ] [ text app.name ] ] , div [ class "card-body" ]
, p [] [ text (withDefault "" app.description) ] [ h5 [ class "card-title" ]
, repository_link [ a [ href app_href ] [ text app.name ] ]
, website_link , p [] [ text (withDefault "" app.description) ]
, a [ class "card-link", href app_href ] , repository_link
[ i [ class "fas fa-book" ] [] , website_link
, text "docs" ] , a [ class "card-link", href app_href ]
] [ i [ class "fas fa-book" ] []
, div [ class "card-footer" ] , text "docs"
[ span [ class "card-link badge badge-secondary" ] [ text app.category ]
, viewStatusBadge app
] ]
] ]
, div [ class "card-footer" ]
[ span [ class "card-link badge badge-secondary" ] [ text app.category ]
, viewStatusBadge app
]
] ]
]
viewApps : Model -> Html Msg viewApps : Model -> Html Msg
@ -192,19 +213,25 @@ viewApps model =
Loading -> Loading ->
div [ class "d-flex align-items-center", style "height" "89vh" ] div [ class "d-flex align-items-center", style "height" "89vh" ]
[ div [ class "spinner-border m-auto text-light" ] [ div [ class "spinner-border m-auto text-light" ]
[ [ span [ class "sr-only" ] [ text "Loading..." ]
span [ class "sr-only" ] [ text "Loading..." ]
] ]
] ]
Success apps -> Success apps ->
div [] div []
[ div [ class "row" ] [ div [ class "row" ]
(List.map viewApp (apps |> List.sortWith (List.map viewApp
(by .status ASC (apps
|> andThen (String.toLower << .name) ASC)) |> List.sortWith
) (by .status ASC
|> andThen (String.toLower << .name) ASC
)
)
)
] ]
-- HTTP -- HTTP
@ -215,12 +242,12 @@ loadApps =
, expect = Http.expectJson GotApps appListDecoder , expect = Http.expectJson GotApps appListDecoder
} }
featuresDecoder = featuresDecoder =
(Decode.oneOf Decode.oneOf
[ Decode.at [ "status" ] Decode.int [ Decode.at [ "status" ] Decode.int
, Decode.succeed 5 , Decode.succeed 5
] ]
)
appDecoder : Decode.Decoder App appDecoder : Decode.Decoder App
@ -244,6 +271,6 @@ appListDecoder =
|> Decode.map buildApp |> Decode.map buildApp
buildApp : List (String, App) -> (List App) buildApp : List ( String, App ) -> List App
buildApp apps = buildApp apps =
List.map (\(slug, app) -> { app | slug = slug}) apps List.map (\( slug, app ) -> { app | slug = slug }) apps