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,10 +27,12 @@ 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
, category : String , category : String
@ -43,6 +46,7 @@ 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
@ -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
@ -86,13 +97,15 @@ update msg model =
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 =
List.filter (\app -> app.slug == model.url.params.app) apps
in in
case List.head apps_filtered of case List.head apps_filtered of
Nothing -> Nothing ->
( { model | status = Failure }, Cmd.none ) ( { model | status = Failure }, Cmd.none )
Just item -> Just item ->
( { model | status = Success (item) }, loadREADME item) ( { model | status = Success item }, loadREADME item )
Err _ -> Err _ ->
( { model | status = Failure }, Cmd.none ) ( { model | status = Failure }, Cmd.none )
@ -103,9 +116,14 @@ 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 )
@ -113,7 +131,6 @@ update msg model =
( { model | status = Failure }, Cmd.none ) ( { model | status = Failure }, Cmd.none )
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
subscriptions model = subscriptions model =
Sub.none Sub.none
@ -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"
@ -156,8 +175,7 @@ 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..." ]
] ]
] ]
@ -168,6 +186,7 @@ body model =
] ]
] ]
viewStatusBadge : App -> Html Msg viewStatusBadge : App -> Html Msg
viewStatusBadge app = viewStatusBadge app =
let let
@ -175,18 +194,23 @@ 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 =
let let
@ -194,28 +218,37 @@ 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
@ -224,7 +257,8 @@ viewApp app readme =
[ 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" ] [ text " back" ]
, span [ class "card-link badge badge-secondary" ] [ text app.category ] , span [ class "card-link badge badge-secondary" ] [ text app.category ]
, viewStatusBadge app , viewStatusBadge app
@ -241,6 +275,8 @@ viewApp app readme =
] ]
] ]
-- HTTP -- HTTP
@ -248,7 +284,8 @@ 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
@ -259,24 +296,26 @@ loadREADME app =
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,18 +116,23 @@ 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 =
let let
@ -129,33 +140,42 @@ 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 }
app_href =
Route.toString <| Route.App_String { app = app.slug }
in in
div [ class "col-md-4 mb-3 col-sm-12" ] div [ class "col-md-4 mb-3 col-sm-12" ]
[ div [ class "card" ] [ div [ class "card" ]
@ -168,7 +188,8 @@ viewApp app =
, website_link , website_link
, a [ class "card-link", href app_href ] , a [ class "card-link", href app_href ]
[ i [ class "fas fa-book" ] [] [ i [ class "fas fa-book" ] []
, text "docs" ] , text "docs"
]
] ]
, div [ class "card-footer" ] , div [ class "card-footer" ]
[ span [ class "card-link badge badge-secondary" ] [ text app.category ] [ span [ class "card-link badge badge-secondary" ] [ text app.category ]
@ -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
(apps
|> List.sortWith
(by .status ASC (by .status ASC
|> andThen (String.toLower << .name) 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